Thread: Linked List problem

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Linked List problem

    I am writing a few functions for link lists, but I can't get these to work, and I can't see what the problem is. The NewList() function works, but the other two don't. If I use these functions to make two lists which are linked, then they end up pointing to each other, and so the linked lists makes a cycle.

    Code:
    struct DATA {
           int val;
           struct DATA *link;
           };
           
    typedef struct DATA INFO;
    typedef INFO *LISTPTR;
    
    LISTPTR NewList(void);
    LISTPTR AddListStart(LISTPTR base);
    LISTPTR AddListEnd(LISTPTR base);
    
    LISTPTR NewList(void)
    {
            LISTPTR base=NULL;
            base = (LISTPTR)malloc(sizeof(INFO));
            
            if(base != NULL) {
                    base->link = NULL;
                    return base;
                    }
                    
            return NULL;
    }
    
    LISTPTR AddListEnd(LISTPTR base)
    {
            LISTPTR temp=NULL;
            LISTPTR current=NULL;
            
            if(base != NULL) {
                    current = base;
                    while(current->link != NULL) {
                                        current = current->link;
                                        }
                                        
                    temp = (LISTPTR)malloc(sizeof(INFO));
                    
                    if(temp != NULL) {
                            current->link = temp;
                            temp->link = NULL;
                            }
                    }
            FreePointers(temp, current, NULL);
            return base;
    }
    
    LISTPTR AddListStart(LISTPTR base)
    {
         LISTPTR temp;
         LISTPTR current;
         
         if(base == NULL) {
                 // This function doesn't start a new list, NewList() does that
                 }
                 else {
                      temp = (LISTPTR)malloc(sizeof(INFO));
                      
                      if(temp != NULL) {
                              temp->link = base;
                              base = temp;
                              }
                      }
         
         FreePointers(temp, current, NULL);
         return base;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Link lists work because the pointers to data aren't freed until you're done with a part of or the whole list.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's considered bad practice to cast the return of malloc.
    Also, avoid pointer typedefs as you have, because they are obfuscating your code (making it harder to read).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM