Chinaunix首页 | 论坛 | 博客
  • 博客访问: 260173
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-03-15 16:47:37


  1. #pragma once

  2. enum Type
  3. {
  4.  HEAD,
  5.  SUB,
  6.  VALUE
  7. };

  8. struct GeneralListNode
  9. {
  10.   GeneralListNode(Type type)
  11.      :_type(type)
  12.      ,_next(NULL)
  13.      ,_SubLink(NULL)
  14.   {}
  15.   GeneralListNode(Type type,char s)
  16.      :_type(type)
  17.      ,_next(NULL)
  18.       ,_value(s)
  19.   {}
  20.  Type _type;
  21.  GeneralListNode * _next;
  22.  
  23.  union
  24.  {
  25.     char _value;
  26.     GeneralListNode *_SubLink;
  27.  };
  28. };

  29. class GeneralList
  30. {
  31. public:
  32.     GeneralList()
  33.         :_head(NULL)
  34.     {}
  35.     GeneralList(char *s)
  36.     {
  37.         _head = _CreatGeneralList(s);
  38.     }
  39.     void Print()
  40.     {
  41.         _Print(_head);
  42.         cout<<endl;
  43.     }
  44.     size_t size()
  45.     {
  46.         return _size(_head);
  47.     }
  48.     GeneralList(const GeneralList &g)
  49.     {
  50.         _head = _Copy(g._head);

  51.     }
  52. //     GeneralList operator=(const GeneralList &g)
  53. //     {
  54. //         if(this != &g)
  55. //         {
  56. //             this->_Destroy(_head);
  57. //             _head = _Copy(g._head);
  58. //         }
  59. //         return *this;
  60. //     }
  61.     GeneralList& operator=(GeneralList g)
  62.     {
  63.         swap(_head,g._head);
  64.         return *this;
  65.     }
  66.     ~GeneralList()
  67.     {
  68.         _Destroy(_head);
  69.     }
  70.     size_t Depth()
  71.     {
  72.         return _Depth(_head);
  73.     }
  74. protected:
  75.     size_t _Depth(GeneralListNode *head)
  76.     {
  77.         GeneralListNode *cur = head;
  78.         size_t dep = 1;
  79.         while(cur)
  80.         {
  81.             if(cur->_type == SUB)
  82.             {
  83.                 size_t SubDep = _Depth(cur->_SubLink);
  84.                 if (SubDep+1 > dep)
  85.                 {
  86.                     dep = SubDep + 1;
  87.                 }
  88.             }
  89.             cur = cur->_next;
  90.         }
  91.         return dep;
  92.     }
  93.     GeneralListNode * _Copy(GeneralListNode* head)
  94.     {
  95.         assert(head);
  96.         GeneralListNode *newHead = new GeneralListNode(HEAD);
  97.         GeneralListNode *cur =head->_next;
  98.         GeneralListNode *newCur = newHead;

  99.         while(cur)
  100.         {
  101.             if(cur->_type == VALUE)
  102.             {
  103.                 newCur->_next = new GeneralListNode(VALUE,cur->_value);
  104.                 newCur = newCur->_next;
  105.             }
  106.             else if(cur->_type == SUB)
  107.             {
  108.                 newCur->_next = new GeneralListNode(SUB);
  109.                 newCur = newCur->_next;

  110.                 newCur->_SubLink = _Copy(cur->_SubLink);
  111.             }
  112.             cur =cur->_next;
  113.         }
  114.         return newHead;
  115.     }
  116.     void _Destroy(GeneralListNode *head)
  117.     {
  118.         GeneralListNode* cur = head;
  119.         
  120.         while(cur)
  121.         {

  122.             GeneralListNode *del = cur;
  123.             cur = cur->_next;
  124.             
  125.             if(del->_type == SUB)
  126.             {
  127.                 _Destroy(del->_SubLink);
  128.             }
  129.             delete del;
  130.         }
  131.     }
  132.     size_t _size(GeneralListNode *head)
  133.     {
  134.         GeneralListNode* cur = head;
  135.         size_t size = 0;

  136.         while(cur)
  137.         {
  138.             if(cur->_type == VALUE)
  139.             {
  140.                 ++size;
  141.             }
  142.             else if(cur->_type == SUB)
  143.             {
  144.                 size += _size(cur->_SubLink);
  145.             }
  146.             cur = cur->_next;
  147.         }
  148.         return size;
  149.     }
  150.     void _Print(GeneralListNode *head)
  151.     {
  152.         GeneralListNode *cur = head;

  153.         while(cur)
  154.         {
  155.             if(cur->_type == HEAD)
  156.             {
  157.                 cout<<"(";
  158.             }
  159.             else if(cur->_type == VALUE)
  160.             {
  161.                 cout<<cur->_value;
  162.                 if(cur->_next)
  163.                 {
  164.                     cout<<",";
  165.                 }
  166.             }
  167.             else
  168.             {
  169.                 _Print(cur->_SubLink);
  170.                 if(cur->_next)
  171.                 {
  172.                     cout<<")";
  173.                 }
  174.             }
  175.             cur = cur->_next;
  176.         }
  177.         cout<<")";
  178.     }
  179.     bool IsValue(char ch)
  180.     {
  181.         if((ch >= '0' && ch <= '9')
  182.             ||(ch >= 'a' && ch <= 'z')
  183.             ||(ch >= 'A' && ch <= 'Z'))
  184.         {
  185.             return true;
  186.         }
  187.         return false;
  188.     }
  189.     GeneralListNode* _CreatGeneralList(char *&s)
  190.     {
  191.         assert(*s == '(');
  192.         GeneralListNode *head = new GeneralListNode(HEAD);
  193.         ++s;
  194.         GeneralListNode *cur = head;

  195.         while(*s)
  196.         {
  197.             if(*s == '(')
  198.             {
  199.                 GeneralListNode *SubNode = new GeneralListNode(SUB);

  200.                 cur->_next = SubNode;
  201.                 cur = cur->_next;

  202.                  SubNode->_SubLink = _CreatGeneralList(s);
  203.             }
  204.             else if (*s == ')')
  205.             {
  206.                 ++s;
  207.                 break;
  208.             }
  209.             else if(IsValue(*s))
  210.             {
  211.                 GeneralListNode *ValueNode = new GeneralListNode(VALUE,*s);

  212.                 cur->_next = ValueNode;
  213.                 cur = cur->_next;

  214.                 ++s;
  215.             }
  216.             else
  217.             {
  218.                 ++s;
  219.             }
  220.         }
  221.         return head;
  222.     }
  223. protected:
  224.     GeneralListNode * _head;
  225. };
测试:

  1. #include "GeneralList.h"

  2. void test()
  3. {
  4.     GeneralList g1("()");
  5.     GeneralList g2("(a,b)");
  6.     GeneralList g3("(a,b,(c,d))");
  7.     GeneralList g4("(a,b,(c,d),(e,(f),h))");
  8.     GeneralList g5;
  9.     g5 = g4;

  10.    g4.Print();
  11.    cout<<g4.size()<<endl;
  12.    cout<<g4.Depth()<<endl;
  13.    g5.Print();
  14. }


阅读(966) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~