|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
菜单第1项完成:创建一个仅含头结点的空链表,如果创建成功,输出创建空链表成功,返回头结点的指针,否则输出创建空链表失败,退出程序运行。
菜单第2项完成:依次输入一组整数,以-1作为结束的标志,创建由这些输入数据所形成的带头结点的单链表,输出链表的数据信息,返回头结点的指针。
若输入:45,45, 23,45,54,25, -1。则生成链表:
#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;
}*b;
void main()
{
int choice;
struct link *head1;
while(1)
{
choice=Menu();
switch(choice)
{
case 1:head1=Num1();break;
case 2:Num2(head1);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");
}
}
}
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;
b = (struct link *)malloc(sizeof(struct link));
head=b;
if(b==NULL)
{
printf("创建空链表失败\n");
exit(1);
}
else
printf("创建空链表成功\n");
free(b);
return head;
}
struct link *Num2(struct link *head)
{
struct link *temp;
printf("输入一组数:");
scanf("%d",b->data);
if(head!=NULL)
{
temp=head;
head=b;
b->next=temp;
}
else
{
head=b;
b->next=NULL;
}
return head;
}
菜单第一项创建一个空链表都不确定对不对,希望能帮我看看,再帮我写一下菜单二,能对着我的代码更改就最好了嘿嘿,对链表比较陌生,谢谢谢谢。
|
|