我正在尝试根据节点数在我的双链表中插入元素n
。就像 if n
is 4
then 输入的元素数是:34 45 32 1
但得到segmentation fault
. 谁能告诉我哪里出错了?
#include<stdio.h>
#include<malloc.h>
struct node{
struct node *prev;
struct node *next;
int info;
}*start;
create_list(int num)
{
printf("Hi I entered!\n");
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
if(start==NULL)
{
printf("Hi I am null!\n");
tmp->prev=NULL;
start->prev=tmp;
start=tmp;
}
else
{
printf("Hi I am no more null!\n");
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}
int main(){
int choice, n, elem,i;
start = NULL;
printf("Enter your choice of number: \n");
scanf("%d", &choice);
while(1)
{
switch(choice)
{
case 1:
printf("Enter the number of nodes: \n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter the elemnts: \n");
scanf("%d", &elem);
create_list(elem);
}
break;
default:
printf("You have tyoed wrong!\n");
}
}
}