要检查示例列表实施,我尝试了以下代码。但是每当我试图显示结果时,它就会进入一个循环。我找不到哪里出错了。
#include<stdio.h>
#include<stdlib.h>
typedef struct linkedlist
{
int data;
struct linkedlist *next;
}node;
int main()
{
int ch,num;
node *head=NULL;
head=(node *)malloc(sizeof(node));
node *new=NULL;
new=(node *)malloc(sizeof(node));
node *temp=NULL;
temp=(node *)malloc(sizeof(node));
printf("\n1.Insert to list");
printf("\n3.Display the list");
printf("\n Enter Choice->");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter data->");
scanf("%d",&num);
new->data=num;
new->next=NULL;
head->next=new;
break;
case 3: temp=head;
while(temp!=NULL)
{
printf("\n %d",temp->data);
temp=temp->next;
}
break;
default:printf("Wrong Choice");
break;
}
return 0;
}