我有一个分配来编写一些与链接列表有关的功能。功能是:插入边 - 在列表的前面放一个新节点
insertback-在列表的末尾放一个新节点
打印 - 打印当前链接列表
最大 - 列表中的最大值
min-列表中的最小值
locinlist-返回列表中的位置编号
我已经完成了我的代码,我认为它可以正常工作,但我一直遇到细分错误。我知道这意味着我正在尝试访问不存在的部分内存。我无法弄清楚我在做什么错。
我已经尝试分开和评论功能,似乎程序运行良好,直到我按打印功能为止。打印功能并未按应有的打印,我假设分割故障发生在我的最大最小和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: %dn", max(head));
printf("Min: %dn", min(head));
printf("locInList 5: %dn", locInList(head, 5));
printf("locInList 9: %dn", 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;
}
和函数max
,min
和locInList
中有一个不必要的tmp = malloc(sizeof(node));
,会导致内存泄漏。
node *tmp = NULL;
tmp = malloc(sizeof(node));
tmp = head;
此代码首先将tmp
设置为NULL
,然后将其设置为指向新分配的节点,然后将其设置为指向head
。我不知道您要做什么,但是将相同的变量设置为连续三个不同的值不可能。
while(tmp->next != NULL){
if(tmp->data > max){
max = tmp->data;
tmp = tmp->next;
}
}
此代码仅tmp = tmp->next;
是tmp->data > max
。那是不对的。一旦遇到小于任何以前元素的元素,您将永远重复外部while
循环。
node *tmp = NULL;
node *end = head;
tmp = malloc(sizeof(node));
end = malloc(sizeof(node));
此代码将tmp
设置为NULL
,但是两行之后会改变其思维,并将其设置为新分配的节点。它将end
设置为等于head
,但后来两行改变了思想,并将其设置为新分配的节点。为什么要将某些东西设置为一个值,只是立即将其设置为另一个值?为什么在一个函数中分配两个节点,该节点将一个节点添加到链接列表中?
while(end->next != NULL){
end = end->next;
end->next = tmp;
}
您想将节点附加到列表的末尾。但是您将其附加每个您从一个节点移到另一个节点!