现在的位置: 首页 > 综合 > 正文

链表的输入与输出

2017年12月19日 ⁄ 综合 ⁄ 共 841字 ⁄ 字号 评论关闭

 

  1. #include"stdio.h"  
  2. #include"malloc.h"  
  3. typedef struct Node  
  4. {   char data;  
  5.     struct Node *next;  
  6. }Node,*Linklist;  
  7.  
  8. void Input(Linklist &p)//输入函数  
  9. {   Node *r,*s;  
  10.     r=p;  
  11.     char c;  
  12.     int flag=1;  
  13.     while(flag)//标记  
  14.     {   c=getchar();//读入字符  
  15.         if(c!='#')  
  16.         {  
  17.             s=(Node *)malloc(sizeof(Node));//动态分配空间  
  18.             s->data=c;//将读入的字符存储在s中  
  19.             r->next=s;  
  20.             r=s;  
  21.         }  
  22.         else 
  23.         {   flag=0;//当输入的字符为#时结束循环  
  24.             r->next=NULL;//将尾指针设置为空  
  25.         }  
  26.     }  
  27. }  
  28.  
  29. void Output(Linklist &p)//输出函数  
  30. {  
  31.     Linklist L;  
  32.     L=p->next;  
  33.     while(L)//当L不为空  
  34.     {  
  35.         printf("%c",L->data);  
  36.         L=L->next;  
  37.     }  
  38. }  
  39. void main()  
  40. {   Linklist p;//定义变量  
  41.     p=(Node *)malloc(sizeof(Node));//初始化  
  42.     Input(p);//输入  
  43.     Output(p);//输出  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/817857

【上篇】
【下篇】

抱歉!评论已关闭.