understanding linked list

This is a discussion on understanding linked list within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to understand linked lists. Can someone help me understand this linked list function: Code: Node* insertNode(Node*head,int ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    understanding linked list

    Hi,

    I am trying to understand linked lists. Can someone help me understand this linked list function:

    Code:
    Node* insertNode(Node*head,int value1){
      Node*newNode=(Node*)malloc(sizeof(Node));
      newNode->value=value1;
      newNode->link=NULL;
      if(head==NULL){
        head=newNode;
    }
      else{
                Node*temp=head;
                while(temp->link!=NULL){
                   temp=temp->link;}
                 
                temp->link=newNode;
              }
     return head;
    }{
    thanks

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    1
    This function is to insert elements to the end of a singly linked list.
    Assuming you know what a linked list is, you call the insertNode() passing it the pointer to the first node in the linked list(head) and the value of the element (value1) you want to insert to the rear of the list.
    Inside the function, first you allocate space for a Node and assign it value1, ie. newNode->value=value1 and also initialize the Node pointer to NULL.
    Next you check whether head is NULL, if yes it means that the linked list is empty and you are inserting the first element, hence you simply assign head to newNode and return head. So now head points to the first node.

    now suppose you call this function again, after allocating space for a new Node and assigning the value to it, you check if head is NULL. but as there is one Node in the list, it is not NULL. So now to insert the new Node to the rear of the list you have to find the last Node.
    the "else" part finds the last Node of the list, and then links the last Node to the newNode ie. temp->link=newNode

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This site explains linked lists well. Very easy concept. Here's a bigger illustration.


    Quzah.
    Last edited by quzah; 04-14-2006 at 04:27 AM. Reason: ooooh bigger picture!!
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    flyingfin thanks for the help. quzah the site you gave have a bunch of games inside it.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right. Barrel of Monkys. Perfect illustration of linked lists.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by quzah
    This site explains linked lists well. Very easy concept. Here's a bigger illustration.


    Quzah.
    lol oh now i get it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21