| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 882 人关注过本帖
标题:[求助]小弟刚学数据结构,请教以下程序错在哪里?
只看楼主 加入收藏
CHEN5354520
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-4-18
收藏
 问题点数:0 回复次数:10 
[求助]小弟刚学数据结构,请教以下程序错在哪里?

这个程序没有错误,可是不能运行,为什么啊?
这个程序的功能是:建立一个链表,输入元素并把它输出来!


#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define ElemType int
#define out printf


typedef struct LNode{
ElemType date;
struct LNode *next;
}LNode,*linklist;


linklist creat(void){ //输入元素的单链表
linklist head;
linklist p,q;
p=(linklist) malloc(sizeof(struct LNode));
head=p=q;
if(!p) exit(0);

scanf("%d",&p->date);
p->next=NULL;
if(p->date==0){exit(0);}

while(p->date!=0){
p=(linklist) malloc(sizeof(struct LNode));
if(!p) {exit(0);}
scanf("%d",&p->date);
if(p->date==0)
{break;}
else
{
q->next=p;
q=p;
p->next=NULL;
}
}
return(head);
}


linklist print(linklist head){
linklist p;
for(p=head;p!=NULL;p=p->next){
out("\n%d",p->date);
}
return(head);
}

void main()
{
linklist head;
head=creat();
out("\n");
print(head);
}

搜索更多相关主题的帖子: 数据结构 
2007-04-20 23:40
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 

OK通过:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define ElemType int
#define out printf


typedef struct LNode{
ElemType date;
struct LNode *next;
}LNode,*linklist;


LNode *creat(void){ //输入元素的单链表
LNode *head,*p,*q;
int x;
head=(LNode *)malloc(sizeof(struct LNode));
head->next=NULL;
q=head;
printf("please input the date:\n");
scanf("%d",&x);
puts("input the list end with '0':");
while(x)
{
p=(LNode *) malloc(sizeof(struct LNode));
p->date=x;
if(!p)
{printf("No memory!\n");
return NULL;
}
p->next=NULL;
q->next=p;
q=p;
scanf("%d",&x);
}
return(head);
}


void print(LNode *head){
LNode *p;
//p=head;
for(p=head->next;p!=NULL;p=p->next){
out("%-5d",p->date);
}
printf("\n");
//return(head);
}

void main()
{
LNode *head;
head=creat();
out("\n");
print(head);
}


~~我的明天我知道~~
2007-04-21 09:45
lq317883361
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-4-20
收藏
得分:0 
写得比较清晰,同意.
2007-04-21 09:57
dainichuhai
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2005-10-14
收藏
得分:0 
回复:(pinglideyu)OK通过:#include ...
2楼算法有错误:输入5
1 2 3 4 5 0 回车
结果为 5 1 2 3 4 5;

原因分析下再恢复

2007-04-21 16:59
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 

你看没错呀,冤枉人!
please input the date:
1 2 3 4 5
input the list end with '0':
0

1 2 3 4 5
Press any key to continue


~~我的明天我知道~~
2007-04-21 18:43
oclassic
Rank: 1
等 级:新手上路
帖 子:140
专家分:0
注 册:2007-4-18
收藏
得分:0 
-> 这个符号什么意思?

还有C语言的注释不是/*   */吗?
//也行??

编程群号码30772309群刚刚建立一个论坛http://tszbbs./?u=2 诚招斑猪哦
2007-04-21 18:49
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
是这样的.当我们要引用结构体里面的元素时,我们可以用圆点运算符,也可以用指针.
->就是指针的引用方式.

~~我的明天我知道~~
2007-04-21 18:58
Vincentwong
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-3-23
收藏
得分:0 

这是我上数据结构时写的关于链表的程序
包括建立头结点,建立链表,头插法,链表的插入删除
恐怕这不是最好的,但希望能给你点帮助








# include <stdio.h>
# include <malloc.h>
# define NULL 0
# define LEN sizeof(struct node)


struct node{
int data;
struct node *next;
};


struct node * create_list(int n)
{
int i;
struct node *head,*p1,*p2;
head=p2=p1=(struct node *)malloc(LEN);
for(i=0;i<n;i++)
{
p1=(struct node *)malloc(LEN);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return(head);
}


void input(struct node * head)
{
struct node *p1;
p1=head->next;
while(p1)
{
scanf("%d",&p1->data);
p1=p1->next;
}
}


void output(struct node *head)
{
struct node *p2;
p2=head->next;
while(p2)
{
printf("%d ",p2->data);
p2=p2->next;
}
}


void headinsert_list(int *m,struct node *head)
{
struct node *p;
p=(struct node *)malloc(LEN);
printf("\nplease input the data in node p:\n");
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
*m+=1;
}


void insert_list(int n,int *m,struct node *head)
{
int i;
struct node *p;
struct node *q;
p=head->next;
q=(struct node *)malloc(LEN);
if(n>*m)
{
printf("fail to insert\n");
}
else
{
for(i=1;i<=*m;i++)
{
if(i!=(n-1))
{
p=p->next;
}
else
{
printf("Please input the data in node q;\n");
scanf("%d",&q->data);
q->next=p->next;
p->next=q;
*m+=1;
printf("You have successfully inserted %d.\n",q->data);
break;
}
}
}
}

void delete_list(int n,int *m,struct node * head)
{
int i;
struct node *p1,*p2;
p1=head->next;
p2=head;
if(n>*m)
{
printf("fail to detele.\n");
}
else
{
for(i=1;i<=*m;i++)
{
if(i!=n)
{
p2=p1;
p1=p1->next;
}
else
{
p2->next=p1->next;
p1->next=NULL;
*m-=1;
printf("You have successfully deleted %d.\n",p1->data);
free(p1);
break;
}
}
}
}


void main()
{
struct node *head;
int seat1,seat2;
int len;
int *len_ptr;
len_ptr=&len;
printf("\nPlease input the length of the list:\n");
scanf("%d",&len);
head=create_list(len);
printf("\nNow,you will input the numbers into the list.\n");
input(head);
printf("\nNow,please check these data.\n");
output(head);
printf("\nlength is %d\n",*len_ptr);
headinsert_list(len_ptr,head);
output(head);
printf("\nlength is %d\n",*len_ptr);
printf("\nNow,please locate the inserted seat.\n");
scanf("%d",&seat1);
insert_list(seat1,len_ptr,head);
output(head);
printf("\nlength is %d\n",*len_ptr);
printf("\nNow,please locate the deleted seat.\n");
scanf("%d",&seat2);
delete_list(seat2,len_ptr,head);
output(head);
printf("\nlength is %d\n",*len_ptr);
}


2007-04-21 22:35
CHEN5354520
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-4-18
收藏
得分:0 

非常的谢谢各位为小弟我解决问题,希望以后还能得到你们的HELP!


我虽然不是最棒的,但我却要做最棒的! 朋友们请相信只要努力了,那就一定会有收获的!
2007-04-22 23:07
限量版猪头
Rank: 2
等 级:论坛游民
威 望:1
帖 子:165
专家分:30
注 册:2006-2-5
收藏
得分:0 
以下是引用oclassic在2007-4-21 18:49:07的发言:
-> 这个符号什么意思?

还有C语言的注释不是/*   */吗?
//也行??

她用的是VC


2007-04-23 10:35
快速回复:[求助]小弟刚学数据结构,请教以下程序错在哪里?
数据加载中...
 
   



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

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