0

Hey,everyone.. pls I need a very exhaustive example of a typical linked list for better understanding .Any help will be appreciated..Thanks

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by tkud
Featured Replies
  • [URL="http://lmgtfy.com/?q=c%2B%2B+linked+list+tutorial"]try this[/URL] In c++ I would use either <vector> or <list> c++ classes instead of creating your own linked list as you might do in C language. Read More

1

try this

In c++ I would use either <vector> or <list> c++ classes instead of creating your own linked list as you might do in C language.

Edited by Ancient Dragon: n/a

0

Here is a decent tutorial I found. I didn't read it all though.. just skimmed through it.

The basic structure of a linked list is this.

struct Node
{
        int info;   //Could be any data type really, this is the field for the actual data.
        Node *next; //This is a pointer the the next node in the list.
};

When I use linked lists I find it useful for keeping track of the first element in the list, called the head. This allows us to go back to the first element in the list, no matter what our current position is.

For creating a linked list with a head node and a current node you would do..

Node *head;
        Node *current;
        head = NULL;
        current = NULL;

        head = new Node;
        current = head;

Create the head node and then set current equal to the head.
After you set the current = head you will not need to do anything to the head node, unless you want to change it or its next pointer.

This is how you would add data to the current node.

cin >> current -> info;  // read in data and store it into info.

                current -> next = NULL;

                current -> next = new Node;  //create the next element in the list
                current = current -> next; // make the current element become the next.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.