Linked List question

This is a discussion on Linked List question within the C Programming forums, part of the General Programming Boards category; I have a question about linked lists. I have this code below, which Im trying to familirize myself with by ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    38

    Linked List question

    I have a question about linked lists. I have this code below, which Im trying
    to familirize myself with by carefully breaking down each statement...
    But im having trouble with one line (see comments below).

    Code:
    struct list {
      int data;
      list *next;
    };
    int main() {
      list *first, *last, *current	// declare pointers to first, last, current elements in the linked list
      int value;			// this will store the value read from the user
      first = (list*)malloc(sizeof(list)); 	// assign memory to the first element
      first ->next=NULL;		// make the first element point to NULL (since there are no more...
    				// ...elements at the moment)
      last = first; 		// for the same reason, make last equal to first
    
      printf("Enter a new value (-1 to exit): ");	// prompt the user for a value
      scanf("%d", &value);		// ...and store it in value
      while(value != -1) {		// loop until -1 is entered
        current = (list*)malloc(sizeof(list));	// allocate memory to the current element
        current->data = value;	// assign value entered to the current element
        current->next = NULL;	// make current element point to NULL
        last = current;		// make current element the last one in the list
        last->next = current;	// this part I don't quite understand. Why do we need the...
    				// ...last point to the current? Would it just point to itself? What's...
    				// ...the difference between the previous statement (last = current)
    				// ...and this one?
        printf("Enter a new value (-1 to exit): ");	// prompt for a new value
        scanf("%d", &value); 	// store it
      }				// end of loop
    
    
    
    /*
    ignore these lines for the moment... I havent reached them yet... ^_^
        current = first; 	// Get the first element
        while(current->next != NULL) {	// loop until end of list
          current = current->next; 		// go to next element
          printf("%d ", current->data); 	// display data
      }
    */
    }
    Last edited by Karpaty; 01-08-2008 at 12:37 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The line in blue and the line above it should be switched. (You need to set the next field of what had, up to now, been the last element first, and then set last to point to the new last element.)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    last = current;		// make current element the last one in the list
    >    last->next = current;	// this part I don't quite understand. Why do we need the...
    				// ...last point to the current? Would it just point to itself? What's...
    				// ...the difference between the previous statement (last = current)
    				// ...and this one?
    Actually the second statement should be placed before the first one. Otherwise as you point out, the code makes no sense. The idea is link the new node to the end of the list (last->next = current), then update last to point to the new node.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Thanx guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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