文档介绍:实验一 顺序表和单链表的基本操作的实现
一、顺序表实验内容:
编写函数, 通过数组, 建立一个顺序表。
编写函数, 实现对该顺序表的遍历。
编写函数, 在顺序表中进行顺序查找某一元素, 查找成功则返回其存储位置 i, 否则返回错误信); switch(ch)
{case 1:
printf("Pleae enter the data you want to find:\n"); //按值查找scanf("%d",&data);
datafind_list(a,data);break; case 2:
printf("Pleae enter where you want to find:\n"); //按位查找scanf("%d",&place);
printf("The place is :%d\n",place); find_list(a, place);break;
case 3:
printf("Pleae enter where you want to insert:\n"); //插入接点scanf("%d",&place);
printf("input a data what you want to insert"); scanf("%d",&data);
in(a,place,data); print_list(a); break; case 4:
printf("Pleae enter where you want to delet:\n"); //删除接点scanf("%d",&place);
printf("The place is :%d\n",place);
printf("Pleae enter where you want to delet:\n"); scanf("%d",&place);
printf("The place is :%d\n",place); delet(a, place); print_list(a);break;
default: printf("输入错误,请重新选择");break;}} return 0;}
void creat_list(int a[], int n) //建立顺序表
{ int i; int j;
int x;
printf("输入数据:"); scanf("%d",&a[1]);
for (i = 2; i <= n; i++)
{ scanf("%d",&x); for (j = 1; j <= i - 1; j++)
{ if (x == a[j])
{
break;
} }
if (j > i - 1)
{
a[i] = x;
}
else
i--;
}
a[0] = n;}
void datafind_list(int a[],int d) //按值查找元素
{ int i; int j=0;
for(i=1;i<=a[0];i++)
{if(d==a[i])
printf("the data is %d and its place is %d\n",d,i);
else j++;} if (j==a[0])
{
printf("error\n");