Linked Lists? (Dummy 1, where is it coming from?)

This is a discussion on Linked Lists? (Dummy 1, where is it coming from?) within the C Programming forums, part of the General Programming Boards category; Hi guys! I am writing this simple program, to learn how to insert into linked list. It seems to be ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Linked Lists? (Dummy 1, where is it coming from?)

    Hi guys!

    I am writing this simple program, to learn how to insert into linked list. It seems to be working but for some reason, there is a one already in the list, when I have not even added any nodes.

    For example if I add nodes 1,2,3,4

    If will print 4->3->2->11

    Where is the extra one coming from?

    Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
         int data;
         struct node  *next;
    };
    
    struct node* insertNode(struct node* list, int value);
    void printList(struct node* list);
    
    int main (void) {
        
        int number = 0;
        int selection = 0;
    
        struct node* mainList;
        mainList = NULL;
    
    
        while(number != -1) {
            scanf("%d", &number);
                
            if(number != -1)
            mainList = insertNode(mainList, number);
                
        }
    
        while (selection != 10) {
    
            printf("1. Print the linked list.\n\n");
            printf("10. Quit\n\n");
            scanf("%d", &selection);
    
            if(selection == 1) {
                printList(mainList);
            }
    
        }
    
        system("PAUSE");
        return 0;
    }
    
    
    struct node* insertNode(struct node* list, int value) {
    
        struct node* newNode;
        newNode = (struct node*)malloc(sizeof(struct node));
        newNode->data = value;  
        newNode->next = NULL;
    
        if(list == NULL) {
            list = newNode;
        }
        else {
            newNode->next = list;
            list = newNode;
        }
    
        return list;
    }
    
    void printList(struct node* list) {
        
        while(list != NULL) {
            printf("->%d", list->data);
            list = list->next;
        }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    You should print a descriptive prompt so I know how to run your program without having to read all your code. Also, you should give an accurate description of the "bad" output you are getting. You only gave part of that line of output, where as providing the whole thing would make the error obvious to us (without our having to run/debug the program) and you as well. Copy-paste your terminal session that shows your exact input and output in the future. Last little thing, system("PAUSE") is non-portable, and doesn't work for people like me (Linux users). Look at better ways of FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com.

    That being said, I must congratulate you on a proper declaration of main, actually returning an integer and having properly indented code, all three of which would create a lot of flak here, and which many newbies seem to have trouble with.

    Here is what I get when I run your program:
    Code:
    $ ./list
    1
    2
    3
    4
    -1
    1. Print the linked list.
    
    10. Quit
    
    1
    ->4->3->2->11. Print the linked list.
    
    10. Quit
    
    10
    Is it really an extra 1 in your list, or are you just not reading your program's output carefully? Printing a newline ('\n') after you print your list would have helped clear this up. Making sure you have good, clean output for your program is critical to avoiding simple mistakes like this.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    OK definitely will do next time!

    Thanks for your help....I guess that was my fail for the day -___- lol.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    all and good but i dont see any clean up..... ie you allocate space for your linked list, but you dont go through and free every object in your list. this can result in memory leaks and an unstable program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 12:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Circular doubly linked list with dummy header
    By mag_chan in forum C Programming
    Replies: 5
    Last Post: 10-31-2005, 07:44 AM
  4. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21