#include<stdio.h>
#include<stdlib.h>
int Menu(void);
struct link *Num1();
struct link *Num2(struct link *head);
struct link
{
int data;
struct link *next;
}*link;
int main()
{
int choice;
struct link *head;
while(1)
{
choice=Menu();
switch(choice)
{
case 1:head=Num1();break;
case 2:Num2(head);break;
/*case 3:Num3();break;
case 4:Num4();break;
case 5:Num5();break;*/
case 6:
printf("End of program!\n");
exit(0);
default:
printf("Input error!!!\n");
break;
}
}
getchar();
return 0;
}
int Menu()
{
int choice;
printf("1、任务一\n");
printf("2、任务二\n");
printf("3、任务三\n");
printf("4、任务四\n");
printf("5、任务五\n");
printf("6、退出程序\n");
printf("Please input your choice:");
scanf("%d",&choice);
return choice;
}
struct link *Num1()
{
struct link *head=NULL;
head = (struct link*)malloc(sizeof(struct link));
head->next = NULL;
if(head==NULL)
{
printf("创建空链表失败\n");
exit(1);
}
else
printf("创建空链表成功\n");
return head;
}
struct link *Num2(struct link *head)
{
struct link *temp, *p;
p = head;
if(p==NULL)
{
printf("链表不存在!\n");
exit(1);
}
else
{
while (1)
{
temp = (struct link*)malloc(sizeof(struct link));
if(temp==NULL)
{
printf("内存分配失败!\n");
exit(1);
}
printf("输入一个数(-1退出): ");
scanf("%d",&temp->data);
if (temp->data== -1)
break;
p->next = temp;
temp->next = NULL;
p = temp;
}
p->next = NULL;
}
for (p=head->next; head; p=p->next)
{
printf("%d ", p->data);
}
printf("\n");
return head;
}
基本实现了,还存在一些问题,自己参考吧 |