#include<stdio.h>
#define ElemType int
#define MAXSIZE 100
typedef struct {
ElemType elem[MAXSIZE]; /* 线性表占用的数组空间 */
int last;
/* 记录线性表中最后一个元素在数组elem[ ]中的位置
(下标值),空表置为-1 */
} SeqList;
int main () {
void ShowSeqList (SeqList L);
void InitSeqList (SeqList *L) ;
void Insert( ElemType e, SeqList*L,int i);
SeqList L ;//define data定义顺序表
ElemType e;
L.last =-1;
InitSeqList ( &L );//初始化顺序表
int i;
printf ("%d\n", L.elem[i]);//display original data显示原来数据
ShowSeqList(L);
printf ("%d\n", L);
Insert(44,&L,999);
printf("%d\n",L.elem[0]);
return 0;
}
void ShowSeqList (SeqList L) {//traversal data 遍历数据
int i;
for (i=0; i<=L.last; i++) {
printf ("输出|: %d\n",L.elem[i] );
}
}
void InitSeqList (SeqList *L) {
int i;
for ( i=0; i<5; i++) {
L->elem [i]=rand()%20+1;
L-> last ++;
}
ShowSeqList (*L);
}
/*3) 在顺序表L中第i个数据元素之前插入一个元素e,插入后表长n=L->last+1。*/
void Insert( ElemType e, SeqList*L,int i) {
int j;
if(L->last>=MAXSIZE-1) {
printf("表满");
return;
}
/*产表空间已满,不能插入*/
if(i<1||i>L->last+2) {
// printf("位置不合法\n");
return;
}
/*检查插入位置的合法性*/
for(j=L->last; j>=i-1; j--) {
L->elem[j+1]=L->elem[j];/*将 a~ an倒序向后移动*/
L->elem[i-1]=e;
L->last++;
return;
}
}