HELP! Linked List Insertion

This is a discussion on HELP! Linked List Insertion within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm needing some help with inserting into a linked list. I'm trying to do it with the function ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    HELP! Linked List Insertion

    Hey guys, I'm needing some help with inserting into a linked list.
    I'm trying to do it with the function I have created (see bottom of provided code), at first, I thought it worked fine, because after inserting, I can run my display() function, and it looks as if a new node has been added. However, upon closer inspection I realized that things were getting reaaally screwy, as I kept getting Segmentation Faults when I used my find function after inserting.

    To be honest... I have no idea why this won't work.. although I'm guessing its because the node I'm creating inside my function is only local to the function???
    I've been cruising the internets for a while now.. and it seems as if theres an easier way to insert if you have a "createnode()" function... but when I tried to implement that technique I just dug my hole deeper.

    Help would be appreciated beyond belief.



    Code:
    #include <stdio.h>
    
    
    typedef struct LinkedList NODE;
    
    struct LinkedList{
    	int info;
    	NODE *next;
    };
    
    
    int main()
    {
    int val;
    
    NODE first;
    first.info = 1;
    
    NODE second;
    second.info = 2;
    
    NODE third;
    third.info = 3;
    
    NODE fourth;
    fourth.info = 4;
    fourth.next = NULL;
    
    first.next = &second;
    second.next = &third;
    third.next = &fourth;
    
    NODE *top;
    top = &first;
    
    display(top);
    insert(top, &third, &val);
    display(top);
    
    return(0);
    }
    
    
    
    /*~~~~~~~~DISPLAY ELEMENTS~~~~~~~~~~~*/
    display(NODE *p)
    {
    printf("%d %d\n", (*p).info, *p);
    
    if(p->next == NULL)
    	{printf("KABLOOOOIE\n\n");
    	return;}
    
    display((*p).next);
    }
    
    /*~~~~~~~~~SEARCH ELEMENT~~~~~~~~~~*/
    search(NODE *p, int srch)
    {
    if(p->info == srch)
    	printf("Node %d contains %d\n", p, srch);
    if(p->next == NULL)
    	return;
    
    p=p->next;
    search(p, srch);
    }
    
    /*~~~~~~~~~DELETE ELEMENT~~~~~~~~*/
    delete(NODE *top, NODE *cur)
    {
    if(top == cur) 			  	//delete first node
    	{cur->next = NULL; printf("~You now need to change top to &Node2~\n");}
    
    else				  	//delete intermediary or last node
    while(top->next->info != cur->info)
    	top=top->next;
    
    top->next = cur->next;
    cur->next = NULL;
    }
    
    /*~~~~~~~~~INSERT ELEMENT~~~~~~~~*/
    insert(NODE *top, NODE *postnew, int *pval) 
    {
    printf("Input new nodes info: ");
    scanf("%d", pval);
    
    NODE new;
    new.info = *pval;
    new.next = postnew;
    
    while(top->next->info != postnew->info)
    	top=top->next;
    
    top->next = &new;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, that is part of your problem. You are adding a new node from the stack, and when the insert function ends, that stack entry goes away. malloc() a new node and you will fix that issue.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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