Singly linked list in C(Inserting a node)

∥☆過路亽.° 提交于 2019-12-11 02:36:58

问题


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.


回答1:


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 lastpointer 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



回答2:


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.



来源:https://stackoverflow.com/questions/31701241/singly-linked-list-in-cinserting-a-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!