[英]Segmentation fault issue while reversing linked list in c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *insert(struct node *link, int data) {
if (link == NULL) {
link = (struct node *)malloc(sizeof(struct node));
link->data = data;
link->next = NULL;
} else {
struct node *newlink = (struct node *)malloc(sizeof(struct node));
newlink->data = data;
newlink->next = link;
link = newlink;
}
return link;
}
void reverse(struct node *link) {
int i, j = 0;
int arr1[100], arr2[100];
struct node *current;
int count = 0;
current = link;
while (current != NULL) {
arr1[i] = current->data;
i = i + 1;
count = count + 1;
current = current->next;
}
printf("\n");
i = 0;
j = 0;
for (i = count - 1; i >= 0; i--) {
arr2[j] = arr1[i];
j = j + 1;
}
printf("The elements in the linked list are: ");
for (i = 0; i < count; i++) {
printf("%d ", arr1[i]);
}
printf("The elements in the reversed linked list are: ");
for (j = 0; j < count; i++) {
printf("%d ", arr2[j]);
}
}
void print(struct node *link) {
struct node *temp = link;
printf("The elements in the linked list are: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
void main() {
int value;
printf("Enter the value:\n");
scanf("%d", &value);
struct node *link = NULL;
link = insert(link, value);
char ans[3] = "yes";
while (ans[0] == 'y') {
printf("Do you want to add another node? Type Yes/No\n");
scanf("%s", ans);
if (ans[0] == 'y') {
printf("Enter the value:\n");
scanf("%d", &value);
link = insert(link, value);
} else {
reverse(link);
}
}
}
這是我編寫的用於在C中反轉單個鏈表的代碼。我似乎嘗試使用該程序的不同組合,但是通過數組方法進行處理時,我無法擺脫分段錯誤,因此它沒有給出輸出。
您的代碼中存在一些問題:
在函數reverse
中的while
循環中使用i
時, i
未被初始化,從而導致無法定義的行為,這可以解釋segmentation fault
。
j
在reverse
函數末尾未在循環中修改,從而導致無限循環:
for (j = 0; j < count; i++) { printf("%d ", arr2[j]); }
您無需反轉列表,只需以相反的順序打印列表內容,並假定其長度最大為100
。 這可能不是您期望的。
在函數main
,應將數組ans
增大為至少容納單詞yes
,並且應防止scanf()
向其中存儲更多字符。 還要重新組織代碼以避免重復:
int main(void) { struct node *link = NULL; for (;;) { char ans[80]; int value; printf("Enter the value:\\n"); if (scanf("%d", &value) != 1) break; link = insert(link, value); printf("Do you want to add another node? Type Yes/No\\n"); if (scanf("%79s", ans) != 1 || ans[0] != 'y') { break; } } reverse(link); return 0; }
通過提高編譯器警告級別,可以立即發現上述大多數問題(例如gcc -Wall -Werror
或clang -Weverything -Werror
)。
這是一個更簡單的版本,可以讀取數字並按照與您相同的順序分配列表,將每個新元素插入上一個元素之前,然后反轉列表並最終打印出來。 正如預期的那樣,該列表按輸入順序打印。
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *insert(struct node *head, int data) {
struct node *newlink = malloc(sizeof(*newlink));
newlink->data = data;
newlink->next = head;
return newlink;
}
struct node *reverse(struct node *link) {
struct node *prev = NULL;
while (link) {
struct node *temp = link->next;
link->next = prev;
prev = link;
link = temp;
}
return prev;
}
void print(struct node *link) {
printf("The elements in the linked list are: ");
for (struct node *n = link; n; n = n->next) {
printf("%d ", n->data);
}
printf("\n");
}
int main(void) {
struct node *link = NULL;
int value;
printf("Enter the values, end the list with 0:\n");
while (scanf("%d", &value) == 1 && value != 0) {
link = insert(link, value);
}
link = reverse(link);
print(link);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.