永发信息网

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

答案:3  悬赏:10  手机版
解决时间 2021-03-11 06:13
  • 提问者网友:鼻尖触碰
  • 2021-03-10 07:36
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;}
}

}
请问这个输出函数该怎么写啊
最佳答案
  • 五星知识达人网友:山河有幸埋战骨
  • 2021-03-10 08:01
#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);


}
全部回答
  • 1楼网友:七十二街
  • 2021-03-10 09:10
#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; }
  • 2楼网友:行雁书
  • 2021-03-10 08:16
#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; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯