|
发表于 2020-10-24 12:46:21
|
显示全部楼层
#include <stdio.h>
#include <stdlib.h>
typedef int Status;
typedef int Elemtype;
#define OK 1
#define ERROR 0
#define INIT_LIST_SIZE 100
#define LISTINCREMENT 10
typedef struct list
{
Elemtype *elem;
int length;
int listsize;
}Sqlist;
Sqlist L;
Status Initlist(Sqlist L)
{
L.elem=(Elemtype*)malloc(INIT_LIST_SIZE*sizeof(Elemtype));
if(L.listsize==0)
return 0;
L.length=0;
L.listsize=INIT_LIST_SIZE;
return OK;
}
Status Insertlist(Sqlist L,int i,Elemtype *e)
{
Elemtype *q,*p;
if(i<1||i>L.length+1)
ERROR;
if(L.length>=L.listsize)
L.elem=(Elemtype*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(Elemtype));
L.listsize+=LISTINCREMENT;
*q=L.elem[i-1];
for(*p=L.elem[L.length-1];p>=q;p--)
*(p+1)=*p;
*e=*q;
L.length++;
return 0;
}
int main()
{
int i,n,x,m;
Initlist(L);
printf("输入元素个数:");
scanf("%d",&x);
printf("\n");
printf("输入元素:");
for(i=0;i<x;i++)
scanf("%d",&L.elem);
printf("插入的位置是:");
scanf("%d",&m);
printf("插入的元素是:");
scanf("%d",&n);
Insertlist(L,m,&n);
printf("%d",L.elem);
}
|
|