线性表顺序存储结构代码记录
#include<stdio.h>
#define maxsize 100
typedef int Elemtype;
typedef struct{
Elemtype date[maxsize];
int length;
}Seqlist;
Elemtype GetSeqlist(Seqlist * L,int n)
{
if(L->length)
if(n<=L->length&&n>0)
return L->date[n-1];
else return -1;
}
int ElemDelete(Seqlist * L,int n)
{
int i;
if(L->length)
if(n<=L->length&&n>0)
{
for(i=n-1;i<L->length;i++)
L->date[i]=L->date[i+1];
L->length--;
return 1;
}
else return 0;
}
int ElemInsert(Seqlist * L,int n,Elemtype m)
{
int i;
if(!L->length)
return 0;
if(n<=L->length+1&&n>0)
{
if(n<=L->length&&n>0)
{
for(i=L->length;i>=n;i--)
L->date[i]=L->date[i-1];
L->date[n-1]=m;
L->length++;
return 1;
}
else if(n==L->length+1);
{
L->date[n-1]=m;
L->length++;
return 1;
}
}
else return 0;
}
void main()
{
Seqlist L;
int i,m;
L.length=10;
for(i=0;i<L.length;i++)
L.date[i]=i;
for(i=0;i<L.length;i++)
printf("%d ",L.date[i]);
printf("\n");
m=GetSeqlist(&L,2);
printf("%d\n",m);
ElemDelete(&L,2);
for(i=0;i<L.length;i++)
printf("%d ",L.date[i]);
printf("\n");
if(ElemInsert(&L,2,77) )
for(i=0;i<L.length;i++)
printf("%d ",L.date[i]);
printf("\n");
}