代码如下:
问题在红色部分
#include<stdio.h>
#include<malloc.h>
typedef int elemtype;
#define INITSIZE 100
typedef struct
{
elemtype *data;
int length;
int listsize;
}sqlink;
/**********************************************/
//开辟顺序表
sqlink *initlist(sqlink *L,int i)
{
int j;
L->data=(elemtype*)malloc(sizeof(elemtype)*INITSIZE);
L->length=0;
L->listsize=INITSIZE;
for(j=0;j<i;j++)
printf("Input %d list",j);
scanf("%d",L->data[i]);
return L;
}
/***********************************************/
//顺序表长度
int getlen(sqlink *L)
{
return L->length;
}
/*************************************************/
//取顺序表的元素
int getelm(sqlink *L,int i)
{
int e;
if(i<0||i>L->length)
return 0;
else
e=L->data[i-1];
return e;
}
/*************************************************/
//元素定位
int locate(sqlink *L,elemtype e)
{
int i=0;
while (i<L->length)
if(L->data[i]==e)
i++;
return i;
}
/*****************************************************/
//元素的插入
int insert(sqlink *L,int i,elemtype e)
{
int j;
if(i<1||i>L->length+1)
return 0;
else
if(L->length==L->listsize)
{
L->data=(elemtype *)realloc(L->data,(L->listsize+1)*sizeof(elemtype ));
L->listsize++;
}
for(j=L->length;j>=i;j--)
L->data[i+1]=L->data[i];
L->data[i-1]=e;
L->length++;
return 1;
}
/********************************************************/
//元素的删除
int delete(sqlink *L,int i,elemtype e)
{
int j;
if(i<1||i>(L->length+1))
return 0;
e=L->data[i-1];//储存删除的元素,windows 下的 Ctrl+z操作,但是在主函数段没有用,这里只是说明
for (j=i;j<L->length;j++)
L->data[i-1]=L->data[i];
L->length--;
return 1;
}
/*********************************************************/
//主函数
void main()
{
sqlink *initlist(sqlink *L,int i);//声明
int getlen(sqlink *L);
int getelm(sqlink *L,int i);
int locate(sqlink *L,elemtype e);
int insert(sqlink *L,int i,elemtype e);
int delete(sqlink *L,int i,elemtype e);
sqlink *K ,*L;
int m,n;
elemtype e;
K=initlist(L);//: error C2198: 'initlist' : too few actual parameters我用的是vc6.0
printf("OK!,created !\n");
printf("The length is %d\n",getlen(K));
printf("/***************************************************/\n");
printf("\n you can deal the follow things :\n");
printf(" \n 1.getelem \n 2.locate\n 3.insert \n 4.delete\n please choose one :\n");
printf("/***************************************************/\n");
scanf("%d",n);
if(n==1)
{ printf("Input which one do u want to get \n");
getchar();
scanf("%d",m);
getelm(K,m);
}
else
if(n==2)
{printf("Input which one do u want to get \n");
getchar();
scanf("%d",e);
locate(K,m);
}
else
if(n==3)
{printf("Input which one do u want to insert and what elem do u want to input:\n");
getchar();
scanf("%d %d",m,e);
insert(K,m,e);
}
else
if (n==4)
{ printf("Input which one do u want to delete :\n");
getchar();
scanf("%d ",m);
}
else
printf("The wrong input !\n");
printf("The length is %d\n",getlen(K));
}