Posted on 2012-03-01 00:53
hoshelly 阅读(220)
评论(0) 编辑 收藏 引用 所属分类:
DS && Algorithm
//静态顺序表的各种操作
#include<stdio.h>
#define MaxSize 10
void insertElem(int Sqlist[],int *len,int i,int x)//表首地址、表长、插入元素位置、待插入的元素值
{
int t,temp;
if(*len==MaxSize||i<1||i>*len+1)
{
printf("This insert is illegal\n");
return;
}
for(t=*len-1;t>=i-1;t--)
Sqlist[t+1]=Sqlist[t];//i-1后的元素都向后移一位
Sqlist[i-1]=x;//插入元素
*len=*len+1;//表长加1
}
void DelElem(int Sqlist[],int *len,int i)//向顺序表中删除元素
{
int j;
if(i<1||i>*len)
{
printf("This insert is illegal");
return;
}
for(j=i;j<=*len-1;j++)
Sqlist[j-1]=Sqlist[j];//将第i个元素之后的元素前移,覆盖即删除
*len=*len-1;
}
int main()
{
int Sqlist[MaxSize];
int len;
int i;
for(i=0;i<6;i++)
scanf("%d",&Sqlist[i]);
len=6;
for(i=0;i<len;i++)
printf("%d ",Sqlist[i]);
printf("\nThe spare length is %d\n",MaxSize-len);//显示表中剩余空间
insertElem(Sqlist,&len,3,0);//在表中第3个位置插入整数0
for(i=0;i<len;i++)
printf("%d ",Sqlist[i]);
printf("\nThe spare length is %d\n",MaxSize-len);
insertElem(Sqlist,&len,11,0);
DelElem(Sqlist,&len,6);
for(i=0;i<len;i++)
printf("%d ",Sqlist[i]);
printf("\nThe spare length is %d\n",MaxSize-len);
return 0;
}