在顺序表中插入一数据!帮忙改下!谢啦!
#include<stdio.h>#include <malloc.h>
#include<math.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ok 1
#define error 0
typedef int ElemType;
typedef int Status;
typedef struct{
ElemType * elem;
int length;
int listsize;
}SqList;
Status InitList(SqList &L)
{
L.elem=(ElemType * )malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem)exit(error);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return ok;
}//ok
Status ListInsert(SqList &L,int i,ElemType &x)
{
int *newbase;
i=0;int j;
if(L.length>=L.listsize)
{
newbase=(int*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase)exit(-1);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
while(L.elem[i]<x&&i<L.length)
i++;
if(i>L.length) return 0;
for(j=L.length-1;j>=i;j--)
L.elem[j+1]=L.elem[j];
L.elem[i]=x;
L.length++;
return ok;
}//ok
Status ListCreat_T(SqList &L)
{
ElemType temp ;int y=1,n;
printf("input L.length:\n");
scanf("%d",&n);
L.length=n;
printf("Please Input Date(n) ending\n");
scanf("%d",&temp);
for(y=1;y<=n;y++)
{
scanf("%d",&temp);
L.elem[y]=temp;
}
return L.length;
}
Status traList(SqList L,int n)
{
int i;
for(i=1;i<=L.length+1;i++)
printf("%d",L.elem[i]);
return L.elem[i];
}
int main()
{
SqList L;
int x,i;
L.length=ListCreat_T(L);
scanf("%d,%d",&x,&i);
InitList(L);
ListCreat_T(L);
ListInsert(L,i,x);
traList(L,L.length);
return 0;
}