静态线性表 练习作业
#include <stdio.h>#include <stdlib.h>
#define M 1000
typedef struct
{
int data;
int youbiao;
}jie,biao;
void chushihua(biao x); //初始化静态表
void shuru(biao x); //输入数据
void shuchu(biao x); //遍历输出
int weizhi(biao x); //要插入的位置
void charu(biao x,int i,int n); //插入数据
int changdu(biao x); //静态表的长度
void shanchu(biao x,int i); //删除指定的成员
void beiyong(biao x,int i); //把删除的成员连接到备用链表里
void main()
{
jie j;
int i,n,x;
chushihua(j);
shuru(j);
shuchu(j);
printf("请输入要插入的位置:");
scanf("%d",&i);
printf("请输入要插入的数据:");
scanf("%d",&n);
charu(j,i,n);
shuchu(j);
printf("请输入要删除第几个成员:");
scanf("%d",&x);
shanchu(j,x);
shuchu(j);
}
void chushihua(biao x) //初始化静态表
{
int i;
for(i=0; i<M-1; i++)
{
x.youbiao =i+1;
}
x.youbiao =0;
}
void shuru(biao x) //输入数据
{
int i,n;
printf("请问你要插入几个数据\n");
scanf("%d",&n);
x.youbiao =1;
for(i=1; i<=n; i++)
{
printf("第%d个数据是:",i);
scanf("%d",&x.data );
}
x.youbiao =i+1;
}
void shuchu(biao x) //遍历输出
{
int i=1,xiabiao;
printf("\n");
xiabiao=M-1;
while(i != x.youbiao-1)
{
xiabiao=x.youbiao;
printf("第%d个数据是:%d\n",i,x.data );
i++;
}
printf("\n");
}
void charu(biao x,int i,int n) //插入数据
{
int j,wei,tou;
tou=M-1;
if(i<1 || i>changdu(x)+1)
{
printf("要插入的数据不在范围内\n\n");
exit(-1);
}
wei=weizhi(x);
x.data =n;
for(j=1; j<=i-1; j++)
{
tou=x.youbiao ;
}
x.youbiao = x.youbiao;
x.youbiao = wei;
printf("插入数据成功\n\n");
}
int weizhi(biao x) //要插入的位置
{
int i;
i=x.youbiao ;
if(x.youbiao)
x.youbiao =x.youbiao ;
return i;
}
int changdu(biao x) //静态表的长度
{
int i=1,xiabiao=x.youbiao;
if(x.youbiao ==0)
{
return 0;
}
else
{
while(i != x.youbiao-1)
{
i++;
xiabiao=x.youbiao;
}
return i-1;
}
}
void shanchu(biao x,int i) //删除指定的成员
{
int tou,j,h;
if(i<1 || i>changdu(x))
{
printf("要删除的数据不在范围内\n\n");
exit(-1);
}
tou=M-1;
for(j=1; j<=i-1; j++)
{
tou=x.youbiao ;
}
h=x.youbiao ;
x.youbiao =x.youbiao;
beiyong(x,h);
}
void beiyong(biao x,int i) //把删除的成员连接到备用链表里
{
x.youbiao = x.youbiao ;
x.youbiao =i;
}
我只是路过打酱油的。向你学习 感恩无私的分享与奉献 :)
页:
[1]