问题 L: 高程08-01:编写函数,向链表的尾部插入数据建立一个链表。

问题 L: 高程08-01:编写函数,向链表的尾部插入数据建立一个链表。

时间限制: 1 Sec  内存限制: 128 MB
提交: 2621  解决: 1699
[提交][状态][讨论版][命题人:]

题目描述

补充完成下面的 insert_tail 函数,向一个链表的尾部插入一个数

#include<stdio.h>

#include<stdlib.h>
 struct node
 {
  int x;
  struct node *next;
 };

void insert_tail(struct node *H,int p)
 {//H带有头结点。头结点后面的结点是第一个数据结点
  
 }

void print(struct node *H)
 {
  struct node *p;
  p=H->next;
  while(p!=0)
  {
   printf("%d ",p->x);
   p=p->next;
  }
 }

void freeheadlist(struct node *h)
{
struct node *t;
h=h->next;
while(h)
{
t=h->next;
free(h);
h=t;
}
}
 int main()
 {
  struct node H;
  int x;
  int i; 
 
  H.next=0; 
 
  for(i=1;i<=5;i++)
  {
   scanf("%d",&x);
  
   insert_tail(&H,x) ;
  
  } 
 
  print(&H);
  freeheadlist(&H);
  return 0;
 }

输入

输入5个整数建立链表,中间以空格分隔

输出

输出所建立链表中的数据,数据之间用一个空格隔开。

样例输入

1 2 3 4 5

样例输出

1 2 3 4 5

提示

[提交][状态]