gpt4 book ai didi

c - 在c中反转链表时出现段错误问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:39:51 26 4
gpt4 key购买 nike

#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 中编写的用于反转单个链表的代码。我似乎尝试了程序的不同组合,但是在通过数组方法执行时,我无法摆脱段错误,因此它没有'给出输出。

最佳答案

您的代码中存在一些问题:

  • i 在函数 reversewhile 循环中使用时未初始化,导致未定义的行为可以解释 段错误

  • jreverse函数最后的循环中没有修改,导致死循环:

    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 -Werrorclang -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;
}

关于c - 在c中反转链表时出现段错误问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44989256/

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