Linked List won't print

This is a discussion on Linked List won't print within the C Programming forums, part of the General Programming Boards category; I'm having trouble getting my linked list to print out, I think the problem is that the head is not ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    4

    Linked List won't print

    I'm having trouble getting my linked list to print out, I think the problem is that the head is not linked to the rest of list which is why it only prints the head when I use the print function, but I don't know how to fix it.....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "Lab8.h"
    
    
    
    
    struct node *head = NULL;
    struct node *last = NULL;
    struct node *temp;
    
    
    struct node *add()
    {
            int input = 1;
            head = malloc(sizeof(struct node));
            last = malloc(sizeof(struct node));
    
    
            while(input != -1)
            {
            printf("\nPlease input a dight 0-9(-1 to Stop): ");
            scanf("%d", &input);
            if(input == -1)
            {
            printf("head ");
            printf("%d\n", head->value);
            printf("temp ");
            printf("%d\n", temp->value);
            printf("last ");
            printf("%d\n", last->value);
            return(head);
            }
            temp = malloc(sizeof(struct node));
    
    
            temp->value = input;
            temp->next = NULL;
    
        printf("head ");
            printf("%d\n", head->value);
            printf("temp ");
            printf("%d\n", temp->value);
            printf("last ");
            printf("%d\n", last->value);
    
    
            if(head->value == 0)
            {
            head = temp;
            printf("if1\n");
            }
            else
            {
            printf("else1\n");
            last->next = temp;
            last = temp;
    
    
            }
    }
            return(head);
    }
    
    void print(struct node *list)
    {
            while(list != NULL)
            {
            printf("%d\n", list->value);
                    list = list->next;
            }
    }
    
    
    void _delete(int v)
    {
    
    
    }
    
    
    struct node *find(int v)
    {
            return 0;
    }
    
    void menu()
    {
            char pick[3];
            int FindThisNum;
            int DeleteThisNum;
            struct node *ptr;
            printf("\nLink List Options Menu\n");
            printf("(A)dd Items\n");
            printf("(P)rint Items\n");
            printf("(D)elete an Item\n");
            printf("(F)ind an Item\n");
            printf("(Q)uit\n\n");
            printf("Select one Option: ");
            scanf("%s", pick);
            char choice = pick[0];
    
    
     switch(choice)
            {
            case 'A':
            case 'a':
            {
                    ptr = add();
                    break;
            }
    
    
            case 'P':
            case 'p':
            {
                    print(head);
                    break;
            }
     case 'D':
            case 'd':
            {
                    printf("Which digit would you like to Delete: ");
                    scanf("%d", &DeleteThisNum);
                    _delete(DeleteThisNum);
                    break;
            }
    
    
                    case 'F':
            case 'f':
            {
                    printf("Which digit would you like to Find: ");
                    scanf("%d", &FindThisNum);
                    find(FindThisNum);
                    break;
            }
    
    
            case 'Q':
            case 'q':
            {
                    printf("Goodbye!\n");
                    exit(1);
                    break;
            }
    
     default:
            {
                    printf("Your entry was not valid, Please try again");
            }
    
    
            }
    menu();
    }
    
    
    int main()
    {
            menu();
            return 0;
    }

  2. #2
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Fix add function.
    Get a papper, draw (just some cubes + pseudoalgorithm) the list according to your add function and see what's going on.
    Then draw on papper the appropriate way.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    I've done that....didn't help

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Can you please post the fixed one add function? Because the one of the first post has problems.For example,every time you add something you are allocating new memory for the head and last.I suggest you name last to tail
    Also then i see that if the input is -1 you request for these
    Code:
    {
            printf("head ");
            printf("%d\n", head->value);
            printf("temp ");
            printf("%d\n", temp->value);
            printf("last ");
            printf("%d\n", last->value);
    but you never assign a value to a node.

    Also mind this situation : You have allocated memory and you have a pointer that is pointing to this memory malloc returned to you.
    Then you call malloc again and assign it to the very same pointer.The newly allocated memory will be the one that this pointer is going to point to.So what happened with the old one?
    memory leak!!!!!
    you have lost access to it and this is now garbage in your memory.
    If you want to use malloc and use the same pointer,first free it and then use it for malloc assignment

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    It would help if you provided Lab8.h, so we have any declarations we may need.

    Your use of globals is not the cause of your problem, but global variables are generally a very bad idea, especially for newer programmers. They usually cause more problems then then solve, so declare head and tail in main or menu and pass them to the functions as needed. Declare temp as a local variable in the appropriate function(s). For more info on the problems with globals, see here: Global Variables Are Bad.

    Then, think about your add function. Go through every line of code there. Pay extra close attention to lines 16 and 17. What happens to the old head and tail, and the previous nodes in the list when you call add() for a second, third, etc time?

    As a matter of design, functions should do one thing and do it well. Your add function should only add a node to the list, it should not involve any user input or print any output (except debugging output as necessary). Ask the user what number they want to add in your menu function perhaps, and then simply call add(number). The same goes for your other functions.

    Also, you menu function should not call itself recursively. If somebody runs your program and uses the menu a lot, it can crash your program (you run out of space for new copies of the menu() funciton on the stack). Use a loop for this (while or do loops are good).

  6. #6
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    The very fisrt step before add a node is to initialize the head/last pointers to NULL propably into main.
    Decide the way new items should be inserted to list. (Let's say at the end)
    In the beggining (empty list) head and last are pointing to the same node (aka NULL)
    Adding an item:
    1. Store the new value to an int
    2. Allocate memory (malloc) of the new node to temp pointer. Initialize it. (temp->next = NULL, value = 0 etc)
    3a. if this is the first time adding item then, both head/last pointers should point to temp and next pointer of temp to NULL
    3b. else, next pointer of last node should point to temp and next of temp should point to NULL.
    4. Copy the new value and return new node

    The fifth rule : A real programmer doesn't eat until debugging ends.

    Name:  lab5-applyToAll.png
Views: 93
Size:  13.5 KB
    Last edited by ch4; 10-31-2012 at 01:07 PM.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Just got back from lunch here is Lab8.h...while I read all the replies.

    Code:
    struct node
    {
        int value;
        struct node *next;
    };
    
    
    struct node *add();
    void print(struct node *list);
    void _delete(int v);
    struct node *find(int v);
    void menu();

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jrclark View Post
    Just got back from lunch here is Lab8.h...while I read all the replies.
    In my opinion i would say that it is better to post all your replies in one post.
    For example, when you read the answers you are going to post again.So why not enclose the information in one package and enclose it in two?

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Quote Originally Posted by std10093 View Post
    In my opinion i would say that it is better to post all your replies in one post.
    For example, when you read the answers you are going to post again.So why not enclose the information in one package and enclose it in two?
    Good thing everyone is entitled to there own opinion.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Find a tutorial on pointers. I'm sure this site has one. You don't seem to understand enough about them yet.
    For example, you don't realise how its wrong to be calling malloc more than once to perform a single insert inside your add function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List - Print Backwards
    By skier21 in forum C Programming
    Replies: 9
    Last Post: 07-27-2012, 06:11 PM
  2. Print function in linked list
    By alphasil in forum C Programming
    Replies: 4
    Last Post: 07-08-2012, 08:52 AM
  3. print linked list problem
    By giannis1990 in forum C Programming
    Replies: 8
    Last Post: 05-29-2012, 11:14 AM
  4. i want to print my linked list in the reverse order please help
    By raghu_equinox in forum C Programming
    Replies: 9
    Last Post: 10-14-2006, 12:45 AM
  5. Linked List print out
    By axon in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2003, 07:15 PM

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