顺序表::在顺序表L中的第i个位置插入元素e。

时间:2025-03-14 15:21:17
#include<>

#include<>

#define LISTINITSIZE 20

#define INCREAMENT 5

typedef int ElemType;

typedef struct {

ElemType *elem;

int length;

int listsize;

} Seqlist;

void Initlist(Seqlist *L) {

int n, i;

L->elem=(ElemType *)malloc(sizeof(ElemType)*LISTINITSIZE);

scanf("%d", &n);

for(i=0; i<n; i++) {

scanf("%d", &L->elem[i]);

}

L->length=n;

L->listsize=LISTINITSIZE;

}

void Prtlist(Seqlist L) {

int i;

for(i=0; i<; i++) {

printf("%d ",[i]);

}

}



int ListInsert(Seqlist *L,int i,ElemType e) {

int j,newsize;

if(i<1||i>L->length+1) {

return 0;

}

if(L->length>=L->listsize){

ElemType *newbase;

newsize=(L->listsize+INCREAMENT)*sizeof(ElemType);

newbase=(ElemType*)realloc(L->elem,newsize);

if(!(newbase)){

return 0;

}

L->listsize+=INCREAMENT;

L->elem=newbase;

}

for(j=L->length-1; i-1<=j; j--) {

L->elem[j+1]=L->elem[j];

}

L->elem[i-1]=e;

L->length++;

return 1;

}



int main(void) {

Seqlist L;

int i,e;

Initlist(&L);

scanf("%d,%d",&i,&e);

ListInsert(&L,i,e);

Prtlist(L);

}