c语言中倒链表时的分段错误问题 #包括 #包括 结构节点{ int数据; 结构节点*下一步; }; 结构节点*插入(结构节点*链接,整数数据){ if(link==NULL){ link=(结构节点*)malloc(sizeof(结构节点)); 链接->数据=数据; 链接->下一步=空; }否则{ 结构节点*newlink=(结构节点*)malloc(sizeof(结构节点)); 新建链接->数据=数据; 新建链接->下一步=链接; link=newlink; } 返回链接; } 无效反向(结构节点*链接){ int i,j=0; int arr1[100],arr2[100]; 结构节点*当前; 整数计数=0; 电流=链路; while(当前!=NULL){ arr1[i]=当前->数据; i=i+1; 计数=计数+1; 当前=当前->下一步; } printf(“\n”); i=0; j=0; 对于(i=count-1;i>=0;i--){ arr2[j]=arr1[i]; j=j+1; } printf(“链表中的元素为:”); 对于(i=0;i下一步; } } void main(){ int值; printf(“输入值:\n”); scanf(“%d”,和值); 结构节点*link=NULL; 链接=插入(链接,值); char ans[3]=“是”; while(ans[0]=“y”){ printf(“是否要添加其他节点?键入是/否\n”); scanf(“%s”,ans); 如果(ans[0]==“y”){ printf(“输入值:\n”); scanf(“%d”,和值); 链接=插入(链接,值); }否则{ 反向(链接); } } }

c语言中倒链表时的分段错误问题 #包括 #包括 结构节点{ int数据; 结构节点*下一步; }; 结构节点*插入(结构节点*链接,整数数据){ if(link==NULL){ link=(结构节点*)malloc(sizeof(结构节点)); 链接->数据=数据; 链接->下一步=空; }否则{ 结构节点*newlink=(结构节点*)malloc(sizeof(结构节点)); 新建链接->数据=数据; 新建链接->下一步=链接; link=newlink; } 返回链接; } 无效反向(结构节点*链接){ int i,j=0; int arr1[100],arr2[100]; 结构节点*当前; 整数计数=0; 电流=链路; while(当前!=NULL){ arr1[i]=当前->数据; i=i+1; 计数=计数+1; 当前=当前->下一步; } printf(“\n”); i=0; j=0; 对于(i=count-1;i>=0;i--){ arr2[j]=arr1[i]; j=j+1; } printf(“链表中的元素为:”); 对于(i=0;i下一步; } } void main(){ int值; printf(“输入值:\n”); scanf(“%d”,和值); 结构节点*link=NULL; 链接=插入(链接,值); char ans[3]=“是”; while(ans[0]=“y”){ printf(“是否要添加其他节点?键入是/否\n”); scanf(“%s”,ans); 如果(ans[0]==“y”){ printf(“输入值:\n”); scanf(“%d”,和值); 链接=插入(链接,值); }否则{ 反向(链接); } } },c,algorithm,linked-list,C,Algorithm,Linked List,这是我用C写的反转单链表的代码。我似乎尝试了程序的不同组合,但在使用数组方法执行时,我无法消除分段错误,因此它不会给出输出。您的代码中有一些问题: i在中使用时未初始化,而在函数反向中循环,导致未定义的行为,这可能解释分段故障 j在reverse函数末尾的循环中未被修改,导致无限循环: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; };

这是我用C写的反转单链表的代码。我似乎尝试了程序的不同组合,但在使用数组方法执行时,我无法消除分段错误,因此它不会给出输出。

您的代码中有一些问题:

  • i
    中使用时未初始化,而
    在函数
    反向
    中循环,导致未定义的行为,这可能解释
    分段故障

  • j
    reverse
    函数末尾的循环中未被修改,导致无限循环:

    #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);
            }
        }
    }
    
通过提高编译器警告级别(例如
gcc-Wall-Werror
clang-Weverything-Werror
),可以立即发现上述大多数问题

下面是一个更简单的版本,它读取数字并按照与您相同的顺序分配列表,在前一个元素之前插入每个新元素,然后反转列表并最终打印它。正如预期的那样,列表按条目顺序打印

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;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*插入(结构节点*头,整数数据){
结构节点*newlink=malloc(sizeof(*newlink));
新建链接->数据=数据;
新建链接->下一步=头部;
返回newlink;
}
结构节点*反向(结构节点*链接){
结构节点*prev=NULL;
while(link){
结构节点*temp=link->next;
链接->下一步=上一步;
prev=链接;
链接=温度;
}
返回上一个;
}
无效打印(结构节点*链接){
printf(“链表中的元素为:”);
对于(结构节点*n=link;n;n=n->next){
printf(“%d”,n->data);
}
printf(“\n”);
}
内部主(空){
结构节点*link=NULL;
int值;
printf(“输入值,以0结尾:\n”);
而(scanf(“%d”,&value)==1&&value!=0){
链接=插入(链接,值);
}
链路=反向(链路);
打印(链接);
返回0;
}

能否共享堆芯转储的堆栈跟踪
arr1[i]=current->data使用统一化变量
i
。随后的
i=0
太晚了,甚至没有用,因为紧接着的循环是(i=count-1;…)
@tron042它可能不会生成堆栈跟踪,但现代编译器非常擅长发出警告。如果您正在评估编译器,请从您知道有效的代码开始。如果你有错误的代码,那么就使用一个你需要的编译器。简单。但是如果你在同一练习中混合了两个目标,那就很复杂了。
for(j=0;j
:当你使用
j
时,增加
i
可能是一个错误。
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;
}
#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;
}