要求创建一个线性表,并完成插入,删除,查找的操作!
这是我写的程序,错误太多了,但还是请高手帮我改一下,谢谢了!
#define TRUE 1
#define OK 1
#define OVERFLOW 0
#define FALSE 0
#define ERROR 0
#define null 0
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 5
#include<stdio.h>
#include<alloc.h>
#include<stdlib.h>
typedef char ET;
typedef int Status;
typedef struct{
ET *elem;
Status length;
Status listsize;
}SqList;
Status ListInsert(SqList *L,int i,ET e){
ET *p,*q;
if(i<1||i>L.length+1)
return ERROR;
if(L.length>=L.listsize)
{
q=(ET *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ET));
if(!q)
exit(OVERFLOW);
L.elem=q;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;
*q=e;
+=L.length;
return OK;
}
Insert(L){
int i;
ET e;
printf("please input the position:");
scanf("%d",&i);
printf("please input the elem:");
scanf("%c",&e);
ListInsert(L,i,e);
}
Status ListDelete(SqList *L,int i,ET *e){
ET *p,*q;
if((i<1)||(i>L.length)) return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.leng
th-1;
for(++p;p<=q;++p) *(p-1)=*p;
--L.length;
return OK;
}
Delete(L){
int i;
ET e;
printf("please input the position:");
scanf("%d",&i);
ListDelete(L,i,&e);
}
Status LocateElem(SqList *L,ET e, Status(*compare)(ET,ET)){
ET *p;
int i=1;
p=L.elem;
while(i<=L.length&&!(*compare)(*p++,e)) ++i;
if(i<=L.length) return i;
else return 0;
}
Locate(L){
ET e;
printf("please input the elem:");
scanf("%c",&e);
LocateElem(L,e,(*compare)(ET,ET));
}
Status Init(SqList *L){
ET e;
int done=TRUE;
InitList(&L);
while(done){
scanf("%c",&e);
if(e!="\n")
ListInsert(&L,L.length+1,e);
else done=FALSE;
}
return OK;
}
void menu(){
printf("*****************************\n");
printf("* 1.hurt the elem of L *\n");
printf("* 2.delete the elem of L *\n");
printf("* 3.insert the elem of L *\n");
printf("*****************************\n");
}
void MenuSelect(SqList *La){
int i,done=1;
while(done){
menu();
printf("input the choice code:");
scanf("%d,&i");
switch(i);
{
case 1: Locate(La); break;
case 2: Delete(La); break;
case 3: Insert(La); break;
case 4: done=0; break;
default:printf("ERROR\n");
}
}
}
main(){
SqList La;
printf("please input the init La's elem:\n");
Init(&La);
MenuSelect(&La);
}