当前位置: 代码迷 >> C语言 >> [求助]这段程序困扰我好久了……
  详细解决方案

[求助]这段程序困扰我好久了……

热度:302   发布时间:2006-02-27 17:00:00.0
[求助]这段程序困扰我好久了……

#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{
ElemType elem[MaxSize];
int length;
}SqList;
void InitList(SqList *L)
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void DestroyList(SqList *L)
{
free(L);
}
int ListEmpty(SqList *L)
{
return(L->length==0);
}
int ListLength(SqList *L)
{
return(L->length);
}
void ListInsert(SqList *L,int i,ElemType e)
{
int j;
if(i<1)
printf("error!\n");
i--;
for(j=L->length;j>i;j--)
L->elem[j]=L->elem[j-1];
L->elem[i]=e;
(L->length)++;
}
void DispList(SqList *L)
{
int i;
if(ListEmpty(L))
printf("The list is empty!\n");
for(i=0;i<L->length;i++)
printf("%c",L->elem[i]);
printf("\n");
}
char GetElem(SqList *L,int i,ElemType e)
{
if(i<1||i>L->length)
printf("error!\n");
e=L->elem[i-1];
return e;
}
int LocateElem(SqList *L,ElemType e)
{
int i=0;
while(i<L->length&&L->elem[i]!=e)i++;
if(i>=L->length)
printf("overflow!\n");
else
return i+1;
}
int ListDelete(SqList *L,int i,ElemType e)
{
int j;
if(i<1||i>L->length)
printf("error!\n");
i--;
e=L->elem[i];
for(j=i;j<L->length-1;j++)
L->elem[j]=L->elem[j+1];
L->length--;
}

void main()
{ SqList *L1;
ElemType e1;
printf("(1)Inital the list L\n");
InitList(L1);
printf("length=%d\n",L1->length);
printf("(2)Insert a,b,c,d,e\n");
ListInsert(L1,1,'a');
ListInsert(L1,2,'b');
ListInsert(L1,3,'c');
ListInsert(L1,4,'d');
ListInsert(L1,5,'e');
printf("(3)Print the list L:");
DispList(L1);
printf("(4)The length of the list =%d\n",ListLength(L1));
printf("(5)The list is:%s\n",(ListEmpty(L1)?"empty":"unempty"));
printf("(6)The third element in the list is:%c\n",GetElem(L1,3,e1));
printf("(7)The element a's location is:%d\n",LocateElem(L1,'a'));
printf("(8)Insert element f on the fourth element's location\n");
ListInsert(L1,4,'f');
printf("(9)Print the list L:");
DispList(L1);
printf("(10)Delete the third element\n");
ListDelete(L1,3,e1);
printf("(11)Print the list L:");
DispList(L1);
printf("(12)Free the list L\n");

}
这个初始化
void InitList(SqList *L)
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
无论把length=0改成多少,运行时都是显示length初始是-1,导致结果出现length=4,少了一个元素,哪位哥哥能告诉我这里初始化有问题吗?应该怎么才能运行出正确结果啊,5555555

我实在没办法了,才来这里问大家,谢谢大家帮我看看吧!

搜索更多相关的解决方案: 困扰  

----------------解决方案--------------------------------------------------------

下次写上注释,就有更多人看了


----------------解决方案--------------------------------------------------------
  相关解决方案