广义表

结点结构(扩展线性链表)

 1 typedef char AtomType;
 2 enum ElemTag { ATOM, LIST };
 3 
 4 typedef struct GLNode
 5 {//扩展的线性链表存储结构
 6     ElemTag tag;
 7 
 8     union
 9     {
10         AtomType atom;
11         struct GLNode* head;
12     }value;
13 
14     struct GLNode* next;
15 }GLNode;

创建广义表(带头结点:因为广义表本身也是一个表结点)

 1 GLNode* CreateGList(GLNode* A)
 2 {//带头结点
 3     //假定广义表中元素类型为ElemType为char类型,每个原子的值被限定为单个字符。
 4     //并假设广义表是一个正确的表达式,其格式为:
 5     //元素之间用一个逗号分隔,表元素的起止符号为左、右括号,空表在元括号内包含#。
 6     char ch = '\0';
 7 
 8     scanf("%c", &ch);
 9     if (ch != '\0')
10     {//表达式未结束
11         A = (GLNode*)calloc(1, sizeof(GLNode));
12         if (ch == '(')
13         {
14             A->tag = LIST;
15             A->value.head = CreateGList(A->value.head);
16         }
17         else if (ch == '#' || ch == ')')
18         {
19             A = NULL;
20         }
21         else
22         {//原子结点
23             A->tag = ATOM;
24             A->value.atom = ch;
25         }
26     }
27     else
28     {
29         A = NULL;
30     }
31 
32     scanf("%c", &ch);
33     if (A != NULL)
34     {
35         if (ch == ',')
36         {
37             A->next = CreateGList(A->next);
38         }
39         else
40         {
41             A->next = NULL;
42         }
43     }
44     return A;
45 }
View Code

打印广义表

 1 void PrintGList(GLNode* A)
 2 {
 3     if (A)
 4     {
 5         if (A->tag == LIST)
 6         {
 7             printf("(");
 8             if (A->value.head)
 9             {
10                 PrintGList(A->value.head);
11             }
12         }
13         else
14         {//原子结点
15             printf("%c", A->value.atom);
16         }
17         
18         if (A->tag == LIST)
19         {
20             printf(")");
21         }
22         if (A->next)
23         {
24             printf(",");
25             PrintGList(A->next);
26         }
27     }//广义表非空
28 }
View Code

取广义表的表头(因为存储结构带头结点,所以要取下一层的表头)

 1 GLNode* GetHead(GLNode* A)
 2 {
 3     if (A->tag != LIST)
 4     {//带头结点的广义表
 5         exit(-1);
 6     }
 7 
 8     A = A->value.head;
 9     if (A == NULL)
10     {
11         return NULL;
12     }
13     else if (A->tag == ATOM)
14     {
15         exit(-1);
16     }
17     else
18     {
19         return A->value.head;
20     }
21 }
View Code

取广义表的表尾

 1 GLNode* GetTail(GLNode* A)
 2 {
 3     if (A->tag != LIST)
 4     {//带头结点的广义表
 5         exit(-1);
 6     }
 7 
 8     A = A->value.head;
 9     if (A == NULL)
10     {
11         return NULL;
12     }
13     else if (A->tag == ATOM)
14     {
15         exit(-1);
16     }
17     else
18     {
19         return A->next;
20     }
21 }
View Code

求广义表长度

 1 int GListLength(GLNode* A)
 2 {
 3     int len = 0;
 4 
 5     if (A->tag != LIST)
 6     {
 7         exit(-1);
 8     }
 9 
10     A = A->value.head;
11     if (A == NULL)
12     {
13         return 0;
14     }
15     while (A)
16     {
17         len++;
18         A = A->next;
19     }
20     return len;
21 }
View Code

求广义表的深度

 1 int GListDepth(GLNode* A)
 2 {
 3     int dep;
 4     int max = 0;
 5 
 6     if (A->tag != LIST)
 7     {
 8         exit(-1);
 9     }
10 
11     A = A->value.head;//带头结点的广义表
12     if (A == NULL)
13     {
14         return 1;
15     }
16     while (A)
17     {
18         if (A->tag == LIST)
19         {
20             dep = GListDepth(A);
21             if (dep > max)
22             {
23                 max = dep;
24             }
25         }
26         A = A->next;
27     }
28     return max + 1;
29 }
View Code

求广义表中的原子结点个数

 1 int CountAtom(GLNode* A)
 2 {
 3     int count = 0;
 4 
 5     if (A->tag != LIST)
 6     {
 7         exit(-1);
 8     }
 9 
10     A = A->value.head;
11     if (A == NULL)
12     {
13         return 0;
14     }
15     while (A)
16     {
17         if (A->tag == ATOM)
18         {
19             count++;
20         }
21         else
22         {
23             count += CountAtom(A);
24         }
25         A = A->next;
26     }
27     return count;
28 }
View Code

复制广义表

 1 GLNode* CopyGList(GLNode* A, GLNode* B)
 2 {
 3     if (A == NULL)
 4     {
 5         return NULL;
 6     }
 7     else
 8     {
 9         B = (GLNode*)calloc(1, sizeof(GLNode));
10         B->tag = A->tag;
11         if (A->tag == ATOM)
12         {
13             B->value.atom = A->value.atom;
14         }
15         else
16         {
17             B->value.head = CopyGList(A->value.head, B->value.head);
18         }
19         B->next = CopyGList(A->next, B->next);
20         return B;
21     }
22 }
View Code

程序源码

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 
  5 typedef char AtomType;
  6 enum ElemTag { ATOM, LIST };
  7 
  8 typedef struct GLNode
  9 {//扩展的线性链表存储结构
 10     ElemTag tag;
 11 
 12     union
 13     {
 14         AtomType atom;
 15         struct GLNode* head;
 16     }value;
 17 
 18     struct GLNode* next;
 19 }GLNode;
 20 
 21 GLNode* CreateGList(GLNode* A);
 22 GLNode* GetHead(GLNode* A);
 23 GLNode* GetTail(GLNode* A);
 24 int GListLength(GLNode* A);
 25 int GListDepth(GLNode* A);
 26 int CountAtom(GLNode* A);
 27 GLNode* CopyGList(GLNode* A, GLNode* B);
 28 
 29 void PrintGList(GLNode* A);
 30 
 31 int main(void)
 32 {
 33     GLNode* A = NULL;
 34     GLNode* B = NULL;
 35 
 36     printf("请输入广义表表达式:\n(元素之间用一个逗号分隔,表元素的起止符号为左、右括号,空表在元括号内包含#)\n");
 37     A = CreateGList(A);
 38     PrintGList(A);
 39 
 40     printf("\nlen:%d\n", GListLength(A));
 41 
 42     printf("dep:%d\n", GListDepth(A));
 43 
 44     printf("CountAtom:%d\n", CountAtom(A));
 45 
 46     B = CopyGList(A, B);
 47     PrintGList(B);
 48 
 49     system("pause");
 50     return 0;
 51 }
 52 
 53 GLNode* CreateGList(GLNode* A)
 54 {//带头结点
 55     //假定广义表中元素类型为ElemType为char类型,每个原子的值被限定为单个字符。
 56     //并假设广义表是一个正确的表达式,其格式为:
 57     //元素之间用一个逗号分隔,表元素的起止符号为左、右括号,空表在元括号内包含#。
 58     char ch = '\0';
 59 
 60     scanf("%c", &ch);
 61     if (ch != '\0')
 62     {//表达式未结束
 63         A = (GLNode*)calloc(1, sizeof(GLNode));
 64         if (ch == '(')
 65         {
 66             A->tag = LIST;
 67             A->value.head = CreateGList(A->value.head);
 68         }
 69         else if (ch == '#' || ch == ')')
 70         {
 71             A = NULL;
 72         }
 73         else
 74         {//原子结点
 75             A->tag = ATOM;
 76             A->value.atom = ch;
 77         }
 78     }
 79     else
 80     {
 81         A = NULL;
 82     }
 83 
 84     scanf("%c", &ch);
 85     if (A != NULL)
 86     {
 87         if (ch == ',')
 88         {
 89             A->next = CreateGList(A->next);
 90         }
 91         else
 92         {
 93             A->next = NULL;
 94         }
 95     }
 96     return A;
 97 }
 98 
 99 void PrintGList(GLNode* A)
100 {
101     if (A)
102     {
103         if (A->tag == LIST)
104         {
105             printf("(");
106             if (A->value.head)
107             {
108                 PrintGList(A->value.head);
109             }
110         }
111         else
112         {//原子结点
113             printf("%c", A->value.atom);
114         }
115         
116         if (A->tag == LIST)
117         {
118             printf(")");
119         }
120         if (A->next)
121         {
122             printf(",");
123             PrintGList(A->next);
124         }
125     }//广义表非空
126 }
127 
128 GLNode* GetHead(GLNode* A)
129 {
130     if (A->tag != LIST)
131     {//带头结点的广义表
132         exit(-1);
133     }
134 
135     A = A->value.head;
136     if (A == NULL)
137     {
138         return NULL;
139     }
140     else if (A->tag == ATOM)
141     {
142         exit(-1);
143     }
144     else
145     {
146         return A->value.head;
147     }
148 }
149 
150 GLNode* GetTail(GLNode* A)
151 {
152     if (A->tag != LIST)
153     {//带头结点的广义表
154         exit(-1);
155     }
156 
157     A = A->value.head;
158     if (A == NULL)
159     {
160         return NULL;
161     }
162     else if (A->tag == ATOM)
163     {
164         exit(-1);
165     }
166     else
167     {
168         return A->next;
169     }
170 }
171 
172 int GListLength(GLNode* A)
173 {
174     int len = 0;
175 
176     if (A->tag != LIST)
177     {
178         exit(-1);
179     }
180 
181     A = A->value.head;
182     if (A == NULL)
183     {
184         return 0;
185     }
186     while (A)
187     {
188         len++;
189         A = A->next;
190     }
191     return len;
192 }
193 
194 int GListDepth(GLNode* A)
195 {
196     int dep;
197     int max = 0;
198 
199     if (A->tag != LIST)
200     {
201         exit(-1);
202     }
203 
204     A = A->value.head;//带头结点的广义表
205     if (A == NULL)
206     {
207         return 1;
208     }
209     while (A)
210     {
211         if (A->tag == LIST)
212         {
213             dep = GListDepth(A);
214             if (dep > max)
215             {
216                 max = dep;
217             }
218         }
219         A = A->next;
220     }
221     return max + 1;
222 }
223 
224 int CountAtom(GLNode* A)
225 {
226     int count = 0;
227 
228     if (A->tag != LIST)
229     {
230         exit(-1);
231     }
232 
233     A = A->value.head;
234     if (A == NULL)
235     {
236         return 0;
237     }
238     while (A)
239     {
240         if (A->tag == ATOM)
241         {
242             count++;
243         }
244         else
245         {
246             count += CountAtom(A);
247         }
248         A = A->next;
249     }
250     return count;
251 }
252 
253 GLNode* CopyGList(GLNode* A, GLNode* B)
254 {
255     if (A == NULL)
256     {
257         return NULL;
258     }
259     else
260     {
261         B = (GLNode*)calloc(1, sizeof(GLNode));
262         B->tag = A->tag;
263         if (A->tag == ATOM)
264         {
265             B->value.atom = A->value.atom;
266         }
267         else
268         {
269             B->value.head = CopyGList(A->value.head, B->value.head);
270         }
271         B->next = CopyGList(A->next, B->next);
272         return B;
273     }
274 }
View Code

 

posted @ 2022-01-03 14:17  吕辉  阅读(146)  评论(0)    收藏  举报