c语言,链表问题

hi名威 2010-03-30 01:44:52
main函数主题
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct LNode //定义单链表结点类型
{
ElemType data;
struct LNode *next;
} LinkList;
extern void InitList(LinkList *&L);
extern void DestroyList(LinkList *&L);
extern int ListEmpty(LinkList *L);
extern int ListLength(LinkList *L);
extern void DispList(LinkList *L);
extern int GetElem(LinkList *L,int i,ElemType &e);
extern int LocateElem(LinkList *L,ElemType e);
extern int ListInsert(LinkList *&L,int i,ElemType e);
extern int ListDelete(LinkList *&L,int i,ElemType &e);
int _tmain(int argc, _TCHAR* argv[])
{
LinkList *h;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');

printf("(3)输出单链表h:");
DispList(h);
printf("(4)单链表h长度=%d\n",ListLength(h));
printf("(5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));
GetElem(h,3,e);
printf("(6)单链表h的第3个元素=%c\n",e);
printf("(7)元素a的位置=%d\n",LocateElem(h,'a'));
printf("(8)在第4个元素位置上插入f元素\n");
ListInsert(h,4,'f');
printf("(9)输出单链表h:");
DispList(h);
printf("(10)删除h的第3个元素\n");
ListDelete(h,3,e);
printf("(11)输出单链表h:");
DispList(h);
printf("(12)释放单链表h\n");
DestroyList(h);
}

请问怎么把上面红色的代码改成由使用者输入链表元素个数,输入各个元素的DATA后再生成相应的链表
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
函数实现如下
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct LNode //定义单链表结点类型
{
ElemType data;
struct LNode *next;
} LinkList;
void InitList(LinkList *&L)
{
L=(LinkList *)malloc(sizeof(LinkList)); //创建头结点
L->next=NULL;
}
void DestroyList(LinkList *&L)
{
LinkList *p=L,*q=p->next;
while (q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
int ListEmpty(LinkList *L)
{
return(L->next==NULL);
}
int ListLength(LinkList *L)
{
LinkList *p=L;int i=0;
while (p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}
void DispList(LinkList *L)
{
LinkList *p=L->next;
while (p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
int GetElem(LinkList *L,int i,ElemType &e)
{
int j=0;
LinkList *p=L;
while (j<i && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL)
return 0;
else
{
e=p->data;
return 1;
}
}
int LocateElem(LinkList *L,ElemType e)
{
LinkList *p=L->next;
int n=1;
while (p!=NULL && p->data!=e)
{
p=p->next;
n++;
}
if (p==NULL)
return(0);
else
return(n);
}
int ListInsert(LinkList *&L,int i,ElemType e)
{
int j=0;
LinkList *p=L,*s;
while (j<i-1 && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL) //未找到第i-1个结点
return 0;
else //找到第i-1个结点*p
{
s=(LinkList *)malloc(sizeof(LinkList)); //创建新结点*s
s->data=e;
s->next=p->next; //将*s插入到*p之后
p->next=s;
return 1;
}
}
int ListDelete(LinkList *&L,int i,ElemType &e)
{
int j=0;
LinkList *p=L,*q;
while (j<i-1 && p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL) //未找到第i-1个结点
return 0;
else //找到第i-1个结点*p
{
q=p->next; //q指向要删除的结点
if (q==NULL) return 0;
e=q->data;
p->next=q->next; //从单链表中删除*q结点
free(q); //释放*q结点
return 1;
}
}
...全文
97 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hi名威 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ljwilson 的回复:]
引用 6 楼 huanmie_09 的回复:
这样改过来。

C/C++ code

int list_size;
int i;
char ch;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("请输入元素个数:\n");
scanf("%d", &amp;list_size);
for(i=0;i……
[/Quote]

对了,为什么把里面的scanf("%c",&ch); 改成scanf(" %c",&ch);就可以了呢?我试了一下
发现改成scanf("%c ",&ch);的话输入“a b c”之后只显示“a b”
hi名威 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 huanmie_09 的回复:]
改了后把前面的fflush(stdin);去掉.
fflush(stdin);
scanf("%c",&ch);
上面两句合为一句:
scanf(" %c",&ch);
[/Quote]
终于搞定了......泪流满面啊~~!
谢谢~!!
huanmie_09 2010-03-30
  • 打赏
  • 举报
回复
改了后把前面的fflush(stdin);去掉.
fflush(stdin);
scanf("%c",&ch);
上面两句合为一句:
scanf(" %c",&ch);
hi名威 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 huanmie_09 的回复:]
fflush(stdin);
scanf("%c",&ch);
改为
scanf(" %c",&ch); /*注意格式控制符中的空格*/
[/Quote]
还是不行
结果如下


附上代码
int main()
{ int list_size;
char ch;
LinkList *h;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("请输入链表元素个数list_size\n");
scanf("%d",&list_size);
printf("(2)依次相应元素\n");
for( int i=1; i<=list_size; i++ )
{ fflush(stdin);
scanf(" %c",&ch);
ListInsert(h,i,ch);
}
printf("(3)输出单链表h:");
DispList(h);
return 0;
}
huanmie_09 2010-03-30
  • 打赏
  • 举报
回复
scanf(" %c",&ch);
左边的空格可以匹配任意多的空白字符(包括回车,空格,跳格),然后下一个非空白字符就作为ch的输入值了。
楼主可以看看c语言程序设计_现代方法里关于scanf函数的描述部分。
huanmie_09 2010-03-30
  • 打赏
  • 举报
回复
fflush(stdin);
scanf("%c",&ch);
改为
scanf(" %c",&ch); /*注意格式控制符中的空格*/

hi名威 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huanmie_09 的回复:]
这样改过来。

C/C++ code

int list_size;
int i;
char ch;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("请输入元素个数:\n");
scanf("%d", &list_size);
for(i=0;i<list_size;i++) {
fflus……
[/Quote]

OH~!我的神啊!原来真的是“fflush(stdin);"这个函数惹的祸~!!

问题是解决了,但是和我老师给我们演示的不一样。
我们老师输入数据的时候不用按回车
例如
直接"a b c"然后再回车
我们现在的是
“a(回车)b(回车)c(回车)”
有办法改过来吗?
huanmie_09 2010-03-30
  • 打赏
  • 举报
回复
这样改过来。

int list_size;
int i;
char ch;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("请输入元素个数:\n");
scanf("%d", &list_size);
for(i=0;i<list_size;i++) {
fflush(stdin);
scanf("%c",&ch);
ListInsert(h,i+1,ch);
}
/*
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');
*/

hi名威 2010-03-30
  • 打赏
  • 举报
回复
int main()
{
LinkList *h;
int x;
char data;
ElemType e;
printf("(1)初始化单链表h\n");
InitList(h);
printf("请输入链表元素个数x\n");
scanf("%d",&x);
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
for( int i=1; i<=x; i++ )
{
scanf("%c",&data);
ListInsert(h,i,data);
}
printf("(3)输出单链表h:");
DispList(h);
return 0;
}
这是我发帖之前自己想的,感觉理论上可行,可是实际运行不了,或者只输入了第一个数据
hi名威 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huanmie_09 的回复:]
C/C++ code

printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');
改为:
前面定义一个变量,表示链表元素个数;
如:
int list_……
[/Quote]

用不了
huanmie_09 2010-03-30
  • 打赏
  • 举报
回复

printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');
改为:
前面定义一个变量,表示链表元素个数;
如:
int list_size;
然后输入list_size, 并以此输入各元素,因为链表数据域为char类型,所以每次输入后都要清空键盘缓冲区:
scanf("%d", list_size);
for(i = 0; i < list_size; i++) {
scanf("%c", ch);
ListInsert(h,5,ch);
fflush(stdin);
}
linyongzuo 2010-03-30
  • 打赏
  • 举报
回复
printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
ListInsert(h,1,'a');
ListInsert(h,2,'b');
ListInsert(h,3,'c');
ListInsert(h,4,'d');
ListInsert(h,5,'e');

printf("please input the number of the data");
sacnf("%d",&n);
for( int i = 0; i < n; i++ )
{
ListInsert(h,i+1,data);//data手动输入。。

}
pur_e 2010-03-30
  • 打赏
  • 举报
回复
类似

printf("(2)依次采用尾插法插入a,b,c,d,e元素\n");
//输入个数n
for(int i=0;i<n;i++)
{
//输入数据data
ListInsert(h,i+1,data);
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧