谁能帮我看看这个程序错在哪了---》》

王朝知道·作者佚名  2009-10-30
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

#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编译。

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航