Data structure C + + generalized table

Source: Internet
Author: User

//CTest.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>#include<string.h>#include<stdio.h>using namespaceStd;typedefCharElemtype;structglnode{BOOLTag//flag Bitunion{//table header pointer field for a range or child tableelemtype data; Glnode*sublist;    }; Glnode*next;};//used for testing (a, (b, (c)), (#), ((D,e))), F, (g));//To find the length of a generalized table is to find the length of a single linked list .intLenth (Glnode *GL) {    if(gl!=NULL) {        return 1+lenth (gl->next); }Else{        return 0; }}//To find the length of a generalized table is to find the length of a single linked list .intDepth (Glnode *GL) {    intmax=0;  while(gl!=NULL) {        if(gl->tag==true){            intDEP = Depth (gl->sublist); if(Dep>max) max=DEP; } GL=gl->Next; }    returnmax+1;}//Create a generalized tablevoidCreate (glnode* &GL) {    CharCh//read a characterCin>>ch; //If # is entered, the GL is empty    if(ch=='#') gl=NULL; //If the input is an opening parenthesis, the child table node pointed by GL is established and the table is recursively constructed.    Else if(ch='(') {GL=NewGlnode; GL->tag=true; Create (GL-sublist); }    Else{GL=NewGlnode; GL->tag=false; GL->data=ch; }    //the characters read in here are Fu Bi comma, right parenthesis, or semicolonCin>>ch; //If GL is empty, the character entered at this time must be ') ', then nothing is done      if(gl==NULL); //recursively constructs a successor table if the input is a comma      Else if(ch==',') Create (gl->next); //if the input is a closing parenthesis or semicolon, the subsequent pointer field of GL is empty      Else if((ch==')')|| (ch==';')) gl->next=NULL;}//Print out generalized tablevoidPrint (Glnode *GL) {    if(gl->tag==true) {cout<<'(';//for table nodes, the opening parenthesis is first exported as the start symbol .        if(gl->sublist==NULL) cout<<'#';//If the Word table pointer is empty, the output ' # ' character        ElsePrint (GL->sublist);//if it is a non-empty Word table, the table is output recursivelycout<<')';//when the output of a Word table is finished, the closing parenthesis should be output as a terminator    }    Elsecout<<gl->data;//for a single-element node, output the value of the node    if(Gl->next!=null) {//output The successor table of the GL nodecout<<',';//after the parentheses and the delimiters are first outputPrint (Gl->next);//re-recursive output successor table    }}int_tmain (intARGC, _tchar*argv[]) {Glnode*g=NULL;    Create (g);    Print (g); cout<<Endl; cout<<"the length of a generalized table:"<<lenth (g->sublist) <<Endl; cout<<"the depth of a generalized table:"<<depth (g->sublist) <<Endl; System ("Pause"); return 0;}

Data structure C + + generalized table

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.