Thread: Linked list problem adding nodes

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    45

    Linked list problem adding nodes

    Hi all, I wrote function to add to elements in the list, but when I call printList function it returns me empty list ! I'm new with linked list in C so I need help and advices ...Thx

    Output:

    Empty list
    List is empty add element at the begining
    New node with packet num 245
    List is not empty add element at the end
    New node with packet num 486

    Linked list: Empty




    Main:

    Code:
    int main(){
    
    struct node * start ;
    start = NULL;
    int i;
    
    /*Check if list is empty*/
    if(start == NULL){
        printf("Empty list\n");
    }else{
        printf("List is not empty!\n");
    }
    
    start = add_node(start,245);
    printf("New node with number %ld\n",start->num);
    
    start = add_node(start,486);
    printf("New node with  number %ld\n",start->num);
    
    printList(start);
    
    return 0;
    
    }
    add_node and printList functions:

    Code:
    struct node * add_node(struct node * start, int num) {
    
        struct node * temp, *p;
        int i;
        temp = (struct node *) malloc(sizeof (struct node));
    
        //Enter new value
        temp->info = num;
    
        if (start == NULL) {
            printf("List is empty add element at the begining\n");
            temp->next = NULL; // start = NULL, new element is the last and must point at NULL
            start = temp; // start now points not to NULL but to new added element
            return start;
    
        } 
         // Add element to the end of the list
         else {
            printf("List is not empty add element at the end\n");
            p = start;
         
            while (p->next != NULL) {
                p = p->next;
            }
            temp->next = NULL;
            p->next = temp;
            return temp;
          
         }
    }
    
    
    void printList(struct node *start) {
    
        struct node *tmp = start->next;
        printf("Linked list: ");
    
        if (start->next == NULL) {
            printf("Empty");
        } else {
    
            while (tmp != NULL) {
                printf("%d ", tmp->info);
                tmp = tmp->next;
            }
        }
        printf("\n");
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. when list was not empty and you added node at the end - you need to return start instead of temp
    2. when you are printing the list - you need to test start == NULL to check that list is empty - you also shoudl not skip first node on the line 35
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    I changed 1. and 2. and it works now, thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding several nodes to a linked list using a loop
    By budala in forum C Programming
    Replies: 3
    Last Post: 09-11-2009, 06:05 AM
  2. adding new nodes to linked list
    By roaan in forum C Programming
    Replies: 8
    Last Post: 07-15-2009, 12:55 PM
  3. Adding nodes into linked list
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2007, 04:03 PM
  4. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  5. Logic problem adding nodes to linked list
    By SeanMSimonsen in forum C++ Programming
    Replies: 0
    Last Post: 04-02-2003, 07:04 PM

Tags for this Thread