尾插法建立单链表及输出

作者在 2008-05-06 21:58:11 发布以下内容
#include<stdio.h>
#include<stdlib.h>
typedef struct node{int data;struct node *link;}LNode;
LNode *createtail()
{
 LNode *s,*h,*r;
 int x,tag;
 printf("结束符号:");
 scanf("%d",&tag);
 h=(LNode *)malloc(sizeof(LNode));
 r=h;
 printf("输入数据:");
 scanf("%d",&x);
 while(x!=tag)
 {
  s=(LNode *)malloc(sizeof(LNode));
  s->data=x;
  r->link=s;
  r=s;
  scanf("%d",&x);
 }
 r->link=NULL;
 return h;
}
void myprint(LNode *h)
{
 LNode *p;
 p=h->link;
 if(p==NULL)
  printf("空链\n");
 else
  do
  {
   printf("%5d",p->data);
   p=p->link;
  }
  while(p!=NULL);
 printf("\n");
}
main()
{
 LNode *h;
 h=createtail();
 myprint(h);
}
经典例题 | 阅读 4786 次
文章评论,共0条
游客请输入验证码