简体   繁体   中英

Singly linked list in C(Inserting a node)

I have this problem when I input. The program will freeze and a pop out window will open and says " .exe has stopped working. "

It is just a simple insert and display fuction of a singly linked list. I tried everything. I rewrote the code and find another way of inserting. I tried different compiler.. It works on turbo C but I am using devC++.

  • Is this a compiler error?

Here is the code:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <string.h>

typedef struct process
{
    int pNum;
    struct process *next;
}node;


node *create_node(node x)
{
    node *temp;
    temp=(node *)malloc(sizeof(node));

    if(temp==NULL)
    {
        return 0;
    }
    else
    {

        temp->pNum=x.pNum;
        temp->next=NULL;


    }
    return temp;
}


node *insert_node(node *head,node *last,node x)
{
    node *temp;

    temp=create_node(x);

    if(head==NULL && head==last)
    {
        head=temp;
        last=temp;
        head->next=NULL;
        last->next=NULL;
    }
    else
    {
        last->next=temp;
        last=temp;
        last->next=NULL;

    }

    return head;
}

int main()
{
    node *head=NULL,*last=NULL,*temp,input;
    int i,x,y,num;

    printf("INPUT NUMBER: ");
    scanf("%d",&num);


    x=0;
    y=6;

    for(i=0;i<num;i++)
    {

        gotoxy(39,y);
        scanf("%d",&input.pNum);

        head=insert_node(head, last, input);

        y++;
    }
    getch();
    return 0;
}

I think I have found out what line it stopped working. On the function insert_node

The line last->next=temp;

It seems I can't find what I had done wrong.

In your code, after the first entry head pointer will point to new value. Because of you are assigning the return value of that calling function. But last value will not be affect. Because of your are calling that as a pass by value. At next time head == last will be fail.

It will go to else block, and you are accessing the

 last->next=temp;

It is like accessing the null pointer this is the reason. If you need to avoid this you need to call last as pass by reference.

You need this:

node *insert_node(node *head, node **last, node x)
{
    node *temp;

    temp=create_node(x);

    if(head==NULL && head== *last)
    {
        head=temp;
        *last=temp;
        head->next=NULL;
        (*last)->next=NULL;
    }
    else
    {
        (*last)->next=temp;
        (*last)=temp;
        (*last)->next=NULL;

    }

    return head;
}

Call like this:

head=insert_node(head, &last, input);

Your function needs to modify the last pointer. In C values including pointers are passed by value. So if you modify the function argument inside the function, the argument passed to the function won't be modified. That is what happens in your program.

In the modified we don't pas simpty the last pointer but we pass a pointer to the last pointer which will allow us to modify the last pointer of the main function.

Simple Example:

int func1(int x)
{
  x = 10;
  return 2;
}

int func2(int *x)
{
  *x = 10;
  return 2;
}

...
int x = 3;
printf ("%d\n", func1(x));
printf ("%d\n", x);
printf ("%d\n", func2(&x));
printf ("%d\n", x);

Will print:

2
3
2
10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM