| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖
标题:[求助]这段程序困扰我好久了……
只看楼主 加入收藏
mengmeng
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-2-27
收藏
 问题点数:0 回复次数:1 
[求助]这段程序困扰我好久了……

#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

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

搜索更多相关主题的帖子: 困扰 
2006-02-27 17:00
feng1256
Rank: 4
等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
收藏
得分:0 

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


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-02-27 17:17
快速回复:[求助]这段程序困扰我好久了……
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014728 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved