【问题标题】:Singly linked list in C(Inserting a node)C中的单链表(插入节点)
【发布时间】:2015-10-20 11:32:17
【问题描述】:

我在输入时遇到了这个问题。程序将冻结并打开一个弹出窗口并显示“.exe 已停止工作。”

这只是一个简单的单链表插入和显示功能。我什么都试过了。我重写了代码并找到了另一种插入方式。我尝试了不同的编译器。它适用于 turbo C,但我使用的是 devC++。

  • 这是编译器错误吗?

代码如下:

#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;
}

我想我已经找到了它停止工作的线路。 关于函数insert_node

一行last-&gt;next=temp;

我好像找不到我做错了什么。

【问题讨论】:

  • 现在,只是一个整数。
  • 当您调用insert_node(head, last, input) 时,您希望last 被修改,但事实并非如此。
  • @RachitAhuja 指向指针?也许是因为我的函数返回了一个指针?我尝试了另一种方法,只是 void。仍然没有为我工作。
  • 我之所以说这可能是编译器错误,因为我已经多次使用此代码,仅使用 Turbo C。但现在,我使用的是 devC++。

标签: c singly-linked-list


【解决方案1】:

在您的代码中,第一个条目后的头指针将指向新值。因为您正在分配该调用函数的返回值。但最后一个值不会受到影响。因为您将其称为按值传递。在 下次 head == last 将失败。

它将进入 else 块,并且您正在访问

 last->next=temp;

这就像访问null 指针,这就是原因。如果您需要避免这种情况,您需要调用 last 作为引用传递。

【讨论】:

  • 我会试试的。我之所以说这可能是编译器错误,是因为我已经多次使用此代码,仅使用 Turbo C。但现在,我使用的是 devC++。
【解决方案2】:

你需要这个:

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;
}

这样调用:

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

您的函数需要修改last 指针。在 C 中,包括指针在内的值是按值传递的。因此,如果您在函数内部修改函数参数,则传递给函数的参数将不会被修改。这就是你的程序中发生的事情。

在修改后,我们没有简单地传递lastpointer,而是传递一个指向last 指针的指针,这将允许我们修改main 函数的last 指针。

简单示例:

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);

将打印:

2
3
2
10

【讨论】:

  • 第一次输入时会弹出窗口。 “.exe 停止工作”
  • 我之所以说这可能是编译器错误,因为我已经多次使用此代码,仅使用 Turbo C。但现在,我使用的是 devC++。
  • 这里工作正常。我确定这不是编译器错误。即使是 Turbo C 也应该正确编译这样的简单代码。如果忘记提及您必须如何调用该函数,我只是编辑了我的答案。
  • 它有效。现在我不明白,我在我的项目中多次使用了错误的代码。效果很好。
  • 您可能对未定义的行为感到震惊。随意接受答案。
猜你喜欢
  • 2019-04-26
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2020-02-18
  • 2018-04-05
  • 1970-01-01
相关资源
最近更新 更多