Thread: Help with linked lists/ pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    7
    Thanks for all of the replies. I see what you guys mean about the form it is in and have changed the code so that it shouldn't be as confusing. Here is the changed code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node {
      int value;
      struct node *next;
    } node;
    
    void createNewNode(node *head, node *newNode);
    void printList(node *head);
    
    int main(int argc, const char * argv[]) {
        node *first;
        first = malloc(sizeof(node));
        node *newNode;
        newNode = malloc(sizeof(node));
        int numberOfEntries = 0;
    
        first->next = NULL;
    
        printf("Number of Entries: ");
        scanf("%d", &numberOfEntries);
        printf("\n");
    
        if(numberOfEntries == 1) {
            printf("Enter Value: ");
            scanf("%d", first->value);
            printf("\n");
        } else {
            int i = 0;
            while(i < numberOfEntries) {
                printf("Enter Value: ");
                scanf("%d", first->value);
                printf("\n");
    
                createNewNode(first, newNode);
                i++;
            }
        }
        printList(first);
    
        return 0;
    }
    
    void createNewNode(node *head, node *newNode) {
        newNode->next = head;
        head = newNode;
    }
    
    void printList(node *head) {
        node *current;
        current = malloc(sizeof(node));
        current = head;
    
        while(current->next != NULL) {
            printf("Entry: %d\n", current->value);
            current = current->next;
        }
    
        printf("Entry: %d\n", current->value);
    
        free(current);
    }
    I am still pretty lost with what else is wrong with the code.... Linked lists are confusing the hell out of me lol
    Last edited by anything25; 06-26-2009 at 01:05 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    7
    Also, as Ive been playing with the code I've realized that the while loop in the printList function never executes. I think I have that code right so I'm assuming that it is probably due to one of my other errors in the code.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's a little thing that might cause a problem:
    Code:
    scanf("%d", &first->value);
    Again, this is a value vs. reference issue; scanf expects an address reference, so use the &address of operator.

    It's not that the printList while loop isn't executing, it's that createNewNode doesn't work. Also, why do you have a malloc call in printList()? Malloc allocates memory for a node -- that is part of what is missing in createNewNode.

    You do not need to create more than one *node in main(), namely "head", and malloc it there. No matter what, for the first entry, use that one. For the subsequent ones, you pass the head to createNewNode, which malloc's a new node and set's the last node's next to that:
    Code:
    void createNewNode(node *head, int val) {
    	node *new = malloc(sizeof(node)), *cur=head;
    	new->value = val;
    	while (cur->next) cur=cur->next;
    	cur->next=new;
    }
    So now your main should look something like this:
    Code:
       node *first = malloc(sizeof(node));
        int numberOfEntries = 0, value;
    	int i = 0;
    
        printf("Number of Entries: ");
        scanf("%d", &numberOfEntries);
        printf("\n");
    
    	printf("Enter Value: ");
    	scanf("%d", &first->value);
    	printf("\n");
            while(i < numberOfEntries-1) {
                printf("Enter Value: ");
                scanf("%d", &value);
                printf("\n");
    
                createNewNode(first,value);
                i++;
            }
    Zlatko, becasue of issue #5 above, uses a pointer to a pointer (**) to accomplish the same thing, you may find my method somewhat simpler and easy to understand (or it may be the other way around).

    @tabstop: my point was the unorthodox typedef'ing is the first mistake that needs correcting. Neither I nor you nor anyone else will want to correct the OP while maintaining that.
    Last edited by MK27; 06-26-2009 at 01:38 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Linked Lists
    By wvu2005 in forum C Programming
    Replies: 12
    Last Post: 10-06-2005, 11:36 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  5. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM