当前位置: 代码迷 >> C语言 >> [求助]小弟刚学数据结构,请教以下程序错在哪里?
  详细解决方案

[求助]小弟刚学数据结构,请教以下程序错在哪里?

热度:277   发布时间:2007-04-20 23:40:53.0
[求助]小弟刚学数据结构,请教以下程序错在哪里?

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


#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);
}

搜索更多相关的解决方案: 数据结构  

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

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);
}


----------------解决方案--------------------------------------------------------
写得比较清晰,同意.
----------------解决方案--------------------------------------------------------
回复:(pinglideyu)OK通过:#include ...
2楼算法有错误:输入5
1 2 3 4 5 0 回车
结果为 5 1 2 3 4 5;

原因分析下再恢复
----------------解决方案--------------------------------------------------------

你看没错呀,冤枉人!
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


----------------解决方案--------------------------------------------------------
-> 这个符号什么意思?

还有C语言的注释不是/*   */吗?
//也行??
----------------解决方案--------------------------------------------------------
是这样的.当我们要引用结构体里面的元素时,我们可以用圆点运算符,也可以用指针.
->就是指针的引用方式.
----------------解决方案--------------------------------------------------------

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








# 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);
}



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

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


----------------解决方案--------------------------------------------------------
以下是引用oclassic在2007-4-21 18:49:07的发言:
-> 这个符号什么意思?

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

她用的是VC


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