C + + implementation of generalized tables

Source: Internet
Author: User




#include <iostream> #include <vector> #include <assert.h>using namespace std;// Enumerates the types of enum type{head,//head nodes that may occur for each node in a generalized table value,//value node type sub//child table type};//the struct type of the node that defines the generalized table struct  generalizednode{type _type;//type generalizednode* _next;//point to the next node in the same layer//a node is either a value or a child table, so use the Union Union{char  _value; generalizednode* _sublink;//pointer to a child table};//the implementation of the node's constructor Generalizednode (type type=head,char value=0): _ Type (type), _next (NULL) {if (_type==value) {_value=value;} Else if (_type==sub) {_sublink=null;}}; class generalist{public://parameterless Constructor Generalist (): _head (NULL) {}//parameter constructor generalist (CONST&NBSP;CHAR*&NBSP;STR) : _head (NULL) {_head=_creategeneralist (str);} destructor ~generalist () { _destory (_head);} Public:void print ()//Print generalized table {_print (_head); Cout<<endl;} Size_t depth ()//For the depth of the generalized table {return _depth (_head);} Size_t size ()//For the size of the generalized table {  return _size (_head);} Protected:generalizednode* _creategeneralist (CONST&NBSP;CHAR*&AMP;&NBSP;STR);//Create a generalized tableVoid _destory (Generalizednode*head); Bool _isvalue (const char ch);//Determine if the value node is a legitimate type size_t  _size (Generalizednode* head);//size_t _depth (Generalizednode*head);//void _Print ( Generalizednode* head);p rotected:  generalizednode* _head;//to hold the address of a generalized header node};//create a generalized table   Generalizednode*  generalist::_creategeneralist (CONST&NBSP;CHAR*&AMP;&NBSP;STR)// Prevents exiting the loop and then returns to the beginning of the position to rebuild {assert (str&&*str== '); str++;//to move the generalized table, which is equivalent to skipping "(" generalizednode *head=new  generalizednode (HEAD); Generalizednode *cur=head;while (*str!= ') {//If the value is encountered if (_isvalue (*STR)) {     cur- >_next=new generalizednode (VALUE,*STR);  cur=cur->_next; str++;} If you encounter a child table Else if (*str== ' (') {Cur->_next=new generalizednode (sub); Cur=cur->_next;cur->_sublink  =_creategeneralist (str);} The end of a generalized table else if (*str== ') ') {str++;return head;} Other characters such as spaces skip elsestr++;} ASSERT (false); return head;} Determines whether the current value is valid for a generalized tableValue Bool generalist::_isvalue (const char ch) {if ((ch>= ' 0 ' &&ch<= ' 9 ') | | (ch>= ' A ' &&ch<= ' z ') | | (ch>= ' A ' &&ch<= ' Z ')) Return true;elsereturn false;} Destroy generalized table Void generalist::_destory (generalizednode*head) {generalizednode* cur=head;while (Cur!=NULL) { generalizednode* del=cur;cur=cur->_next;//encounters a child table recursively destroys if (del->_type ==sub) {     _destory (del->_sublink );} Delete del;head=null;}} The size of the generalized table Size_t generalist::_size (generalizednode* head) {generalizednode* cur=head;size_t  Size=0;while (cur!=null) {if (cur->_type ==value) {++size;} Else if (cur->_type ==sub) {     size+=_size (cur->_sublink );// Encountered a child table for recursive}cur=cur->_next ;} Return size;} To find the depth size_t generalist::_depth (generalizednode*head) of the generalized table {size_t depth=1; Generalizednode* cur=head;while (cur!=null) {if (cur->_type ==sub) {&NBSP;SIZE_T&NBSP;SUBLINKDEpth= _depth (cur->_sublink );  if (depth<sublinkdepth+1)  { depth=sublinkdepth+1;  } }cur=cur->_next;   }return depth;} Print Generalized table Void generalist::_print (generalizednode* head) {generalizednode* cur=head;while (Cur!=NULL {if (cur->_type ==head) {cout<< "(";} Else if (cur->_type ==value) { cout<<cur->_value; if (Cur->_next !=NULL)  { cout<< ",";  }}else if (Cur->_type ==sub) {cout<< "("; _print (cur->_ sublink ); if (cur->_next!=null ) {cout<< ",";}} cur=cur->_next ;} cout<< ")";} Void test () {generalist g1= ("(A, B, (C, (G)), D)"); G1. print  ();cout<< "G1 depth is:" <<g1. depth  () <<endl;cout<< "G1 size is:" <<g1. size  () <<endl;} Int main () {test (); GetChar (); return 0;}




C + + implementation of generalized tables

Related Article

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.