【发布时间】:2021-02-14 04:50:03
【问题描述】:
在我输入第一个数字后,它显示分段错误,我不知道为什么。我的目标是制作一个链接列表,其中我有一个特定数字的根,并会输入更大的数字,这些数字最终将被添加到链接列表的一部分中。例如,根/第一个数字将是 50。我将添加一个更大的数字 60,它将添加到 lin 列表下方的另一个节点。这个过程会重复,因为我假设输入的数字会越来越大。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node* left;
struct node* right;
}
node;
int main(void){
printf("Put in a number:");
int x = 0;
scanf("%i", x);
node* a = malloc(sizeof(node));
a->num = x;
node* temp = a;
int n = 1;
while(n == 1){
printf("Put in another number:");
int y = 0;
scanf("%i", y);
while(n == 1){
if (temp->num < y){
if (temp->right == NULL){
node* a = malloc(sizeof(node));
temp->right = a;
temp->right->num = y;
break;
}
else{
temp = temp->right;
}
}
printf("Want to stop? Yes(1) or No (1)?");
scanf("%i", n);
}
}
}
【问题讨论】:
标签: c while-loop linked-list segmentation-fault singly-linked-list