Thread: please give me small and easy example for linked lists

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I wanted this example to be as small and easy to follow as possible - adding functions can confuse things
    Personally, I find that well selected and well named functions make code, even example code, much easier to follow. It beats trying to figure out why a particular block of code exists in the first place, and helps to skip over that block of code once I understand why it is there.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I decided to take a crack at it.

    I am afraid this is in C, but here is a barebones example of what a list is. It only implements the simplest of list methods (add to head and print).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct IntList {
       int data;
       struct IntList * next;
    };
    
    /**
     * Adds data to the beginning of the list.  Modifies the structure of the list.
     * @param head The list to modify.
     * @param addMe The data to add to the list.
     */
    void IntList_add2head (struct IntList ** head, int addMe) {
      
       // Create a new node to be inserted into the list. 
       struct IntList * NewNode = malloc (sizeof (struct IntList));
       NewNode->data = addMe;
    
       // Make the new node precede the head of the list.
       NewNode->next = *head;
    
       // Make the head point at the new node.
       *head = NewNode;
    }
    
    /**
     * Prints the contents of the list.
     * Output should look like...
     *    HEAD -> 5 -> 1 -> 7 -> NULL
     * @param head The list to print.
     */
    void IntList_print (struct IntList * head) {
       struct IntList * curr_node;
    
       printf ("HEAD ->");
       // The structure of this for loop is characteristic of linked lists.
       for (curr_node = head; curr_node != NULL; curr_node = curr_node->next) {
          printf (" %d ->", curr_node->data);
       }
       printf (" NULL\n");
    }
    
    int main (void) {
       // The end of a list is always NULL.  So, an empty list is one where
       // the head is NULL.
       struct IntList * head = NULL;
    
       printf ("Welcome to the linked list example.\n");
    
       // Repeatedly prompt the user for input and add items to the list.
       for (;;) {
          int new_value;
    
          printf ("\n");
          IntList_print (head);
          printf ("Input a number (x to exit): ");
          if (scanf ("%d", &new_value) != 1)
             break;
          IntList_add2head (&head, new_value);
       }
    
       return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Linked List is a cakewalk... Just do simpler programming if you're not comfortable with for loops or whatever else you said, all knowledge of basic programming is required to do linked lists... they come AFTER you learn everything else like memory and loops.

    If it helps you, try to draw a example on paper first to figure out what needs to happen to create/destroy/add/remove... then you can search/sort/ect... step by step, don't code 5 pages of code and expect it to work...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An small easy Metaball tutorial
    By MipZhaP in forum Game Programming
    Replies: 5
    Last Post: 08-26-2004, 12:11 PM
  2. Replies: 1
    Last Post: 11-16-2001, 06:22 PM