永发信息网

c语言 定义一个函数输出链表数据。

答案:3  悬赏:10  手机版
解决时间 2021-03-11 06:13
struct node
{int data;
struct node *next;
};
struct node *create()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->next=0;
return p;
}
main()
{

struct node *head,*q,*p,*t;
int i;
int x;
head=create();
for(i=1;i<=10;i++)
{
printf("please input data:");
scanf("%d",&x);
q=create();
q->data=x;
if(i==1)
{
head->next=q;
p=q;
}
else
{p->next=q;
p=q;}
}

}
请问这个输出函数该怎么写啊
最佳答案
#include<stdio.h>
#include <stdlib.h>
struct node
{int data;
struct node *next;
};
struct node *create()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->next=0;
return p;
}
void show(node  *head)
{
head=head->next;
while(head!=NULL)
{
printf("%d\n",head->data);
head=head->next;
}
}
void main()
{

struct node *head,*q,*p,*t;
int i;
int x;
head=create();
for(i=1;i<=10;i++)
{
printf("please input data:");
scanf("%d",&x);
q=create();
q->data=x;
if(i==1)
{
head->next=q;
p=q;
}
else
{p->next=q;
p=q;}
}
show(head);


}
全部回答
#include  #include  typedef struct poi {     char num[10];     char name[20];     int age;     struct poi *next; }pointer; pointer* head,* tail; pointer* newnode() {     pointer* u=(pointer*) malloc(sizeof(pointer));//分配一个动态地址。这个函数要记下里。同时要开cstdlib头文件      u->next=null; } int main() {     head=newnode();//创建一个新的指针。      tail=head;     for (int i=1;i<=5;i++)         {             tail->next=newnode();             tail=tail->next;             //你可以输入数据然后存入指针中。比如scanf("%d",&tail->age);然后给tail->num什么的赋值。          }     pointer* u=head->next;     while (u!=null)         {             //输出什么东西。。。比如printf("%d\n",u->age);              u=u->next;         }     return 0; }
#include <stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; }; struct node *create() {     struct node *p;     p=(struct node *)malloc(sizeof(struct node));     p->next=0;     return p; } void list_print(struct node *head) {     struct node *pos;     for (pos = head->next; pos != NULL; pos = pos->next) {         printf("%d\n", pos->data);     }     return; } int main() {     struct node *head,*q,*p;     int i;     int x;     head=create();     for(i = 1; i <= 10; i++) {         printf("please input data[%d]:", i);         scanf("%d",&x);         q=create();         q->data=x;         if(i==1) {             head->next=q;             p=q;         } else {             p->next=q;             p=q;         }     }     list_print(head);  //这个样子就行了     return 0; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我初当出纳,我们原来现金支票取钱时候用途都
汽车喷漆承包工资要怎样讲合理
千足金与万足金哪个好?
6+3+5等于18158 3+5+4等于152015 7+6+8等于42
当代中学生报化学物质的量浓度测试卷第八期的
哈哈麻辣烫二部地址在哪,我要去那里办事
为什么我一想到他会心痛,会哭??
Mary is often praised for her hard work.为
混凝土车辆安全职责
济南学计算机专业有什么好学校(3+2)
大排档在什么地方啊,我要过去处理事情
为抵御爆吧挖坟危害,本吧吧主已暂时放出贴吧
【物质的量浓度计算公式】关于物质的量浓度的
理光DX2430c制版是空白如何处理?请求答案!
缺钾的人,能吃什么,不能吃什么
推荐资讯
adjustment的动词形式
谁可以帮我把我爱梅梅四个字为开头做成诗句谢
智尚汽车美容装饰地址在什么地方,想过去办事
h3c er3100与h3cer5200硬件都差不多为啥差价
路虎汽车多少钱一辆
36+64-36+64写出相等的式子
龙泉洼我想知道这个在什么地方
网站里出现下面这句话,是空间问题还是域名问
已到退休年龄被解雇,公司有无赔偿
根据基金的分类标准,正确的是()。Ⅰ.80%以上
在我国,一般年度投资总规模是否合适主要以(
如图,已知三角形ABC中,∠A=30,AB=5,AC=4,四边
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?