C语言数据结构上机题

要求创建一个线性表,并完成插入,删除,查找的操作!
这是我写的程序,错误太多了,但还是请高手帮我改一下,谢谢了!
#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);
}

第1个回答  2010-03-29
参考一下我的吧:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define ElemType int

typedef int Status;
typedef struct SqList{
ElemType *elem;
int length;
int listsize;
};

int InitList_Sq(struct SqList *L)
{
L->elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L->elem)
exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}

int Load_Sq(struct SqList *L)
{
int i;
if(L->length == 0)
printf ("The List is empty!");
else
{
printf("The List is: ");
for(i = 1; i <= L->length; i++)
printf("%d\n",L->elem[i-1]);
}
printf("\n");
return OK;
}

int ListInsert_Sq(struct SqList *L, int i, int e)
{
int *q;
int *p;
ElemType *newbase;
if(i <1||i >L->length+1)
return ERROR;
if(L->length >= L->listsize)
{
newbase = (ElemType*)realloc(L->elem,(L->listsize + LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
exit (OVERFLOW);
L->elem = newbase;
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;
}

int ListDelete_Sq(struct SqList *L, int i,int *e)
{
int *p;
int *q;
if(i <1||i >L->length+1)
return ERROR;
p=&(L->elem[i-1]);
*e=*p;
q=L->elem + L->length -1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L->length;
return OK;
}

int main()
{

int a,i;
int temp;
ElemType *e=&temp;
ElemType x;
struct SqList *T;
T=(struct SqList *)malloc(sizeof(T));
if(InitList_Sq(T)==OK)
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1:printf("Please input the location and the number you want to insert:\n");
scanf("%d,%d",&i,&x);
if(ListInsert_Sq(T,i,x)==ERROR)
printf("Insert Error!\n");
else printf("The Element %d is Successfully Inserted!\n",x);
break;
case 2:scanf("%d",&i);
if(ListDelete_Sq(T,i,e)==ERROR)
printf("Delete Error!\n");
else printf("The Element %d is Successfully Deleted!\n",*e);
break;
case 3:Load_Sq(T);
break;
case 0:return 1;
}
}
}本回答被提问者采纳
相似回答