主题:求助:我写的链表实现的程序(C语言)编译通过了但结果不对.
zwdnet2
[专家分:0] 发布于 2006-11-04 09:36:00
求助:我写的链表实现的程序编译通过了但结果不对.
我写了个实现链表的程序,在wintc(相当与tc2.0)里编译通过,但是运行时要输出链表程序给出的结果却不对,请各位帮忙看看.谢谢.
代码如下:
/*建立链表并显示*/
#include <stdio.h>
struct myList /*链表结构*/
{ /*数据*/
int data;
struct myList *next; /*链表指针*/
};
/*建立链表*/
struct myList *CreatList(void)
{
int x;
struct myList *s,*q,*h;
h = (struct myList *)malloc(sizeof(struct myList));
h->next = NULL;
q = h;
printf("input number:");
scanf("%d",&x);
while(x != 0) /*为链表赋值*/
{
s = (struct myList *)malloc(sizeof(struct myList));
s->data = x;
q->next = s;
printf("input number:");
scanf("%d",&x);
s->next = NULL;
q = s;
}
return (h);
}
/*输出所有链表的值*/
int putList(struct myList *head)
{
int i = 0;
if(head->next == NULL) /*若是空链表则返回*/
{
return 0;
}
while(head->next != NULL) /*输出链表,这里运行结果不对*/
{
i++;
printf("the %d number in list is:%d\n",i,head->data);
head = head ->next;
}
return 1;
}
main()
{
struct myList *my;
my = CreatList(); /*建立链表*/
if(putList(my) == 0) /*输出链表,这里运行结果不对*/
printf("The input list is empty!");
while(my->next != NULL)
{
free(my);
my = my->next;
}
printf("over,press any key to end");
getch();
clrscr();
}
运行时输入1(回车)2(回车)3(回车)0(回车) 其中"0"代表结束输入
程序给出的输出为:
the 1 number in list is:-30170
the 2 number in list is:1
the 3 number in list is:2
over,press any key to end
第一个输出完全错了,
剩下两个数字对了但位置不对
我设想的正确的应为:
the 1 number in list is:1
the 2 number in list is:2
the 3 number in list is:3
over,press any key to end
请各位帮忙看看哪里错了,谢谢帮助!
[em10]
回复列表 (共14个回复)
沙发
Shadowfax [专家分:890] 发布于 2006-11-04 13:59:00
while(head->next != NULL) /*输出链表,这里运行结果不对*/
{
i++;
printf("the %d number in list is:%d\n",i,head->data); //错
head = head ->next;
}
这里错了
你建立链表的时候,H是空的结点...你没对Head->data赋值...
也就是说你的链表是 Head->1->2->3->NULL的。
板凳
bingbinggongzi [专家分:30] 发布于 2006-11-04 16:28:00
#include <stdio.h>
#include <stdlib.h>
struct myList /*链表结构*/
{ /*数据*/
int data;
struct myList *next; /*链表指针*/
};
/*建立链表*/
struct myList *CreatList(void)
{
int x;
struct myList *s,*q,*h;
h = (struct myList *)malloc(sizeof(struct myList));
h->next = NULL;
q = h;
printf("input number:");
scanf("%d",&x);
while(x != 0) /*为链表赋值*/
{
s = (struct myList *)malloc(sizeof(struct myList));
s->data = x;
q->next = s;
printf("input number:");
scanf("%d",&x);
s->next = NULL;
q = s;
}
return (h);
}
/*输出所有链表的值*/
int putList(struct myList *head)
{
int i = 0;
if(head->next == NULL) /*若是空链表则返回*/
{
return 0;
}
head = head ->next;// 加了一句
while(head!= NULL) /*输出链表,这里运行结果不对*/
{
i++;
printf("the %d number in list is:%d\n",i,head->data);
head=head->next;
}
return 1;
}
void main()
{
struct myList *my,*p;
my = CreatList(); /*建立链表*/
if(putList(my) == 0) /*输出链表,这里运行结果不对*/
printf("The input list is empty!");
while(my->next != NULL) //这里给你改了
{
p=my->next;
my->next = p->next;
free(p);
}
printf("over,press any key to end");
// getch();
// clrscr();
}
3 楼
zwdnet2 [专家分:0] 发布于 2006-11-05 09:05:00
多谢两位帮助!
4 楼
zwdnet2 [专家分:0] 发布于 2006-11-05 11:34:00
不好意思,又出问题了,我重新写了一下,其它函数都对了,就是输出这个函数还不对,代码我只贴有问题的了.
/*数据结构:链表的实现
2006年11月5日*/
#include <stdio.h>
#define Over -1
/*单链表结构类型定义*/
typedef struct node
{
int data;
struct node *next; /*结点的指针域*/
}LinkList;
LinkList *head,*p;
/*其他没有问题的函数我略了*/
/*显示所有单链表的值*/
void DispLinkList(LinkList *head)
{
int i = 0;
if(head == NULL)
{
printf("This is a empty List,can't display\n");
return;
}
while(head!= NULL) /*这里循环800多次才停*/
{
i++;
head = head->next;
printf("the %d number in the List is:%d\n",i,head->data);
getch();
}
printf("output over! press any key to continue!\n");
getch();
}
/*主程序,测试用*/
main()
{
LinkList *my;
int num[] = {1,2,3,4,5};
my = CreatLinkList(num);
printf("the 4th number is%d\n",GetNode(my,4)->data);
InsertLinkList(my,10,4);
printf("the 4th number after Insert is%d\n",GetNode(my,4)->data);
DeleteLinkList(my,4);
printf("the 4th number after Insert is%d\n",GetNode(my,4)->data);
getch();
printf("5 is in %d of the List\n",LocateNode(my,3)->data);
getch();
printf("display all numbers\n");
DispLinkList(my);/*调用这里出错*/
DestroyLinkList(my);
getch();
clrscr();
}
运行后前5个数据输出正常,但是过了第五个后DispLinkList里的while循环还没有
终止,一直运行了800多次.显示的数据也是乱七八糟的.请帮我看看那里出问题.谢谢!
5 楼
freeeerf [专家分:5440] 发布于 2006-11-05 20:02:00
如果head是链表的最后一个节点,则
while(head!= NULL) /*这里循环800多次才停*/
{
i++;
head = head->next;//此次一执行则head为NULL了.
printf("the %d number in the List is:%d\n",i,head->data);//然后当然这句就会出现问题了.
getch();
}
6 楼
zwdnet2 [专家分:0] 发布于 2006-11-06 16:02:00
谢谢,可是我把主函数改成这样:
main()
{
LinkList *my;
int num[] = {1,2,3,4,5};
my = CreatLinkList(num);
DispLinkList(my); /*我把调用挪到这里,问题照旧*/
printf("the 4th number is%d\n",GetNode(my,4)->data);
InsertLinkList(my,10,4);
printf("the 4th number after Insert is%d\n",GetNode(my,4)->data);
DeleteLinkList(my,4);
printf("the 4th number after Insert is%d\n",GetNode(my,4)->data);
getch();
printf("5 is in %d of the List\n",LocateNode(my,3)->data);
getch();
printf("display all numbers\n");
/*DispLinkList(my); 原来在这里调用*/
DestroyLinkList(my);
getch();
clrscr();
}
把DispLinkList函数的调用改在创建了链表(CreatLinkList)之后,其他操作之前,问题照旧啊?
另外我把程序输出贴一下,这是上面改过后的程序.
the 1 number in the List is:1
the 2 number in the List is:2
the 3 number in the List is:3
the 4 number in the List is:4
the 5 number in the List is:5
the 6 number in the List is:-30
the 7 number in the List is:292
the 8 number in the List is:1356
the 9 number in the List is:1
the 10 number in the List is:-32
the 11 number in the List is:2316
the 12 number in the List is:-28
the 13 number in the List is:0
the 14 number in the List is:14917
the 15 number in the List is:23132
the 16 number in the List is:19801
the 17 number in the List is:17500
the 18 number in the List is:21569
the 19 number in the List is:23617
the 20 number in the List is:18764
the 21 number in the List is:19278
the 22 number in the List is:18764
the 23 number in the List is:21587
......(从5以后就是错的)
7 楼
zwdnet2 [专家分:0] 发布于 2006-11-06 16:08:00
所有程序都贴出来吧:
/*数据结构:链表的实现
XXX
2006年11月5日*/
#include <stdio.h>
#define Over -1
/*单链表结构类型定义*/
typedef struct node
{
int data;
struct node *next; /*结点的指针域*/
}LinkList;
LinkList *head,*p;
/*分配内存出错处理函数*/
void JudgeMalloc(LinkList *L)
{
if(L==NULL)
{
printf("malloc error!");
getch();
return;
}
}
/*建立带头结单链表*/
LinkList *CreatLinkList(int num[])
{/*将新结点插入到链表的尾部,建立起带头结点的单链表*/
int i = 0;
LinkList *head;
LinkList *s;
LinkList *rear;
head = (LinkList *)malloc(sizeof(LinkList));
JudgeMalloc(head);
rear = head;
while(num[i]!= Over)
{
s = (LinkList *)malloc(sizeof(LinkList));
JudgeMalloc(s);
s->data = num[i];
rear->next = s;/*新结点插入表尾*/
rear = s; /*尾指针rear指向新的表尾*/
i++;
}
rear->next = NULL;/*将rear所指的结点的指针域置为NULL*/
return head;
}
/*销毁链表*/
void DestroyLinkList(LinkList *head)
{
LinkList *p;
if(head == NULL)
return;
while(head->next!=NULL)
{
p = head->next;
head->next = p->next;
free(p);
}
}
/*按序号查找*/
LinkList *GetNode(LinkList *head,int i)
{
int j;
LinkList *p;
p = head;
j = 0;
while(p!= NULL && j<i)
{
p = p->next;
j++;
}
if(i==j)
return p;/*找到了第i个结点*/
else
return NULL;
}
/*按值查找*/
LinkList *LocateNode(LinkList *head, int Key)
{/*在带头结点的单链表head中查找其值为Key的结点*/
LinkList *p;
p = head->next;/*p就指向链表的第一个值*/
while(p!= NULL && p->data != Key)
p = p->next;
return p;
/*若return NULL则查找失败*/
}
/*插入*/
/*1.从头结点开始找到a(i-1)存储的位置p
2.生成一个新结点*s
3.将x赋给新结点*s的数据域.
4.将新结点*s的指针指向结点a(i)
5.结点指针域指向新结点*s
*/
void InsertLinkList(LinkList *head, int x,int i)
{
LinkList *p,*s;
p = GetNode(head,i-1); /*1*/
if(p==NULL)
{
printf("position error\n");
return;
}
else
{
s = (LinkList *)malloc(sizeof(LinkList));/*2*/
JudgeMalloc(s);
s->data = x; /*3*/
s->next = p->next; /*4*/
p->next = s; /*5*/
}
}
/*删除操作*/
/*1.找到a(i-1)的存储位置p
2.使r指向被删除的结点a(i),即r = p->next;
3.将a(i)从链上摘下,即p->next = r->next;
4.释放结点a(i)的空间,将其归还系统
*/
void DeleteLinkList(LinkList *head, int i)
{
LinkList *p,*r;
p = GetNode(head, i-1); /*1*/
if(p==NULL || p->next == NULL)
{
printf("position error\n");
return;
}
else
{
r = p->next; /*2*/
p->next = r->next; /*3*/
free(r);
}
}
/*显示所有单链表的值*/
void DispLinkList(LinkList *head)
{
int i = 0;
if(head == NULL)
{
printf("This is a empty List,can't display\n");
return;
}
while(head!= NULL)
{
i++;
head = head->next;
printf("the %d number in the List is:%d\n",i,head->data);
/*getch();*/
}
printf("output over! press any key to continue!\n");
getch();
}
/*主程序,测试用*/
main()
{
LinkList *my;
int num[] = {1,2,3,4,5};
my = CreatLinkList(num);
DispLinkList(my); /*我把调用挪到这里,问题照旧*/
printf("the 4th number is%d\n",GetNode(my,4)->data);
InsertLinkList(my,10,4);
printf("the 4th number after Insert is%d\n",GetNode(my,4)->data);
DeleteLinkList(my,4);
printf("the 4th number after Insert is%d\n",GetNode(my,4)->data);
getch();
printf("5 is in %d of the List\n",LocateNode(my,3)->data);
getch();
printf("display all numbers\n");
/*DispLinkList(my); 原来在这里*/
DestroyLinkList(my);
getch();
clrscr();
}
8 楼
Shadowfax [专家分:890] 发布于 2006-11-06 21:14:00
/*按序号查找*/
LinkList *GetNode(LinkList *head,int i)
{
int j;
LinkList *p;
p = head;
j = 0;
while(p!= NULL && j<i) //把p改成p->next
{
p = p->next;
j++;
}
if(i==j)
return p;/*找到了第i个结点*/
else
return NULL;
}
/*显示所有单链表的值*/
void DispLinkList(LinkList *head)
{
int i = 0;
if(head == NULL) //改head为head->next
{
printf("This is a empty List,can't display\n");
return;
}
while(head!= NULL) //改head为head->next
{
i++;
head = head->next;
printf("the %d number in the List is:%d\n",i,head->data);
/*getch();*/
}
printf("output over! press any key to continue!\n");
getch();
}
//建议先弄明白什么是头结点...
9 楼
zwdnet2 [专家分:0] 发布于 2006-11-08 12:53:00
谢谢您,可是我改成这个样子还是出错:
/*显示所有单链表的值*/
void DispLinkList(LinkList *head)
{
LinkList *p; /*我忽然想到,直接用head是不是函数调完了head 的值也变了?所以加了个p*/
int i = 0;
p = head;
if(p->next == NULL)
{
printf("This is a empty List,can't display\n");
return;
}
while(p ->next!= NULL)
{
i++;
p = p->next;
printf("the %d number in the List is:%d\n",i,p->data);
/*getch();*/
}
printf("output over! press any key to continue!\n");
getch();
}
运行结果为:
the 1 number in the List is:1
the 2 number in the List is:2
the 3 number in the List is:3
the 4 number in the List is:4
the 5 number in the List is:5
the 6 number in the List is:-30
the 7 number in the List is:292
the 8 number in the List is:3533
the 9 number in the List is:1
the 10 number in the List is:-32
the 11 number in the List is:2316
the 12 number in the List is:-28
the 13 number in the List is:0
the 14 number in the List is:14918
the 15 number in the List is:23132
the 16 number in the List is:19801
the 17 number in the List is:17500
the 18 number in the List is:21569
the 19 number in the List is:23617
......
the 348 number in the List is:24864
the 349 number in the List is:27756
the 350 number in the List is:28192
the 351 number in the List is:28021
the 352 number in the List is:25954
the 353 number in the List is:29554
the 354 number in the List is:10
the 355 number in the List is:0
the 356 number in the List is:4864
the 357 number in the List is:514
the 358 number in the List is:1284
the 359 number in the List is:2054
the 360 number in the List is:2056
the 361 number in the List is:5396
the 362 number in the List is:4869
the 363 number in the List is:5887
the 364 number in the List is:4357
the 365 number in the List is:-254
output over! press any key to continue!
the 4th number is4
the 4th number after Insert is10
the 4th number after Insert is4
5 is in 3 of the List
display all numbers
10 楼
Shadowfax [专家分:890] 发布于 2006-11-08 16:18:00
你看看你的是不是这里错了。
while(num[i]!= Over) //不知道是不是这个循环的问题。
{
s = (LinkList *)malloc(sizeof(LinkList));
JudgeMalloc(s);
s->data = num[i];
rear->next = s;/*新结点插入表尾*/
rear = s; /*尾指针rear指向新的表尾*/
i++;
}
=。= 我也是粗略的看了下C语言。有些细节还是不太了解。不过这个编译在我的C-FREE是通过了的。我看了下超出范围的数组,超出的第一个元素值为-1。不知道你的是不是。你检查下,人为的把数组int num[] = {1,2,3,4,5};改为int num[] = {1,2,3,4,5,-1};试一试,看看你的系统会不会自动-1标志结束。
另外,
void DispLinkList(LinkList *head)
{
LinkList *p; /*我忽然想到,直接用head是不是函数调完了head 的值也变了?所以加了个p*/
int i = 0;
p=head;
以下省略
}
这个问题我也想求教一下。不过我个人是觉得head是不变的。你在实际调用这个函数的时候,系统重新开辟了一个head指针,这个head是指向主体函数head指针传递的地址的。但在函数里head指针的变化,是不影响到这个函数外的head指针,因为2个指针,本身的地址是不同的。
我也不知道对不对。不过好象是这样。
我来回复