前插法建立单链表及输出

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