Thread: HELP! Linked List Insertion

  1. #1
    Registered User
    Join Date
    Apr 2010

    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.

    #include <stdio.h>
    typedef struct LinkedList NODE;
    struct LinkedList{
    	int info;
    	NODE *next;
    int main()
    int val;
    NODE first; = 1;
    NODE second; = 2;
    NODE third; = 3;
    NODE fourth; = 4; = NULL; = &second; = &third; = &fourth;
    NODE *top;
    top = &first;
    insert(top, &third, &val);
    /*~~~~~~~~DISPLAY ELEMENTS~~~~~~~~~~~*/
    display(NODE *p)
    printf("%d %d\n", (*p).info, *p);
    if(p->next == NULL)
    /*~~~~~~~~~SEARCH ELEMENT~~~~~~~~~~*/
    search(NODE *p, int srch)
    if(p->info == srch)
    	printf("Node %d contains %d\n", p, srch);
    if(p->next == NULL)
    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->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; = *pval; = postnew;
    while(top->next->info != postnew->info)
    top->next = &new;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    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, 03:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01: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