关于数据结构的一个程序

我们数据结构老师要求我们交实验的程序,但是现在还没搞出来,请大家来帮帮我吧,谢谢了!(要是能很好解决问题的话,还有追加分,先放50出来了)
题目是
试验一: 设某线性表数据元素的类型为整型,以顺序表为存储结构。试编程实现:
⑴ 线性表置空
⑵ 求线性表长度
⑶ 数据元素的插入操作
⑷ 数据元素的删除操作
⑸ 显示线性表中的全部元素
我的程序是
#include<STDIO.H>
#include<MALLOC.H>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define NULL 0
typedef int ElemType
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(OVERFLOW);
(*L).length=0;
(*L).listsize=LIST_INIT_SIZE;
return OK;
}

Status ClearList(SqList *L)
{
(*L).length=0;
return OK;
}

int ListLength(SqList L)
{
return L.length;
}

Status ListInsert(SqList *L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
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;
}

Status ListDelete(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>(*L).length)
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;
}

Status displist(SqList L)
{
ElemType *p;
p=L.elem;
while(p!=NULL)
{p++;
printf("%d\n",*p);
}

main()
{int n,i,e;
sqlist *L;InitList(*L);
printf("1Clear the List2the Length of List3insert element4Delete the element5show all the element");
printf("\nPlease choose a num!");
scanf("%d",&n);
switch(n)
{case 1:printf("Clear the List");
ClearList(*L);break;
case 2:printf("ClearList") ;
ListLength(*L);break;
case 3:printf("insert element\nplease input a element");
scranf("%d%d",&i,&e);ListInsert(*L,i,e);break;
case 4:printf("Delete the element");
scranf("%d%d",&i,&e);ListDelete(*L,i,e);break;
case 5;printf("show all the element");
displist(*L);
}
}

但是我在TC2.0下编译不能通过,提示错误为:
c1:unable to open include file 'stdio.h'
c2:unable to open include file 'malloc.h'
c12:declaration error syntax
c20:declaration error syntax

第1个回答  推荐于2016-02-14
/*我给改了下,改的说明注释写了,改后编译能通过,但是有逻辑错误,逻辑错误太多了。。。改了一些,剩下你自己改吧。。*/
#include<STDIO.H>
#include<MALLOC.H>
#include<stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define NULL 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(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}

Status ClearList(SqList *L)
{
(*L).length=0;
return OK;
}

int ListLength(SqList L)
{
return L.length;
}

Status ListInsert(SqList *L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
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;
}

Status ListDelete(SqList *L,int i,ElemType &e) //要改变e的值,就这样定义
{
ElemType *p,*q;
if(i<1||i>(*L).length)
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;
}

void displist(SqList L) //没有返回值的定义为void型
{
ElemType *p;
p=L.elem;
while(p!=NULL)
{p++;
printf("%d\n",*p);
}
}//遗漏括号

main()
{int n,i,e;
SqList *L=(SqList *)malloc(sizeof(SqList));InitList(L); //大写的S、L,Sqlist; *L改成L;另外L是指针,没有给L分配空间就使用了

while(n){
fflush(stdin);//清空输入缓冲区,防止冗余输入
printf("\n0Exit\n1Clear the List\n2the Length of List\n3insert element\n4Delete the element\n5show all the element\n");//加换行符方便观看
printf("Please choose a num!\n");
scanf("%d",&n);
switch(n)
{
case 0:exit(0);
case 1:printf("Clear the List");
ClearList(L);break; //*L改成L
case 2:printf("The Length of List=%d\n",ListLength(*L));break;
case 3:printf("insert element\nplease input an element, input i and e separated by space\n");
scanf("%d %d",&i,&e);ListInsert(L,i,e);break;//scanf拼写错误,*L改成L
case 4:printf("Delete the element,input i and e separated by space\n");
scanf("%d %d",&i,&e);ListDelete(L,i,e);break; //scanf拼写错误,*L改成L
case 5:printf("show all the element\n"); //case 5后面是冒号不是分号
displist(*L);
}
}
}本回答被提问者采纳
第2个回答  2012-05-26
你这个程序能发个给我么
相似回答