用C单链表(插入节点) [英] Singly linked list in C(Inserting a node)

查看:190
本文介绍了用C单链表(插入节点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题,当我输入。该计划将冻结和蹦出窗口将打开,并说:.EXE已停止工作。

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

这仅仅是一个单向链表的一个简单的插入和显示机能的研究。我什么都试过。我重写了code和发现插入的另一种方式。我尝试不同的编译器。它的工作原理上的Turbo C,但我使用DEVC ++。

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++.


  • 这是一个编译器错误?

下面是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;
}

我想我已经发现了什么行它停止工作。
在功能上 insert_node

最后所&gt;接下来=温度;

看来我无法找到我做了什么错。

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

推荐答案

您需要这样的:

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

您的功能需要修改末页指针。在C值包括指针是按值传递。所以,如果你修改函数内部的函数参数,传递给函数的参数将不会被修改。那是在你的程序发生了什么。

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.

在修改后的我们不PAS simpty的末页指针,但我们通过一个指向末页指针这将使我们修改函数的末页指针。

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.

简单的例子:

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

这篇关于用C单链表(插入节点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆