添加到单链表的末尾 Segmentation fault C
Add to end of a single linked list Segmentation fault C
我正在尝试制作一个 c 应用程序,它在单个链表的末尾添加元素,但在读取最后一个元素后出现分段错误。
我使用函数addAtEndSLL()
在末尾添加一个元素。
//Program to add elements at the end of a single linked list
#include <stdio.h>
#include <stdlib.h>
//Basic declaration of a SLL
struct singleList{
int data;
struct singleList *next;
};
//Add an element at the end of a SLL
int addAtEndSLL(struct singleList **startPtr, int value){
struct singleList *newNode;
newNode = (struct singleList *)malloc(sizeof(struct singleList));
if(newNode == NULL){
printf("\nFailed to Allocate Memory");
return;
}
newNode->data = value;
newNode->next = NULL;
if(*startPtr == NULL){
*startPtr = newNode;
} else {
struct singleList *temp = NULL;
temp = *startPtr;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
}
int main() {
int i, value;
struct singleList *first = NULL, *tempS = NULL;
tempS = first;
for(i = 1; i <= 5; i++){
printf("\nEnter the data:");
scanf("%d", &value);
addAtEndSLL(&first, value);
}
/*****This is where I belive the segfault occurs*****/
while(tempS->next != NULL){
printf("%d", tempS->data);
tempS = tempS->next;
}
return 0;
}
任何帮助将不胜感激。
首先,修复警告:将函数 void
替换为 int
,并删除最后一个 return
,现在不再需要了。
接下来,针对您代码中的真正问题:当您在 addAtEndSLL
函数中设置 start
时,first
的值保持不变,因为 C 按值传递参数;其中包括指针。
要解决此问题,请更改函数以接受指向指针的指针(即双星号),向其传递 &first
而不是 first
,并向里面的参数 addAtEndSLL
:
void addAtEndSLL(struct singleList **startPtr, int value) {
...
// Change all uses of start with *startPtr, like this:
if(*startPtr == NULL){
*startPtr = newNode;
}
...
}
我正在尝试制作一个 c 应用程序,它在单个链表的末尾添加元素,但在读取最后一个元素后出现分段错误。
我使用函数addAtEndSLL()
在末尾添加一个元素。
//Program to add elements at the end of a single linked list
#include <stdio.h>
#include <stdlib.h>
//Basic declaration of a SLL
struct singleList{
int data;
struct singleList *next;
};
//Add an element at the end of a SLL
int addAtEndSLL(struct singleList **startPtr, int value){
struct singleList *newNode;
newNode = (struct singleList *)malloc(sizeof(struct singleList));
if(newNode == NULL){
printf("\nFailed to Allocate Memory");
return;
}
newNode->data = value;
newNode->next = NULL;
if(*startPtr == NULL){
*startPtr = newNode;
} else {
struct singleList *temp = NULL;
temp = *startPtr;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
}
int main() {
int i, value;
struct singleList *first = NULL, *tempS = NULL;
tempS = first;
for(i = 1; i <= 5; i++){
printf("\nEnter the data:");
scanf("%d", &value);
addAtEndSLL(&first, value);
}
/*****This is where I belive the segfault occurs*****/
while(tempS->next != NULL){
printf("%d", tempS->data);
tempS = tempS->next;
}
return 0;
}
任何帮助将不胜感激。
首先,修复警告:将函数 void
替换为 int
,并删除最后一个 return
,现在不再需要了。
接下来,针对您代码中的真正问题:当您在 addAtEndSLL
函数中设置 start
时,first
的值保持不变,因为 C 按值传递参数;其中包括指针。
要解决此问题,请更改函数以接受指向指针的指针(即双星号),向其传递 &first
而不是 first
,并向里面的参数 addAtEndSLL
:
void addAtEndSLL(struct singleList **startPtr, int value) {
...
// Change all uses of start with *startPtr, like this:
if(*startPtr == NULL){
*startPtr = newNode;
}
...
}