gpt4 book ai didi

C 链表函数程序遇到段错误

转载 作者:行者123 更新时间:2023-11-30 20:02:59 25 4
gpt4 key购买 nike

我有一个作业要编写一些处理链表的函数。其功能是:insertFront - 将一个新节点放在列表的前面

insertBack - 将新节点放在列表末尾

print - 打印当前链表

Max - 返回列表中的最大值

Min - 返回列表中的最小值

locInList - 返回列表中的位置编号

我已经完成了我的代码,我认为它会起作用,但我一直遇到段错误。我知道这意味着我正在尝试访问不存在的内存部分。我不明白我做错了什么。

我尝试过分离和注释掉函数,看起来程序运行良好,直到我点击打印函数。打印函数没有按应有的方式打印,我假设段错误发生在我的 max min 和 locInList 函数中。编译器没有错误消息。

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

struct node
{
int data;
struct node *next;
};
typedef struct node node;

node* insertFront(node* head, int d);
node* insertBack(node* head, int d);
void print(node* head);
int max(node* head);
int min(node* head);
int locInList(node* head, int x);

int main()
{
node* head = NULL;

head = insertFront(head, 5);
head = insertFront(head, 4);
head = insertBack(head, 6);
head = insertBack(head, 7);
print(head);

printf("Max: %d\n", max(head));
printf("Min: %d\n", min(head));
printf("locInList 5: %d\n", locInList(head, 5));
printf("locInList 9: %d\n", locInList(head, 9));
return 0;
}

node* insertFront(node* head, int d)
{
node *tmp = NULL;
tmp = malloc(sizeof(node));
tmp->data = d;
tmp->next = head;
head = tmp;


return head;
}

node* insertBack(node* head, int d)
{
node *tmp = malloc(sizeof(node));

tmp->data = d;
tmp->next = NULL;

if(head == NULL) return tmp;

node *end = head;

while(end->next != NULL){
end = end->next;
}
end->next = tmp;


return head;


}

void print(node* head)
{

node *tmp = head;

while(tmp != NULL){
printf("%d ", tmp->data);
tmp = tmp->next;
}
}

int max (node* head)
{
int max = 0;
node *tmp = NULL;

tmp = head;

while(tmp->next != NULL){
if(tmp->data >= max){
max = tmp->data;
tmp = tmp->next;
}
}

return max;
}

int min (node* head)
{
int min = head->data;
node *tmp = NULL;

tmp = head;

while(tmp != NULL){
if(tmp->data <= min){
min = tmp->data;
tmp = tmp->next;
}
}

return min;
}

int locInList(node* head, int x)
{

int i = 0;
node *tmp = NULL;

tmp = head;

while(tmp != NULL){


if(tmp->data == x){
return i;
}else{
i++;
tmp = tmp->next;

}
}
return i;

}

最佳答案

您的代码中有几个错误,但这里有一个很容易开始的错误:

node* insertFront(node* head, int d)
{
node *tmp = NULL;
tmp = malloc(sizeof(node));
tmp->data = d;
tmp->next = head;
return head; // Sure you want to return head ?
// The first time you call this function head is NULL
// and then you return NULL
// How about returning tmp instead
}

现在有一个更复杂的错误:

node* insertBack(node* head, int d)
{
node *tmp = NULL;
node *end = head;
tmp = malloc(sizeof(node)); // Why malloc two new nodes ?
end = malloc(sizeof(node)); // Your code shall only insert one new node
tmp->data = d;
tmp->next = NULL;

while(end->next != NULL){
end = end->next;
end->next = tmp; // Here you destroy the list by inserting
// the new node in incorrect places.
// You don't want to do this until
// the end has been reached
}

return head; // Again ... this is bad when called the first time
}

试试这个:

node* insertBack(node* head, int d)
{
node *tmp = malloc(sizeof(node));
tmp->data = d;
tmp->next = NULL;
if (head == NULL) return tmp;

node *end = head;

while(end->next != NULL){
end = end->next;
}
end->next = tmp;

return head;
}

在函数 maxminlocInList 中,您有一个不必要的 tmp = malloc(sizeof(node)); 这会导致内存泄漏。

关于C 链表函数程序遇到段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56925359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com