#include "stdio.h"
#include "malloc.h"
#define len sizeof(struct node)
struct node{
int data;
struct node *next;
};
struct node *scanff()
{
struct node *p1;
struct node *head;
int n=0;
struct node *p2;
p1=p2=(struct node *)malloc(len);
scanf("%d",&p1->data);
head=0;
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(len);
scanf("%d",&p1->data);
}
p2->next=0;
return(head);
}
void print(struct node *head)
{
struct node *p;
p=head;
if(head!=0)
do{
printf("%d ",p->data);
p=p->next;
}while(p!=0);
}
void main()
{
struct node *head;
head=scanff();
print(head);
}
请各位帮忙看一下这个链表输出程序出现了什么问题,当我输入一些数的时候它没有输出!
一个简单的链表输出
答案:3 悬赏:50
解决时间 2021-03-07 09:04
- 提问者网友:心裂
- 2021-03-06 16:33
最佳答案
- 二级知识专家网友:心与口不同
- 2021-03-06 18:12
怎么会没有输出呢?因为你并没有结束程序
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(len);
scanf("%d",&p1->data);
}
注意这个while循环,是当p1->data=0时退出,因此你必须输入0结束输入才会有输出我改了一下程序这样更直观些
#include "stdio.h"
#include "malloc.h"
#define len sizeof(struct node)
struct node{
int data;
struct node *next;
};
struct node *scanff()
{
struct node *p1;
struct node *head;
int n=0;
struct node *p2;
p1=p2=(struct node *)malloc(len);
printf("请连续输入整数 输入0停止\n");
scanf("%d",&p1->data);
head=0;
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(len);
scanf("%d",&p1->data);
}
p2->next=0;
return (head);
}
void print(struct node *head)
{
struct node *p;
p=head;
if(head!=0)
do{
printf("%d \n",p->data);
p=p->next;
}while(p!=0);
}
void main()
{
struct node *head;
head=scanff();
printf("你所输入的整数如下:\n");
print(head);
}
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(len);
scanf("%d",&p1->data);
}
注意这个while循环,是当p1->data=0时退出,因此你必须输入0结束输入才会有输出我改了一下程序这样更直观些
#include "stdio.h"
#include "malloc.h"
#define len sizeof(struct node)
struct node{
int data;
struct node *next;
};
struct node *scanff()
{
struct node *p1;
struct node *head;
int n=0;
struct node *p2;
p1=p2=(struct node *)malloc(len);
printf("请连续输入整数 输入0停止\n");
scanf("%d",&p1->data);
head=0;
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct node *)malloc(len);
scanf("%d",&p1->data);
}
p2->next=0;
return (head);
}
void print(struct node *head)
{
struct node *p;
p=head;
if(head!=0)
do{
printf("%d \n",p->data);
p=p->next;
}while(p!=0);
}
void main()
{
struct node *head;
head=scanff();
printf("你所输入的整数如下:\n");
print(head);
}
全部回答
- 1楼网友:白日梦制造商
- 2021-03-06 20:22
除程序结束后,没有释放内存之外! 没有问题
至于链表输出的问题,我想应该是你用标准的输入函数scanf,
只要你输入的格式有误就会不打印, 能不能给定些输入的特例
- 2楼网友:浪者不回头
- 2021-03-06 18:59
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
void main()
{ link ptr,head,tail;
int num,i;
tail=(link)malloc(sizeof(node));
tail->next=null;
ptr=tail;
printf("\nplease input 5 data==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
head=(link)malloc(sizeof(node));
head->next=ptr;
ptr=head;
}
ptr=ptr->next;
while(ptr!=null)
{ printf("the value is ==>%d\n",ptr->data);
ptr=ptr->next;
}}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯