=NULL)
{j++;
p=p->next;
}
if(p==NULL)//未找到第i-1个结点
return0;
else//找到第i-1个结点*p
{s=(link*)malloc(sizeof(link));//创建新结点*s
s->data=pos;
s->next=p->next;//将*s插入到*p之后
p->next=s;
return1;
}
}
intGetElem(link*top,inti)
{intj=0;
link*p;
p=top;
while(p->next&&j
{
p=p->next;
j++;
}
if(i==j)
printf("该位置的元素为%d",p->data);
returnOK;
}
//----------主菜单和二级菜单----------//
intmenu_select_list()
{intchoice;
printf("*****************************************\n");
printf("*1------------------顺序表*\n");
printf("*2------------------链表*\n");
printf("*3------------------退出*\n");
printf("*****************************************\n");
printf("请输入选择:
(1-3)");
scanf("%d",&choice);
if(choice<1||choice>3)
printf("输入选择有误,请重新选择(1-3):
\n");
returnchoice;
}
intmenu_select_2_sqlist()
{intchoice;
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\t*1----------建立顺序表*\n");
printf("\t*2----------显示*\n");
printf("\t*3----------插入*\n");
printf("\t*4----------删除*\n");
printf("\t*5----------查找*\n");
printf("\t*6----------求线性表的长度*\n");
printf("\t*7----------返回*\n");
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("请输入选择(1-7):
");
scanf("%d",&choice);
if(choice<1||choice>7)
printf("输入选择有误,请重新选择(1-7):
\n");
returnchoice;
}
intmenu_select_2_list()
{intchoice;
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\t*1----------前插建立链表*\n");
printf("\t*2----------显示*\n");
printf("\t*3----------后插建立链表*\n");
printf("\t*4----------插入*\n");
printf("\t*5----------删除*\n");
printf("\t*6----------查找*\n");
printf("\t*7----------求线性表的长度*\n");
printf("\t*8----------返回*\n");
printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("请输入选择(1-8):
");
scanf("%d",&choice);
if(choice<1||choice>8)
printf("输入选择有误,请重新选择(1-8):
\n");
returnchoice;
}
//----调用顺序表中的函数----//
voidmain()
{SqListL;
inti,e;
linktop,p;
intpos;
while
(1)
{
switch(menu_select_list())
{
while
(1){
case1:
switch(menu_select_2_sqlist())
{case1:
InitList_Sq(&L);system("cls");
break;
case2:
printlist_sq(L);getch();system("cls");
break;
case3:
printf("请输入要插入的位置:
");
scanf("%d",&i);
printf("请输入要插入的元素:
");
scanf("%d",&e);
ListInsert_Sq(&L,i,e);
printf("插入后的线性表:
");
printlist_sq(L);
getch();
system("cls");
break;
case4:
printf("请输入被删除的位置:
");
scanf("%d",&i);
ListDelete_Sq(&L,i,e);
printf("已成功删除!
");
getch();
system("cls");
break;
case5:
GetElem_Sq(&L,i,&e);getch();system("cls");
break;
case6:
ListLength_Sq(&L);
getch();
system("cls");
break;
case7:
return0;break;system("cls");
default:
printf("\t\t\t输入有误,请重新输入:
\n");break;
}
}break;
//----调用链表中的函数----//
while
(1)
{
case2:
switch(menu_select_2_list())
{
case1:
add_top(&top);
printf("建立好的链表为:
");
arrive_end(&top);
getch();
system("cls");
break;
case2:
printf("建立好的链表为:
");
arrive_end(&top);
getch();
system("cls");break;
case3:
add_bottom(&top);
arrive_end(&top);
getch();
system("cls");break;
case4:
printf("请输入插入位置:
");
scanf("%d",&i);
printf("请输入要插入的元素:
");
scanf("%d",&pos);
insert(&top,i,pos);
arrive_end(&top);getch();
system("cls");break;
case5:
printf("请输入删除位置:
");
scanf("%d",&pos);
delete(&top,pos);
printf("删除后的链表为:
");
arrive_end(&top);
getch();
system("cls");break;
case6:
printf("请输入查找位置:
");
scanf("%d",&i);
GetElem(&top,i);
getch();
system("cls");break;case7:
pos=length(&top);
printf("链表长度为:
%d\n",pos);
getch();
system("cls");break;
case8:
return0;break;
default:
;break;
}
}
default:
;
}break;
system("pause");
return0;
}
}
四:
实验内容截图
实验主菜单:
一、顺序表:
1、顺序表的建立:
3、顺序表的显示:
4:
插入操作:
5、删除操作:
6、查找操作:
7、求线性表长度:
二、链表:
1、前插建立链表:
2、显示链表:
3、前插链表插入操作:
4、前插链表删除操作:
5、前插链表查找操作:
6、求链表长度:
7、后插建立链表的建立:
8、后插链表的显示:
9、后插链表的插入操作:
10、后插链表的删除操作:
11、后插链表的查找操作:
12、后插链表的长度:
五、实验项目分工
王星:
定义链表中的函数和调用链表函数。
郭坤铭:
定义顺序表中的函数和调用顺序表函数。
张磊:
进行宏定义和结构体的定义;设计编写主菜单和二级菜单。
六、实验总结
我组将本次实验实验分成了三大部分,三个人分别负责一部分,由于初次接触数据结构实验,对于实验所用知识不够了解和编写不熟悉,经过三人的商讨和书本知识的巩固学习,慢慢的将实验的每一部分编写了出来,在此过程中遇到许多疑惑和困难,经过不断研究和摸索调试,实验项目最终得以完成。
因此,在今后的学习中应认真学习课本知识,不断强化已学内容,要练习,熟悉算法,逐步提高编程能力。