Thread: Cannot make linked lists work

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    19

    Cannot make linked lists work

    Hi! I am trying to create a simple program to create a linked list and delete one of the nodes. When I run my program the print out for the "old list" is 4 and the "new list" is nothing. I am not sure if my problem is in the way I print the lists or in the way I make them or both. From some examples I have found on the web I suspect the problem is in how I print the lists, but I don't know for sure. My code is below:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct myllist
    {
            int info;
            struct myllist * link;
    } myllist;
    
    void GetNewNode (int newInfo, myllist * pNode);
    void disposeOfNode(myllist *change, myllist *remove);
    
    main(){
           //setup nodes
           myllist * pNode;
           pNode = (myllist *) malloc(sizeof(*pNode));
           GetNewNode(4, pNode);
           
           myllist * pNode2;
           pNode2 = (myllist *) malloc(sizeof(*pNode));
           GetNewNode(5, pNode2);
           
           myllist * pNode3;
           pNode3 = (myllist *) malloc(sizeof(*pNode));
           GetNewNode(6, pNode3);
           
           //print values to check
           printf("%d\n%d\n%d\n", pNode->info, pNode2->info, pNode3->info);
           
           //link nodes together
           pNode->link = pNode2;
           
           //print list
           printf("Old List:\n");
           myllist * tmp;
           tmp = pNode;
           while(tmp->link != NULL){
                           printf("%d", tmp->info);
                           tmp = tmp->link;
                           }
           
           //get rid of pNode2
           disposeOfNode(pNode, pNode2);
           
           //print new list
           printf("\nNew List:\n");
           tmp = pNode;
           while(tmp->link != NULL){
                           printf("%d", tmp->info);
                           tmp = tmp->link;
                           }
           
           //end
           getchar();
    }
    
    void disposeOfNode(myllist *change, myllist *remove){
         change->link = remove->link;
         free(remove);
         return;
         }
    
    void GetNewNode (int newInfo, myllist * pNode){
          /*myllist * pNode;
          pNode = (myllist *) malloc(sizeof(*pNode));*/
          if(pNode == NULL){
                   printf("error: malloc failed");
                   }else{
                         pNode->info = newInfo;
                         pNode->link = NULL;
                         }
          return;
    }
    Could someone tell me what I did wrong? I have been trying to figure this out for weeks without luck. Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
         while(tmp->link != NULL){
                           printf("%d", tmp->info);
                           tmp = tmp->link;
                           }
    This will never print out the last entry in the list. You should check whether tmp itself is NULL instead.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    This code is pure C (my clue was that your main() doesn't return int explicitly). A moderator should move this to the C forum.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, and how about typedef struct, free, malloc and printf?
    C. All C! And C99 too, it looks like, if this compiles.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    19
    Quote Originally Posted by tabstop View Post
    Code:
         while(tmp->link != NULL){
                           printf("%d", tmp->info);
                           tmp = tmp->link;
                           }
    This will never print out the last entry in the list. You should check whether tmp itself is NULL instead.
    Thank you, I fixed that and now my program works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  3. Linked Lists in C++
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2002, 07:28 PM
  4. Replies: 1
    Last Post: 03-21-2002, 06:10 PM
  5. Linked lists
    By sballew in forum C Programming
    Replies: 6
    Last Post: 10-24-2001, 08:52 PM