Generalized Table of C + + data structure

Source: Internet
Author: User

Recently learned the generalized table, we know that the generalized table is also a linear table, and the name implies that the generalized table is more than one table, below to raise a chestnut:

A= ()

b= (1, 2,3)

C= (1, 2, 3, (A, B, c))

D= (1, 2, 3, (A, (b,c), D), 4)

The above a,b,c,d are generalized tables, but the depth is not the same, that is, the parentheses of the logarithm is not the same, a is a special generalized table, that is, empty table. b There are three elements, c there are 6 elements, including a sub-table (A,B,C), C is the same, just a layer of sub-table. This can be summed up as a sentence: table

This may not be intuitive to see, the following is a generalized table C as an example to see its structure diagram:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/40/wKiom1cXaDXAgQb6AAAlQ9FgaVQ092.png "title=" Image.png "alt=" Wkiom1cxadxagqb6aaalq9fgavq092.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

(The picture is a little ugly, do not spit on me)

Whenever you encounter a front parenthesis, you create a child table until you meet the closing brackets.


So how to create a generalized table, in the creation of the node structure, we have to consider the type of each node, it may be the head knot

Point, it could be a child table node, or it might be a normal value node, so when you construct a node, you define a ternary body,

So table, we can use recursive method to solve the problem of generalized table, each child table can be recursive to a sub-problem, you can

It's easy to solve the problem.


The following is the implementation-specific code:


Construct a ternary structure first:

Enum Type{head,value,sub,};template<class t>struct Generalizednode{type _type; generalizednode<t>* _next;union{char _value;   generalizednode<t>* _sublink; The head node of the child table};p ublic:generalizednode (Type type = Head,char value = ' _value '): _type (Type), _next (NULL) {if (type = = value) {= Value;} else if (type = = SUB) {_sublink = NULL;}};

Below to construct a generalized table class

Template<class t>class generalizedlist{public:generalizedlist (): _head (New GeneralizedNode <T> (HEAD) {}generalizedlist (const char* str) {_head=_createlist (str);} Generalizedlist (const generalizedlist& g)     //copy Construction {_head = Copy (g . _head;)}size_t size () {return size (_head);} Size_t depth () {return depth (_head);} Void print () {print (_head);} Protected:generalizednode<t>* _createlist (CONST&NBSP;CHAR*&AMP;&NBSP;STR) {assert (str & & *str ==  ' ('); ++str; Generalizednode<t>* head = new generalizednode<t> (HEAD,NULL); generalizednode<t>* cur = head;while  (*STR) {if  (_IsValue (*str)) {Cur->_next  = new GeneralizedNode<T> (VALUE,*STR); cur = cur->_next;str++;} else if  (*str ==  ' (') {generalizednode<t>* newnode= new  Generalizednode<t> (Sub);         //adds a child table node to the list cur->_next = newnode;cur  = cur->_next;cur->_sublink = _createlist (str); str++;//Create a sub-table node recursively}else if  (*str ==  ') ')                 //indicates that a table has ended {str++;return head;} else{str++;     //does not need to handle the situation}}assert (false); return head;} Size_t depth (Generalizednode<t>* head) {generalizednode<t>* cur = head; size_t depth = 1;while  (cur) {if  (cur->_type == sub) {size_t subdepth  = depth (Cur->_sublink);if  (subdepth+1 > depth) {depth=subdepth;//Save large depth}}cur  = cur->_next;} Return depth;} Size_t size (generalizednode<t>* head) {generalizednode<t>* cur = head;size _t count = 0;while  (cur) {if  (cur->_type ! = sub) {count++;} Else{count += size (Cur->_sublink);} Cur = cur->_next;} Return count;} Void print (generalizednode<t>* head) {Generalizednode<t>* cur = head;while   (cur) {             if  (cur->_ Type == head) {cout <<  ' (';} else if  (Cur->_type == value) {cout << cur->_value ;if  ( Cur->_next != null) {cout <<  ', '  ;}} else if  (cur->_type == sub) {Print (Cur->_sublink);if  (cur->_next !=  null) {cout <<  ', ';}} Cur = cur->_next;} cout <<  ') ';} Bool _isvalue (char ch)    //Check if it is a legitimate value {if  ((ch >=  ' 0 ' &&ch<= ' 9 ')  | |   (ch>= ' a ' &&ch<= ' z ')  | |   (ch>= ' A ' &&ch <=  ' Z ')) {RETurn true;} Return false;} protected:generalizednode<t>* _head;};

The test code is given below:

#include "Generalized.h" void Test () {char* ch = "(A,b,c, (), C)"; generalizedlist<char> GL1 (CH); Gl1.print () cout << endl;cout << "Generalized table depth:" << gl1.depth () < < endl;cout << "Generalized table size:" << gl1. Size () << Endl;} int main () {Test (); GetChar (); return 0;}

Operation Result:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/40/wKiom1cXayDxqM9wAAAFVrKt1gA365.png "title=" Qq20160420194132.png "alt=" Wkiom1cxaydxqm9waaafvrkt1ga365.png "/>


The above is the C + + implementation of the generalized table method, there may still exist some problems, I hope that we can correct them, together to promote learning.

This article from "Fu da Xin" blog, reproduced please contact the author!

Generalized Table of C + + data structure

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.