Thread: Linked list source code errors

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Linked list source code errors

    I have been using an online C tutorial to study C. The tutorial is ctutor.pdf, by Tim Ward, which can easily be found online. I'm currently studying Linked lists. The example code seems to have logical errors, which I'm not capable of correcting because I'm new to the concept.

    Could someone find these errors and correct them for me and repost the source code for me to study?

    Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define RECORDS 6
    
    main( )
    {
      struct animal {
        char name[25]; /* The animals name */
        char breed[25]; /* The type of animal */
        int age; /* The animals age */
        struct animal *next; /* a pointer to another record of this type */
      } *point, *start, *prior; /* this defines 3 pointers, no variables */
    int index;
    
    /* the first record is always a special case */
      start = (struct animal *)malloc(sizeof(struct animal));
      strcpy(start ->name,"general");
      strcpy(start ->breed,"Mixed Breed");
      start->next = NULL;
      prior = start;
    
      /* a loop can be used to fill in the rest once it is started */
      for (index = 0;index < RECORDS;index++) {
        point = (struct animal *)malloc(sizeof(struct animal));
        strcpy(point->name,"Frank");
        strcpy(point->breed,"Laborador Retriever");
        point->age = 3;
        point->next = point; /* point last "next" to this record */
        point->next = NULL; /* point this "next" to NULL */
        prior = point; /* this is now the prior record */
      }
    
      /* now print out the data described above */
      point = start;
      do{
        prior = point->next;
        printf("%s is a %s,and is %d years old.\n", point->name,
        point->breed, point->age);
        point = point->next;
      } while (prior != NULL);
    
      /* good programming practice dictates that we free up the */
      /* dynamically allocated space before we quit */
      point = start; /* first block of group */
      do{
        prior = point->next; /* next block of data */
        free(point); /* free present block */
        point = prior; /* point to next */
      } while (prior != NULL); /* quit when next is NULL */
    
      getch();
    }
    Also, I would appreciate if someone could link me to some useful resources that explain Linked lists. A Linked list is the most difficult programming concept yet. =[

    Thanks.

    ~Billy
    Last edited by cb0ardpr0gr^mm3r; 11-22-2010 at 04:46 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
        point->next = point; /* point last "next" to this record */
        point->next = NULL; /* point this "next" to NULL */
    It looks like this should be:
    Code:
        prior->next = point; /* point last "next" to this record */
        point->next = NULL; /* point this "next" to NULL */
    I haven't tested the code or anything, but I can definitely tell you the original two lines are definitely not correct.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The first thing to do is throw away that garbage tutorial! It's very outdated, DOS/Applix specific, full of bad practices and they clearly never even tested their examples.

    That being said, here is what I changed to get it to "work":
    Line 28: change "point->next" to "prior->next"

    Other suggestions (so you don't learn bad practices):
    1. Declare main to return int and put a return 0 at the bottom of main.
    2. Don't cast the result of malloc
    3. Fix the potential null pointer/seg fault in the print and free loops, by always checking the iterator before you do anything with the node (a do-while loop always exectutes at least once, whereas a while loop might not execute at all):
    Code:
    point = start;
    while (point) {
        // do some stuff
        point = point->next;
    }
    4. Learn good design by creating an API with insert, delete and print funcitons for your linked list.

    Check out the following link for a much better tutorial on C: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  3. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM