Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个链表代码在c中出现故障?_C_Linked List - Fatal编程技术网

为什么这个链表代码在c中出现故障?

为什么这个链表代码在c中出现故障?,c,linked-list,C,Linked List,我用c编写了这段代码来实现链表。虽然语义和语法都很好,但它并没有发挥应有的作用。例如,当我在列表中插入1,然后打印链接列表时,它显示列表为空 #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *next; }; typedef struct node Node; void addNode(Node *head, int x) { Node

我用c编写了这段代码来实现链表。虽然语义和语法都很好,但它并没有发挥应有的作用。例如,当我在列表中插入1,然后打印链接列表时,它显示列表为空

#include<stdio.h>
#include<stdlib.h>

struct node {
    int info;
    struct node *next;
}; typedef struct node Node;


void addNode(Node *head, int x)
{
        Node *temp;
        temp=malloc(sizeof(temp));
        temp->info=x;
        if (head==NULL)//if this is the first node in the list
        {
            head=temp;
            temp->next=NULL;

        }
        else //if not, add it as the head
        {
            temp->next=head;
            head=temp;
        }
}

void appendNode(Node *head, int x)
{
    Node *rear =head;
    Node *temp= malloc(sizeof(temp));
    temp->info=x;
    temp->next=NULL;
    while (rear->next!=NULL)
        rear=rear->next;
    rear->next=temp;
    rear=temp;

}

void insertNodeafter(Node *head, int location, int x)
{
    int i;
    Node *before, *after = head;
    Node *temp= malloc(sizeof(temp));
    temp->info=x;

    for (i=0;i<location;i++)
        before=before->next;
    after=before->next;
    temp->next=after;
    before->next=temp;

}

void insert(Node *head, int x)
{
    int c=0;
    Node *temp;
    temp=head;
    if(temp==NULL)
    {
    addNode(temp,x);
    }
    else
    {
    while(temp!=NULL)
    {
        if(temp->info<x)
        c++;
        temp=temp->next;
    }
    if(c==0)
        addNode(temp,x);
    else if(c<listSize())
        insertNodeafter(temp,x,++c);
    else
        appendNode(temp,x);
    }
}

int listSize()
{
    Node *head, *n;
    int c=0;
    n=head;
    while(n!=NULL)
    {
    n=n->next;
    c++;
    }
    return c;
}

void DisplayLinkedList(Node* head)
{
    Node *rear=NULL;
    if (head==NULL)
        printf("list is empty!\n");
    else
    {
        rear=head;
        while (rear!=NULL)
            printf("%d |---> ", rear->info);
            rear=rear->next;
    }


}

int getNextNode(Node *head)
{
    if (head == NULL)
        return -1;
    else
        return head->next->info;
}

Node* deleteNode(Node *head, int x)
{
    Node *temp;
    if (head == NULL)
        return NULL;

    else
        {
            if (head->info==x)
                {
                temp = head->next;
                free(head);
                head=temp;
                return head;
                }
            else
                {
                deleteNode(head->next,x);
                return head;
                }
        }
}

void main()
{
    int i=0;
    Node *myNode;
    insert(myNode,1);
    DisplayLinkedList(myNode); 

}
#包括
#包括
结构节点{
国际信息;
结构节点*下一步;
}; typedef结构节点;
void addNode(节点*头部,整数x)
{
节点*温度;
温度=malloc(sizeof(temp));
温度->信息=x;
if(head==NULL)//如果这是列表中的第一个节点
{
压头=温度;
temp->next=NULL;
}
否则//如果不是,则将其添加为头部
{
温度->下一步=头部;
压头=温度;
}
}
无效追加节点(节点*头部,整数x)
{
节点*后部=头部;
节点*temp=malloc(sizeof(temp));
温度->信息=x;
temp->next=NULL;
while(后->下一步!=NULL)
后=后->下一步;
后->下一步=温度;
后部=温度;
}
void insertNodeafter(节点*头部,int位置,int x)
{
int i;
节点*之前,*之后=头部;
节点*temp=malloc(sizeof(temp));
温度->信息=x;
对于(i=0;不精确;
之后=之前->下一步;
临时->下一步=之后;
前->下一步=温度;
}
空心插入(节点*头部,整数x)
{
int c=0;
节点*温度;
温度=水头;
if(temp==NULL)
{
addNode(temp,x);
}
其他的
{
while(temp!=NULL)
{
如果(临时->信息下一步);
}
如果(c==0)
addNode(temp,x);
否则,如果(cnext;
C++;
}
返回c;
}
无效显示链接列表(节点*头)
{
节点*rear=NULL;
if(head==NULL)
printf(“列表为空!\n”);
其他的
{
后部=头部;
while(后!=NULL)
printf(“%d |-->”,后->信息);
后=后->下一步;
}
}
int getNextNode(节点*头)
{
if(head==NULL)
返回-1;
其他的
返回头部->下一步->信息;
}
节点*删除节点(节点*头,int x)
{
节点*温度;
if(head==NULL)
返回NULL;
其他的
{
如果(头部->信息==x)
{
温度=头部->下一步;
自由(头);
压头=温度;
回流头;
}
其他的
{
删除节点(head->next,x);
回流头;
}
}
}
void main()
{
int i=0;
Node*myNode;
插入(myNode,1);
显示链接列表(myNode);
}

因为您使用的是
节点*
变量,而不是在函数参数中使用
节点**
变量。因为您使用的是
节点*
,所以在变量
头中所做的更改是该函数的局部更改。如果您想在函数调用之后反映这些更改(您显然想这样做)然后使用
Node**
并在代码中相应地使用它。

如前一张海报所述,最好使用Node**变量来反映函数调用后的更改。如果要使用Node*,则必须将Node*返回到main以从中打印

我使用AddNode、insert和DisplayLinkedList将您的代码缩减为添加1,现在它与下面的代码一起正确显示

您还应该在main中将Node*设置为NULL以初始化空链接列表。请检查DisplayLinkedList函数-while循环中缺少花括号。它只是读取printf行,而不是遍历列表,导致无限循环

最佳实践是在创建此程序时进行调试和测试

#include<stdio.h>
#include<stdlib.h>
#include "stack.h"

void main()
{
    int i=0;
    Node *myNode;
    myNode = NULL;
    insert(&myNode,1);
    DisplayLinkedList(myNode);
}

void addNode(Node **head, int x)
{
    Node *temp;
        temp=malloc(sizeof(temp));
        temp->info=x;
        if (*head==NULL)//if this is the first node in the list
        {
            *head=temp;
            temp->next = NULL;
        }
        else //if not, add it as the head
        {
            temp->next=*head;
            *head=temp;
        }
}

void insert(Node **head, int x)
{
    int c=0;
    Node *temp;
    temp=*head;
    if(temp==NULL)
    {
        addNode(head ,x);
    }
}

void DisplayLinkedList(Node* head)
{
    Node *rear=NULL;
    if (head==NULL)
        printf("list is empty!\n");
    else
    {
        rear=head;
        while (rear!=NULL)
    {
            printf("%d |---> ", rear->info);
            rear=rear->next;
    }
    }
}
#包括
#包括
#包括“stack.h”
void main()
{
int i=0;
Node*myNode;
myNode=NULL;
插入(&myNode,1);
显示链接列表(myNode);
}
void addNode(节点**头部,整数x)
{
节点*温度;
温度=malloc(sizeof(temp));
温度->信息=x;
if(*head==NULL)//如果这是列表中的第一个节点
{
*压头=温度;
temp->next=NULL;
}
否则//如果不是,则将其添加为头部
{
温度->下一步=*头部;
*压头=温度;
}
}
无效插入(节点**头部,int x)
{
int c=0;
节点*温度;
温度=*水头;
if(temp==NULL)
{
addNode(头,x);
}
}
无效显示链接列表(节点*头)
{
节点*rear=NULL;
if(head==NULL)
printf(“列表为空!\n”);
其他的
{
后部=头部;
while(后!=NULL)
{
printf(“%d |-->”,后->信息);
后=后->下一步;
}
}
}

通常,调试器在类似情况下非常有用。main中的myNode应分配新内存。next字段应指向NULL。1)函数可以更改其参数的值,但这些更改不会影响参数的值。此外,
malloc(sizeof(*temp))
星号很重要。你试过调试代码吗?最基本的是,当我们将值从一个函数传递到另一个函数时,如果我们希望实际参数和形式参数包含相同的值,就应该通过引用调用来完成。试着传递节点**,而不是节点*。分配的内存只有在那时才可供调用方使用当然,代码中还有其他几个问题。还有一个替代方法:
Node*addNode(Node*head,intx){…}
,类似于:
head=addNode(head,23);