程序如下:
# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>
struct list
{
int data;
struct list *next;
};
void print(struct list *head);
void insert(struct list *head);
void print(struct list *head)//打印链表
{
struct list *p;
p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
void insert(struct list *head)//插入链表
{
struct list *pnew,*p;
int m;
char f,f1;
p=head;
aa:printf("Enter the number of you want:\n");
scanf("%d",&m);
pnew=(struct list*)malloc(sizeof(struct list));
if (pnew==NULL)
{
printf("NO MEMORY!\n");
return ;
}
pnew->data=m;
pnew->next=NULL;
for (p=head;p!=NULL;p=p->next)
if (p->next==NULL||p->next->data>pnew->data)
{
pnew->next=p->next;
p->next=pnew;
break;
}
while (1)
{
printf("是否继续操作(y/n):");
f=getchar();
if((f=='y')||(f=='Y')||(f=='N')||(f=='n'))
{
if((f=='y')||(f=='Y'))
{
printf("操作继续.\n");
goto aa;
}
else
{
printf("操作结束。\n");
break;
}
}
else
{
printf("输入错误。\n");
}
}
while (1)
{
printf("是否继续打印操作(y/n):");
f1=getchar();
if((f1=='y')||(f1=='Y')||(f1=='N')||(f1=='n'))
{
if((f1=='y')||(f1=='Y'))
{
printf("操作继续.\n");
print(head->next);
}
else
{
printf("操作结束。\n");
break;
}
}
else
{
printf("输入错误。\n");
}
}
}
void main()
{
struct list *head,*p,*rear;
int x,n,t=1;
printf("*********主菜单*********\n");
printf("*********1.插入链表*******\n");
printf("*********2.打印链表*******\n");
head=(struct list*)malloc(sizeof(struct list));
if (head==NULL)
{
printf("NO MEMORY!\n");
exit(-1);
}
head->next=NULL;
rear=head;
printf("Please input the date:\n");
while(t)
{
scanf("%d",&x);
p=(struct list*)malloc(sizeof(struct list));
if (p==NULL)
{
printf("NO MEMORY!\n");
exit(-1);
}
p->data=x;
p->next=NULL;
rear->next=p;
rear=p;
}
printf("请输入需要操作的序号:\n");
scanf("%d",&n);
switch(n)
{
case 1:void insert(struct list *head);break;
case 2:void print(struct list *head);
default:printf("输入错误!\n");break;
}
}
首先程序会提示:
*********主菜单***********
*********1.插入链表*******
*********2.打印链表*******
Please input the date:
然后我输入:1 2 3 4 5 6时
程序应该提示
请输入需要操作的序号:
可是我这个到这就没有了.我想请问这是为什么呀?
我对链表一直不懂!