Create and print a generalized table
Create and print a generalized table
This article is taken from "data structure and algorithm" (C edition) (Third edition). The press is Tsinghua University Press.
This blog post is used as learning materials. The source code is the executable program in VC ++ 6.0. I moved it to VS2010 for execution.
Create a C ++ Win32 console application project in VS2010. The creation result is as follows:
1. Create a generalized table:
A generalized table can be defined using the following recursive method.
Basic items: (1) If a generalized table is empty, when s is empty, (2) If a generalized table is an atomic node and s is a single string.
Induction: Assume that Subs is the string of S to remove the outermost parentheses, and record it as "S1, S2 ,..., sn ", where Si (I = 1 ,..., n) is a non-null string. Create a table node for each Si and set the hp domain pointer to the header pointer of the sub-table created by Si. Except that the tail pointer of the last created table node is NULL, the tail pointer of other table nodes points to the table nodes created after it. That is to say, because Si is a string, we must first create a table node for this string. the pointer field of the table node points to the element in Si, its tp domain points to the Si + 1 created next time, while the tp domain of the Sn is empty.
The subs string is used in the definition of a generalized table. Therefore, a function is required to obtain this string. The Getsubs (str, hstr) function is used here. Among them, hstr stores 1st "," the previous substring, and the function modifies str to delete the substring hstr and "," the remaining substring. If the str string does not contain ",", hstr is str, and str is changed to NULL.
2. Print generalized tables:
The algorithm used to print a generalized table is: print a left brace first (". If tag = ATOM is encountered, print the value of this node. If there is a successor node after this node, print another ",". If tag = LIST indicates a sub-table, call the function recursively to print the sub-Table. After all the nodes are printed, print a "). ".
The source code is as follows: GeneralList. cpp
// GeneralList. cpp: defines the entry point of the console application. # Include "stdafx. h" # include
# Include
# Include
Typedef enum {ATOM, LIST} ElemTag; // ATOM = 0 atom, LIST = 1 sub-Table struct GLNode {ElemTag tag; // identify the domain union {char ATOM; struct GLNode * hp;}; struct GLNode * tp;}; typedef struct GLNode GList; // obtain the substring Subsvoid Getsubs (char str [], char hstr []) of the generalized table. {int I = 0; int j = 0; int k = 0; // used to record the numbers of unmatched left parentheses int r = 0; char s [100]; while (str [I] & (str [I]! = ',' | K) {if (str [I] = '(') k ++; else if (str [I] = ')') k --; if (str [I]! = ',' | Str [I] = ',' & k) {hstr [j] = str [I]; I ++; j ++ ;}} hstr [j] = '\ 0'; if (str [I] =', ') I ++; while (str [I]) {s [r] = str [I]; r ++; I ++;} s [r] = '\ 0'; strcpy (str, s );} // create a generalized table GList * GListCreate (char str []) {GList * G; char subs [100]; char hstr [100]; GList * q; int len = strlen (str); if (len = 0 |! Strcmp (str, "()") G = NULL; else if (len = 1) // case of atomic nodes {G = (GList *) malloc (sizeof (GList); if (! G) {printf ("An error occurred while applying for a space! "); Exit (0);} G-> tag = ATOM; G-> atom = * str; G-> tp = NULL;} else {GList * p; G = (GList *) malloc (sizeof (GList); if (! G) {printf ("An error occurred while applying for a space! "); Exit (0);} G-> tag = LIST; p = G; str ++; strncpy (subs, str, len-2); // remove the outermost () subs [len-2] = '\ 0'; while (len> 0) {GList * r; Getsubs (subs, hstr ); // separate subs into the header hstr and the end subsr = GListCreate (hstr); // generate the header's generalized table p-> hp = r; q = p; len = strlen (subs); if (len> 0) {p = (GList *) malloc (sizeof (GList); if (! G) {printf ("An error occurred while applying for a space! "); Exit (0);} p-> tag = LIST; q-> tp = p;} q-> tp = NULL;} return G ;} // print the generalized table void GListPrint (GList * G) {GList * q, * p; printf ("("); while (G) {p = G-> hp; q = G-> tp; if (p & q &&! P-> tag) // p is an atomic node with a subsequent node {printf ("% c,", p-> atom); p = q-> hp; q = q-> tp;} if (p &&! P-> tag) // p is an atomic node, and no subsequent node {printf ("% c", p-> atom); break;} else {if (! P) printf ("()"); // p is an empty table else GListPrint (p); if (q) printf (","); // The subsequent node G = q ;}} printf (")") ;}int main () {int n; char * s; GList * G; printf ("Enter the characters in the generalized table: \ n"); scanf ("% d", & n); printf ("Enter the generalized table: \ n "); s = (char *) malloc (n * sizeof (char); scanf ("% s", s); G = GListCreate (s ); printf ("the input generalized table is \ n"); GListPrint (G); printf ("\ n"); return 0 ;}
Ctrl + F5 run GeneralList. cpp: