This article is aimed at the data structure Basic Series Network course (5): Array and generalized table practice project.
"Project-Atomic statistics of generalized tables"
Design algorithm to find the number of atoms and the largest atom in a given generalized table G
[Refer to answer] (header file glist.h is the result of the construction of generalized table algorithm library, see link ... )
#include <Stdio.H>#include "Glist.h"int Atomnum (Glnode*G//To find the number of atoms in a generalized table G{if(g!=NULL) {if(g -Tag==0)return 1+Atomnum (g -Link);Else returnAtomnum (g -Val.sublist)+Atomnum (g -Link); }Else return 0;} Elemtype Maxatom (Glnode*G//Find the largest atom in generalized table G{Elemtype max1,max2;if(g!=NULL) {if(g -Tag==0) {Max1=Maxatom (g -Link);return(g -Val.Data>Max1?G -Val.Data: MAX1); }Else{MAX1=Maxatom (g -Val.sublist); Max2=Maxatom (g -Link);return(Max1>Max2?MAX1:MAX2); } }Else return 0;} int main () {Glnode*G Char*S="(b, (B,a, (#), D), ((a B), C ((#) ))"; G=CREATEGL (s); DISPGL (g); printf"\ n"); printf"Number of atoms:%d\n", Atomnum (g)); printf"Max Atom:%c\n", Maxatom (g));return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure Practice--atomic statistics of generalized tables