C + +: implementation of generalized tables

Source: Internet
Author: User


generallist.hpp             #pragma  once# Include<iostream> #include <string>using namespace std;enum NodeType{                //node Type head_type,value_type,sub_ type,};struct generallistnode{            // Structural body nodetype _type; of nodes generallistnode* _next;union{char _value; generallistnode* _sublink;}; Generallistnode (nodetype type = head_type, char value =  '): _type (Type),  _next (NULL) {if  (type == value_type) {_value = value;} else if  (Type == sub_type) {_sublink = null;}}}; Class generallist{public:generallist (CONST&NBSP;CHAR*&NBSP;STR)  :_link (NULL) {       //Constructor _creategenerallist (_LINK,&NBSP;STR);} Generallist (Const genERALLIST&AMP;&NBSP;GL) {             //copy constructor _ Link = _copy (Gl._link);} Generallist& operator= (CONST&NBSP;GENERALLIST&AMP;&NBSP;GL) {   //overloaded function if  (this &NBSP;!=&NBSP;&AMP;GL) {        _delete (_link);  _Copy (Gl._link);} Else    return *this;}   ~generallist () {                                   //destructor  _delete (_link);} Void print ()                                      //display function {_print (_link); Cout << endl;} Int size () {                         //calculates the size of a generalized table, The outermost element number generallistnode* begin = _link;int size = 0;if  (Begin->_type  == head_type) return 1;else{while  (begin) {Size++;begin = begin->_next;}} Return size;} Int depth () {//Calculate the depth of the generalized table return _depth (_link);} Protected:const char* _creategenerallist (GENERALLISTNODE*&AMP;&NBSP;LINK,&NBSP;CONST&NBSP;CHAR*STR) { The implementation of the   //constructor generallistnode* begin = null;char* comparestr =  "()";      //defines whether the check is a () string while  (Isspace (*STR))            //Check if there is a space str++;if  (*str !=  ' (') {             //Check if the input is legal cout <<  "input illeagle!"  << endl;exit (-1);} if  (strcmp (STR,&NBSP;COMPARESTR)  == 0){                     //to determine if Head_typegenerallistnode* head = new generallistnode (HEAD_TYPE);    //create node Link = head;begin = head;} Str++;          //begins after the first opening parenthesis//determines the type, creating a node while  for it (*str  !=  ' + ') {if  (*str ==  ', ') str++;while  (Isspace (*STR))            //Check if there is a space str++;if  (*str ==  ' (')     {          //if it's sub_typegenerallistnode* subnode = . New generallistnode (Sub_type);if  (begin == null) {begin = subnode;link =  begin; str = _creategenerallist (BEGIN-&GT;_SUBLINK,&NBSP;STR);    //recursive creation of a child table} Else{begin->_next = subnode;begin = begin->_neXt;str = _creategenerallist (BEGIN-&GT;_SUBLINK,&NBSP;STR);}} else if  (*str ==  ') '  && *++str !=  ') {                //recursive end condition Return str;} else if  (*str !=  ') '  && *str !=  ') {          //if it's value_typegenerallistnode* valuenode = new . Generallistnode (VALUE_TYPE,&NBSP;*STR);if  (begin == null) {Begin = valuenode;link  = begin;} Else{begin->_next = valuenode;begin = begin->_next;}} str++;}} Generallistnode* _copy (Generallistnode* link) {          Implementation of       //Copy construction generallistnode* _new = null;if  (link) {_new  = new generallistnode (link->_type);               //Create head node _new->_next = link->_next;switch  (_new- >_type) {case head_type:                                   _new->_value = link->_value;break;case value_type:_new->_value =  link->_value;break;case sub_type:_new->_sublink = link->_sublink;break;default: break;} _new->_next = _copy (Link->_next);                   //Create the remaining nodes recursively}return _new;} Generallistnode*  _delete (generallistnode* link) {     //destructors are implemented from right to left, From bottom to top generallistnode* begin = link;if  (begin) {switch  (begin->_type) {case HEAD_ Type:break;case value_type:_delete (BEGIN-&GT;_next); Break;case sub_type:_delete (Begin->_next); _delete (begin->_sublink); break;default:break;} Delete begin;} Return begin;} Void _print (Generallistnode* link) {           Implementation of          //display function generallistnode* begin = link; cout <<  "(";while  (begin) {if  (Begin->_type == head_type) {cout < <  ")";} else if  (Begin->_type == value_type) {cout << begin->_value;if  ( Begin->_next) {cout <<  ",";} else{cout <<  ")";}} else if  (Begin->_type == sub_type) {_print (Begin->_sublink);if  (Begin->_next) {cout <<  ",";}} Begin = begin->_next;}} Int _depth (Generallistnode* link) {              //depth calculation, find the maximum number of sub-tables Generallistnode* begin = link;int depth = 0;int max = 0;while  (BEGIN) { if  (Begin->_type == head_type) return 1;else if  (begin->_type ==  Value_type) {Begin = begin->_next;} else if  (Begin->_type == sub_type) {depth = _depth (begin->_subLink);   (Depth > max) Max = depth;begin = begin->_next;}} return max+1;} private:generallistnode* _link;};


This article is from the "Molova" blog, make sure to keep this source http://molova.blog.51cto.com/10594266/1711332

C + +: implementation of generalized tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.