Generalized table, relative to the linked list is more complex, relative to the tree is simpler .... It is a good choice to transition the order table and the tree.
Needless to say, a word does not stick to the code.
/** File Description: Generalized table related declarations and definitions * Author: High Minor * Date: 2016-12-12* integrated development environment: Microsoft Visual Studio */#ifndef __generallist_h__#define __ Generallist_h__#include<assert.h>enum type{head,sub,value};struct generallistnode{type Type;union{char VALUE ; Generallistnode *sublink;}; Generallistnode *next; Generallistnode (type t,const char v = 0): type (t), next (0) {if (t = = HEAD | | t ==value) {VALUE = V;} else if (t = = SUB) {sublink = 0;} else {assert (false);}}}; Class Generallist{typedef Generallistnode node;public://constructor generallist (const char *s) {_head = _createlist (s);} Copy construction generallist (const generallist &g) {_head = _copy (g._head);} Assignment operator overload generallist &operator= (const generallist &g) {if (_head!=g._head) {////Do not self-assign to yourself generallist temp (g); Swap (_head,temp._head);}} destructor ~generallist () {if (_head!=null) {_destory (_head); _head = NULL;}} public://Print Generalized table void Print () {_print (_head); Cout<<endl;} Generalized table Data number size_t Size () {return _size (_head);} Generalized table Depth size_t Depth () {return _depth (_head);} protected://Copy Generalized Table Node * _copy (node* head) {Node *newhead = new node (HEAD); Node *tail = Newhead; Node *cur = head;while (cur) {if (Cur->type = = VALUE) {//Data node Tail->next = new node (value,cur->value);//Creating New nodes, Add to tail node Tail = tail->next;//tail node cur = cur->next;//copied node}else if (Cur->type = = sub) {//Sub-table node *newsub = new node (sub);//Create New child table Node Tail->next = newsub;//Add to tail node Tail = tail->next;//tail node Newsub->sublink = _copy (cur->sublink );//Recursive copy child node cur = cur->next;//copied node}else if (Cur->type = = head) {//head node cur = cur->next;//is copied and moved back}else{assert ( FALSE);//Error!}} return newhead;} swap void _swap (node* &l,node *&r) {Node *tmp = L;l = R;r = tmp;} destroys void _destory (node *head) {Node *cur = head;while (cur) {if ((Cur->type = = VALUE) | | (Cur->type = = HEAD)) {//Current node is head node or data node *del = cur;//Save current node cur = cur->next;//current node move delete del;//Delete node}else if (Cur->type = = SUB) {// The current node is a child table node * del = cur;//Saves the current node cur = cur->next;//The current node moves back _destory (del->sublink);//recursively destroys data in the child table delete del;//destroy child tables} Else{assert (false);//program Run Error!}} Determine if the data isBOOL _isvalue (const char s) {if ((s>= ' 0 ' &&s<= ' 9 ') | | (s>= ' A ' &&s<= ' z ') | | (s>= ' A ' &&s<= ' Z ')) {return true;} return false;} Recursive creation of generalized table node *_createlist (const char * &s) {assert (*s== ');//assertion string s first word is (++s;//skip (Enter data section node *head = new Node ( Head);//Create header node *tail = head;//as tail node to insert new data while (*s) {if (_isvalue (*s)) {//data item Tail->next = new Node (value,*s);// Create data node Tail = tail->next;//tail node to move ++s;} else if (*s = = ' (') {//Sub-table entry node *newnode = new node (sub);//Create child table Node Tail->next = newnode;//Insert Tail Tail = tail->next;//tail move NE Wnode->sublink = _createlist (s);//recursively Create child table data node}else if (*s = = ') ') {//Data end ++s;return head;//return head node}else{++s;//encounter comma Skip}} return head;} Recursive printing generalized table void _print (node *head) {node * cur = head;while (cur) {if (Cur->type = = head) {//Head node cout<< "(";} else if (Cur->type = = value) {//Values node Cout<<cur->value;if (cur->next!=null) {//is not the last element cout<< ",";// Comma-delimited}}else if (cur->type = = sub) {//Sub-table entry _print (cur->sublink);//recursive Print sub-table if (cur->next!=null) {//Not last datacout<< ",";//comma-delimited}}else{assert (false);} cur = cur->next;} cout<< ")";} Recursive get generalized table data number size_t _size (node *head) {node *cur = head;size_t ret = 0;while (cur) {if (Cur->type ==value) {//Current value node ++ret ;//Data number +1}else if (Cur->type ==sub) {ret + = _size (cur->sublink);//recursion to fetch the number of child table data}else{//head node}cur = Cur->next;} return ret;} Recursive generalized table depth size_t _depth (node *head) {node *cur = head;size_t MaxDepth = 1;//Current depth is 1while (cur) {if (Cur->type = = SUB) {// Encountered sub-table size_t Depth = _depth (Cur->sublink);//recursive sub-table depth if (maxdepth<depth+1) {//If the child table is deeper than the current value MaxDepth = depth+1;// Update Max Depth}}cur = Cur->next;} Return maxdepth;//returns the maximum depth}private:node * _head;}; #endif
/** file Description: Test file * Author: High Minor * Date: 2016-12-12* integrated development environment: Microsoft Visual Studio */#include <iostream>using namespace std; #include "GeneralList.h" void Generallisttest () {const char * const STR1 = "(a)"; Generallist G1 (STR1); G1. Print ();cout<< "Size =" <<g1. Size () <<endl;cout<< "Depth =" <<g1. Depth () <<endl;const char * Const STR2 = "(A, b)"; Generallist G2 (STR2); G2. Print ();cout<< "Size =" <<g2. Size () <<endl;cout<< "Depth =" <<g2. Depth () <<endl;const char * Const STR3 = "(A, B, (c,d))"; Generallist G3 (STR3); G3. Print ();cout<< "Size =" <<g3. Size () <<endl;cout<< "Depth =" <<g3. Depth () <<endl;const char * Const STR4 = "((E, (f), h))"; Generallist G4 (STR4); G4. Print ();cout<< "Size =" <<g4. Size () <<endl;cout<< "Depth =" <<g4. Depth () <<endl;const char * Const STR5 = "((()), ((3,4)))"; Generallist g5 (STR5); G5. Print ();cout<< "Size =" <<g5. Size () <<endl;cout<< "DePTH = "<<g5." Depth () <<endl;cout<< "////////copy construction//////" <<endl<<endl;//(a) generallist G6 (G1); G6. Print ();cout<< "Size =" <<g6. Size () <<endl;cout<< "Depth =" <<g6. Depth () <<endl;//(A, b) generallist G7 (G2); G7. Print ();cout<< "Size =" <<g7. Size () <<endl;cout<< "Depth =" <<g7. Depth () <<endl;//(A, B, (c,d)) generallist G8 (G3); G8. Print ();cout<< "Size =" <<g8. Size () <<endl;cout<< "Depth =" <<g8. Depth () <<endl; Generallist G9 (G4); G9. Print ();cout<< "Size =" <<g9. Size () <<endl;cout<< "Depth =" <<g9. Depth () <<endl; Generallist G10 (G5); G10. Print ();cout<< "Size =" <<g10. Size () <<endl;cout<< "Depth =" <<g10. Depth () <<endl;} int main () {generallisttest (); return 0;}
Summarize:
First contact with this, but also really a bit difficult, write my head is overdrawn, but also dedicated to a few lol to rest a bit ....
This thing is not very difficult, just because the recursive program, extremely difficult to debug. When the program is wrong, debugging is a bit crazy!
The other thing is that the individual is too close to the point. Sometimes it is quicker to accept others ' knowledge in time and then incorporate into your own knowledge system, much faster than your own death.
Realization of generalized table using C + + in data structure