Thread: HELP! have an exam tommorrow.

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    HELP! have an exam tommorrow.

    Hello all.. was wondering if someone could tell me how this linked list is linked together. I've tried drawing memory pictures and have gone over it a thousand times and can't understand it. For some reason it prints the input 3 1 4 1 5 backwords. If u can show me how they are linked together. Thank you.

    /*
    Linked lists: sections 12.8 through 12.12.
    Try 3 1 4 1 5.
    */

    #include <stdio.h>
    #include <stdlib.h>

    struct intlist {
    int data;
    struct intlist *next;
    } ;

    int main(void)
    {
    struct intlist head = { 0, 0 };
    struct intlist *t;
    int n;

    while (scanf("%d",&n) == 1) {
    t = (struct intlist *) malloc(sizeof(struct intlist));
    if (!t) { fprintf(stderr,"fatal: out of memory\n"); exit(1); }

    t->data = n;
    t->next = head.next;
    head.next = t;
    }

    for (t = head.next;t;t = t->next)
    printf("%d\n",t->data);

    exit(0);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You can either add to a linked list at the head or the tail. You are doing it at the head. Let's look at how that works..., with input 3 1 4 1 5
    Code:
    NULL
    3 -> NULL
    1 -> 3 -> NULL
    4 -> 1 -> 3 -> NULL
    1 -> 4 -> 1 -> 3 -> NULL
    5 -> 1 -> 4 -> 1 -> 3 -> NULL
    Let's look at this running through your code real quick...
    You are using head.next as the beginning of list which is... well, strange, but not a problem really...
    Code:
    t->data = n;
    Okay, this creates the node, but we haven't told it where to point to yet.
    Code:
    t->next = head.next;
    This little snippet tells the node that the node after it is the beginning of the list.
    Code:
    head.next = t;
    And this snippet assigns the beginning of the list to point at the newly created node, so that every time you make a new node, you make it the first element in the list, and make what used to be the first element in the list the one after it... like in the picture.

    Here is some code that adds to the end of the list...
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    struct intlist { 
    int data; 
    struct intlist *next; 
    } ; 
    
    int main(void) 
    { 
     struct intlist head = { 0, 0 }; 
     struct intlist *t;
     int n;
    
     t = &head;
     while (scanf("%d",&n) == 1) { 
      t -> next = malloc(sizeof(struct intlist)); 
      if (!t -> next) { fprintf(stderr,"fatal: out of memory\n"); exit(1); } 
    
      t = t -> next;
      t -> data = n; 
     // Since t is the last element, the node after it is NULL
      t -> next = NULL;
     // head.next = t; Value of head needn't change.
     } 
    
     for (t = head.next;t;t = t->next) 
      printf("%d\n",t->data); 
    
     exit(0); 
    }
    Or you could just search for the end of the list every time you inserted a node, instead of keeping your pointer t always at the end of the list...
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    struct intlist { 
    int data; 
    struct intlist *next; 
    } ; 
    
    int main(void) 
    { 
     struct intlist head = { 0, 0 }; 
     struct intlist *t;
     int n;
    
     while (scanf("%d",&n) == 1) { 
    
      // This is really unneccisary, but it's helpful to know that this 
      //  will run to the node such that t -> next == NULL
      for (t = &head; t -> next != NULL; t = t -> next);
    
      t -> next = malloc(sizeof(struct intlist)); 
      if (!t -> next) { fprintf(stderr,"fatal: out of memory\n"); exit(1); } 
    
      t = t -> next;
      t -> data = n; 
     // Since t is the last element, the node after it is NULL
      t -> next = NULL;
     // head.next = t; Value of head needn't change.
     } 
    
     for (t = head.next;t;t = t->next) 
      printf("%d\n",t->data); 
    
     exit(0); 
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  2. Lab exam
    By hawkins786 in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 11:24 AM
  3. Exam of programming
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-15-2005, 03:40 PM
  4. What do you think of my exam?
    By axon in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-03-2004, 08:30 PM
  5. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 09:46 PM