求广义表深度

试按表头、表尾的分析方法编写求广义表的深度的递归程序。

输入一串以‘(’开始,以(’结束的字符串,并且输入的左右括号必须匹配,如:(),(())……

分别输出按表头、表尾分析方法求广义表深度的结果,每个结果占一行。

((a,b,(c,(d,e),f)),g)

4

4

 
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. struct Node  
  5. {  
  6.     int judge;  
  7.     struct Node *before;  
  8.     struct Node *next;  
  9.     union  
  10.     {  
  11.         struct Node *down;  
  12.         char data;  
  13.     };  
  14. };  
  15.   
  16. void createGeneralNode (struct Node *pre);  
  17. void createGeneralList (struct Node *head);  
  18. void run ();  
  19. int getDepth (struct Node *node);  
  20.   
  21. int main()  
  22. {  
  23.     run ();  
  24.     return 0;  
  25. }  
  26.   
  27. void createGeneralNode (struct Node *pre)  
  28. {  
  29.     char s,ss=0;  
  30.     struct Node *cur;  
  31.     if ((s=getchar())==',')  
  32.     {  
  33.         ss=s;  
  34.         s=getchar();  
  35.     }  
  36.     if (s>='a'&&s<='z')  
  37.     {  
  38.         cur =(struct Node *)malloc (sizeof(struct Node));  
  39.         if (ss==',')  
  40.         {  
  41.             pre->next=cur;  
  42.         }  
  43.         else  
  44.         {  
  45.             pre->down=cur;  
  46.         }  
  47.         cur->before=pre;  
  48.         cur->data=s;  
  49.         cur->next=NULL;  
  50.         cur->judge=1;  
  51.         createGeneralNode (cur);  
  52.     }  
  53.     else  
  54.     {  
  55.         if (s=='(')  
  56.         {  
  57.             cur =(struct Node *)malloc (sizeof(struct Node));  
  58.             if (ss==',')  
  59.             {  
  60.                 pre->next=cur;  
  61.             }  
  62.             else  
  63.             {  
  64.                 pre->down=cur;  
  65.             }  
  66.             cur->before=pre;  
  67.             cur->down=NULL;  
  68.             cur->next=NULL;  
  69.             cur->judge=0;  
  70.             createGeneralNode (cur);  
  71.         }  
  72.         else  
  73.         {  
  74.             if (s==')')  
  75.             {  
  76.                 while (pre->judge||pre->next)  
  77.                 {  
  78.                     pre=pre->before;  
  79.                 }  
  80.                 createGeneralNode (pre);  
  81.             }  
  82.         }  
  83.     }  
  84. }  
  85.   
  86. void createGeneralList (struct Node *head)  
  87. {  
  88.     head->judge=0;  
  89.     head->down=NULL;  
  90.     head->next=NULL;  
  91.     head->before=NULL;  
  92.     createGeneralNode(head);  
  93. }  
  94.   
  95. void run ()  
  96. {  
  97.     struct Node head;  
  98.     int depth;  
  99.     createGeneralList(&head);  
  100.     depth=getDepth(head.down);  
  101.     printf ("%d\n%d",depth,depth);  
  102. }  
  103.   
  104. int getDepth (struct Node *node)  
  105. {  
  106.     int max=0,depth;  
  107.     struct Node *p;  
  108.     for (p=node->next;p;p=p->next)  
  109.     {  
  110.         if (p->judge)  
  111.         {  
  112.             depth=0;  
  113.         }  
  114.         else  
  115.         {  
  116.             if (p->down)  
  117.             {  
  118.                 depth=getDepth(p->down)+1;  
  119.             }  
  120.             else  
  121.             {  
  122.                 depth=1;  
  123.             }  
  124.         }  
  125.         max=depth>max?depth:max;  
  126.     }  
  127.     if (node->judge)  
  128.     {  
  129.         depth=0;  
  130.     }  
  131.     else  
  132.     {  
  133.         if (node->down)  
  134.         {  
  135.             depth=getDepth(node->down)+1;  
  136.         }  
  137.         else  
  138.         {  
  139.             depth=1;  
  140.         }  
  141.     }  
  142.     return max=depth>max?depth:max;  
  143. }  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值