#include <stdio.h>
#define MAXSIZE 100
typedef int elemtype;
typedef struct
{ elemtype elem[MAXSIZE];
int len;
}SqList;
int init_Sq(SqList &L)
{ L.len=0; }
int length_Sq( SqList L )
{ return L.len; }
int create_Sq( SqList &L)
{ printf("请输入元素个数:");
scanf("%d",&L.len);
printf("请输入各元素的值:");
for (int i=0; i<L.len; i++){
scanf("%d",&L.elem[i]); }
}
int get_Sq(SqList L,int i)
{ return L.elem[i] ; }
int disp_Sq(SqList L)
{ int i;
for (i=0; i<L.len; i++){
printf("%3d",L.elem[i]);}
printf("\n"); }
int search_Sq(SqList L, int key)
{ int i;
for (i=0; i< L.len; i++)
if (L.elem[i]==key ) return i;
return -1; }
int delete_Sq(SqList &L, int i)
{ int j;
for (j=i; j<L.len; j++ )
L.elem[j-1]=L.elem[j];
L.len--; }
int insert_Sq(SqList L,int i,int x)
{ int j;
for (j=L.len-1; j>=i+1; j--)
L.elem[j+1]=L.elem[j];
L.elem[i+1]=x;
L.len++;
}
int pre_Sq(SqList &L,int i)
{
if(i>L.len||i<1) printf("error");
i=L.elem[i-1];
L.len--;
}
int next_Sq(SqList &L,int i)
{
if(i>L.len+1||i<1) printf("error");
i=L.elem[i+1];
L.len++;
}
int main()
{ int k,x;
SqList L;
init_Sq(L);
create_Sq(L);
printf("建立的顺序表为:\n ");
disp_Sq(L);
printf("表的长度为=%d\n",length_Sq(L));
printf("请输入结点的序号: ");
scanf("%d",&k);
printf("结点的值为:%d\n", get_Sq(L,k) );
printf("结点的前驱为:%d\n", pre_Sq(L,k) );
printf("结点的后继为:%d\n\n ", next_Sq(L,k) );
printf(" \n请输入需删除的结点的序号: ");
scanf("%d",&k);
delete_Sq(L,k);
printf("删除结点后的顺序表为:\n ");
disp_Sq(L);
printf(" \n请输入需插入结点的位置与值: ");
scanf("%d %d",&k,&x);
insert_Sq(L,k,x);
printf("插入结点后的顺序表为:\n ");
disp_Sq(L);
return 0;
}