Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 26th, 2004, 09:59 AM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Linked List....!!!

hello...
I am facing a problem with whats called "linked lists".....my problem is ...that when i read the ecxplanation of linked list in books i understand it....especially the visuual examples....but in the coding its really hard for me to understand.it...can anyone advise me..what to do..?...
thanks

Reply With Quote
  #2 (permalink)  
Old March 26th, 2004, 12:37 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I suggest you go to google (or your favorite search portal) and look for
"linked list tutorial"

There is lots of stuff out there; maybe you can find something that is easier to understand than your book. The probability that I personally could do better than what you have already seen is low; some of the sites have code snippets and examples that you might be able to use.

Dave
Reply With Quote
  #3 (permalink)  
Old March 30th, 2004, 09:09 PM
Registered User
 
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Link list is very easy to implement in C++. Below is all that you will
ever need to handle linked list. It is Lifo structured list. For the FIFO I hope that by next week you will be able to do it by yourself. Let me know if you don't get it.

Judex

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>


//create global variables

void push(int); // add data elements to list
void pop(); // delete data from list
void readp(); // read data





struct link
{
 int prime;
 link *next;
};

link *first = NULL;
main ()
{
 int param,remainder;

 push(2);
 push(3);
 push(5);
 push(7);
 push(11);
 readp();
 pop();


 readp();
 getch();
}

void push(int pp)
{
 link *newlink= new link;
 newlink ->prime = pp;
 newlink->next = first;
 first=newlink;
}

void pop()
{
 first = first->next;
}

void readp()
{
  link *current = first;
  while (current != NULL)
        {
         cout << current->prime;
         cout << " ";
         current = current->next;
        }
}


}


Reply With Quote
  #4 (permalink)  
Old March 31st, 2004, 03:12 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you judex......your example gave me a sense of what is really linked list..and this is what i wanted....

thank yo very much

Reply With Quote
  #5 (permalink)  
Old April 6th, 2004, 01:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, sorry, you're wrong. A linked list is definitely NOT a LIFO structure. (LIFO means "Last In, First Out"). That's a Stack. You can implement a stack using a linked list, or array, or vector, etc... but one is definitely _NOT_ forced to follow this restriction on how items are inserted or removed from a linked list

The above code sample is also a really poor example of C++. The only thing that really makes it C++ is the use of cout.

Otherwise, it's a very C-ish implementation -- you don't use a Linked List or Linked List Node object, everything's declared as individual functions in global scope.

Also, notice that your pop() method doesn't delete nodes being removed from the list. You've got a blatantly obvious memory leak. You use "new" to create new list nodes, but don't use delete to free them.


A linked list is simply described as a data structure where each node in the list is "connected" to the node next to it in succession. You can also create a "doubly-linked" list, where each node is connected to the node before it as well. There is usually a node which serves the role of the "head" of the list. This means that insertions and deletions to the head of the list are fairly quick and operate in constant time.

This also means that searching for an item in the list or inserting at the back of the list is a relatively expensive operation, and occurs in linear time. (That is, the time it takes to perform the operation grows proportionally to the size of the list. "Size of the list" means the number of elements in the list.


Again, you're much better off searching for a Linked List tutorial on the net than using the above example for guidance.

I recommend searching for a well-rounded tutorial on Data Structures. This tutorial should cover data structures including Stack, Queue, Deque (Double-Ended Queue), Linked List, Hash Table, Binary Tree, Priority Queue, etc. It should definitely mention the time complexity of each operation (insert, delete, search) for each structure.

As a developer, you'll find yourself in situation where several data structures will definitely serve to solve a problem, but usually one or two stand out as being the "ideal" choice when you think about the way you're going to USE the structures.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List? pmcizhere Java Basics 2 March 6th, 2008 09:02 PM
Linked List Implementation shah123 C# 0 April 17th, 2007 09:20 AM
2D Linked List sjf905 C# 0 October 8th, 2006 11:02 PM
C# Object Linked List millsbruce C# 5 July 12th, 2005 04:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.