看看所有的原代码吧!
#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)
i++;
if(L->data[i]==e)
return i;
}
/*****************************************************/
//元素的插入
int insert(sqlink *L,int i,elemtype e)
{
int j;
if(i<1||i>L->length+1)
return 0;
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 *L,*K;
char s[]="a";
int m;
elemtype e;
K=initlist(L);
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 getelem \n locate\n insert \n delete\n");
printf("/***************************************************/\n");
scanf("%s",s);
if(s[]=="getelem")
{
{ printf("Input which one do u want to get \n");
getchar();
scanf("%d",m);
getelm(K,m);
}
else
if(s[]=="locate")
printf("Input which one do u want to get \n");
getchar();
scanf("%d",e);
locate(K,m);
else
if(s[]=="insert")
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(m,e);
else
if (s[]=="delete")
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));
}