链表程序(c语言)出现分段错误



我正在尝试使用c++编写链表代码。由于某种原因,开始插入和结束插入不能正常工作。代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void insertAtBeginning(int );
void insertAtEnd(int );
void printLL();
struct Node
{
int data;
struct Node *link;
};
struct Node *head;

int main()
{
struct Node *temp, *newnode;
int ch=1, i=1, info;
head = NULL;
while(ch)
{
printf("Enter data: ");
scanf("%d", &info);

newnode = (struct Node *)malloc(sizeof(struct Node));

newnode->data = info;
newnode->link = NULL;

if(head == NULL)
{
head = newnode;
temp = newnode;
}
else
{
temp ->link = newnode;
temp = newnode;
}
printf("You wish to continue? (press 0 to terminate)n");
scanf("%d",&ch);
if(!ch)
{
break;
}
}
temp = head;
while(temp!=NULL)
{
printf("%d -> ",temp->data);
temp = temp->link;
}
printf("n");
insertAtBeginning(50);
insertAtEnd(150);
//printLL();

}
void insertAtBeginning(int info)
{
struct Node *newnode;
newnode->data = info;
printf("n%dn", newnode ->data);
newnode->link = head;
head = newnode;
}
void insertAtEnd(int info)
{
struct Node *temp, *newnode;
newnode->link = NULL;
newnode->data = info;
temp = head;
while(temp!=NULL)
{
temp = temp->link;
}
temp->link = newnode;
printf("n%dn", newnode -> data);
}
void printLL()
{
struct Node *temp;
temp = head;
while(temp!=NULL)
{
printf("%d -> ",temp->data);
temp = temp->link;
}
}

问题在函数的newnode->data = info附近。

创建了两个函数,一个用于在开头插入元素,另一个用于在末尾插入元素。在这两个节点中,我都创建了一个新节点。问题是我不能在这些节点中插入数据。

当您想要在链表的末尾添加一个新节点时,您必须找到最后一个节点(其链接为NULL的节点,而不是其本身)。此外,最好使用有意义的变量名(temp是太通用的名称)。您还忘记在insertAtBeginninginsertAtEnd函数中malloc新节点。我已经在以下代码

中修复了这些问题
#include<stdio.h>
#include<stdlib.h>
void insertAtBeginning(int );
void insertAtEnd(int );
void printLL();
struct Node
{
int data;
struct Node *link;
};
struct Node *head = NULL;

int main()
{
struct Node *it, *newnode, *tail;
int ch=1, i=1, info;
while(ch){
printf("Enter data: ");
scanf("%d", &info);
newnode = malloc(sizeof(struct Node));
newnode->data = info;
newnode->link = NULL;
if (head == NULL) {
head = newnode;
tail = newnode;
} else {
tail->link = newnode;
tail = newnode;
}
printf("You wish to continue? (press 0 to terminate, else to continue)n");
scanf("%d",&ch);
if(ch == 0) {
break;
}
}
it = head;
while(it != NULL) {
printf("%d -> ",it->data);
it = it->link;
}
printf("n");
insertAtBeginning(50);
insertAtEnd(150);
}
void insertAtBeginning(int info)
{
struct Node *newnode;
newnode = malloc(sizeof(struct Node));
newnode->data = info;
printf("n%dn", newnode->data);
newnode->link = head;
head = newnode;
}
void insertAtEnd(int info)
{
struct Node *it, *newnode;
newnode = malloc(sizeof(struct Node));
newnode->link = NULL;
newnode->data = info;
it = head;
while(it->link != NULL)
{
it = it->link;
}
it->link = newnode;
printf("n%dn", newnode->data);
}
void printLL()
{
struct Node *it;
it = head;
while(it!=NULL)
{
printf("%d -> ",it->data);
it = it->link;
}
}

您正在处理未初始化的堆栈变量(struct Node *newnode)作为指向struct Node的指针,并试图更新其字段:

struct Node *newnode;
newnode->data = info;

这不起作用,因为newnode的值是事先在堆栈上的任何垃圾,并且尝试def (*newnode)可能会给出一个seg错误,因为您试图从一些未知的内存地址读取。

请注意,在main内部,您如何将malloc的结果赋值给newnode,这意味着您知道(给定malloc没有返回NULL),指针是有效的,并且您可以自由地使用它指向的内存。

最新更新