#include <stdio.h>
#include <malloc.h>
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status
typedef struct
{
ElemType *elem; //存储空间长度
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList
/****1.构造一个空的线形表*****/
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;
}//InitList
/*********2.销毁线形表*******/
Status DestroyList(SqList &L)
{
p1=L;
if(p1)
{
free(p1);
p1++;
}
return OK;
}//DestroyList
/******3.将表L置空*******/
Status ClearList(SqList &L)
{
if(L)
{
L.elem=0;
}
return OK;
}
/*******4.判断表L是否为空*******/
Status ListEmpty(SqList L)
{
p2=L;
if(p2)
{
if( p2++->elem==0 )
{
return TURE;
}
else
return FALSE;
}
}
/****5.返回L中数据元素的个数*****/
Status ListLength(SqList L)
{
p3=L;
Counter=0;
while(p3)
{
if(p3++->elem)
{
Counter++;
}
}
return Counter;
}
/*****6.用e返回L中第i个数据元素的值*******/
GetElem(L,i,&e)
{
while(L)
{
*e=L.elem[i];
}
}//GetElem
/*********7.返回L中与e满足compare()函数的数据元素的位置********/
Status LocateElem(L,e,compare())
{
while(L)
{
i=0
if( compare(e,L.elem[i++]) ) //要先定义当e和L.elem[i++]满足条件时,值为0
{
return L.elem[i];
}
else
return 0;
}
}
/***8.返回cur_e的前驱*****/
Status PriorElem(L,cur_e,&pre_e)
{
p4=L;
while(p4->next && cur_e != L.elem) //cur_e不是第一个元素
{
if(p4++->next->elem !=cur_e)
{
pre_e=p4;
}
else
return FLASE;
}
}
/********9.返回cur_e的后驱********/
Status NextElem(L,cur_e,&next_e)
{
p5=L;
while(cur_e != L.elem[i-1] && p5->next)
{
if(p5++->elem != cru_e)
{
next_e=p5;
}
else
return FLASE;
}
}
/*****10.在L中第i个位置之前插入新的数据元素e,且长度加1*******/
Status ListInsert(SqList &L,int i,ElemType e)
{
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的新地址
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;
}//ListInsert
/*****11.删除L的第i个元素*********/
Status ListDelete(SqList &L,i,&e)
{
if(i<1 || i>L.length+1)
return ERROR;
q=L.elem+i-1;
e=*q;
for(p=q;p<=L.elem+L.length-1;p++)
{
*p=*(p+1);
}
--L.length;
return OK;
}
參考答案:错误多,举例:
1. typedef int Status 后面应该用";"结束。
2. ElemType没有定义,便在 typedef struct { ElemType *elem; ... } }SqList; 中引用了。
3. Status DestroyList(SqList &L)中p1没定义。
3. GetElem()函数类型和形参的类型都没有说明。PriorElem(), NextElem()形参的类型都没有说明。
.......
看程序应该用C++, 不要用Turbo C编译。