#include<stdio.h>
#include<stdlib.h>
struct node {
char str[200];
int nn;
struct node* next;
};
int number;
struct node* start=NULL;
struct node* current;
//function to insert into the list
void insert() {
struct node* n;
n=(struct node*)malloc(sizeof(struct node));
n->str=malloc(sizeof(char) * 1000);
printf("please enter the data that you would like to insert: ");
gets(n->str);
printf("asdasdasdasd");
n->next=NULL;
if( start==NULL ) {
start->next=n;
current=n;
}
else {
current->next=n;
current=n;
}
printf("done\n");
}
void display() {
current=start;
int i=0;
while( current->next!=NULL ) {
printf("node%d= %s\n",++i,current->str);
current=current->next;
}
printf("this the end");
}
int main() {
char c;
int input;
do {
printf("Select from the following options:\n"
"1.Display list\n"
"2.Add to list\n"
"3.delete from list\n");
scanf("%d",&input);
switch (input) {
case 1: display(); break;
case 2: insert(); break;
// case 3: delete(); break;
default : printf("Please select 1 , 2 or 3\n");
}
printf( "would you like to continue?(y/n)\n");
scanf("%s",&c);
} while(c=='y');
return 0;
}
这在插入函数中给了我一个错误,A SEGMENTATION FAULT!
我尝试了一些东西,但我只是没有得到清晰的画面。我对指针有点弱,实际上很困惑!
请告诉我我做错了什么来帮助我。忘记我的链表逻辑,让它错了。我只想知道为什么会发生分段错误!