数据结构广义表实验

一、问题描述:

1、题目内容:识别广义表的“头”或“尾”的演示

写一个程序,建立广义表的存储结构,演示在此存储结构上实现的广义表求头/求尾操作序列的结果。

2、基本要求

(1)设一个广义表允许分多行输入,其中可以任意地输入空格符,原子是不限长的仅字母或数字组成的串。

(2)广义表采用如教材中结点的存储结构,试按表头和表尾的分解方法编写建立广义表存储结构的算法。

(3)对已建立存储结构的广义表施行操作,操作序列为一个仅由“t“或”h”组成的串,它可以是空串(此时印出整个广义表),自左至右施行各操作,再以符号形式显示结果。

代码:

  1 //panda
  2 //2013-4-24
  3 
  4 typedef char ElemType;
  5 
  6 //定义结点
  7 struct GLNode
  8 {
  9     bool tag;//标志域
 10     union 
 11     {
 12         ElemType data;
 13         GLNode* sublist;
 14     };
 15     GLNode* next;//指向后续结点指针域
 16 };
 17 
 18 class GList
 19 {
 20 //private:
 21 public:
 22     GLNode* head;
 23     //int length,depth;难以维护
 24 
 25     //输入字符串的左右括号数量
 26     int ifinputover;  //左括号+1,右括号-1
 27     //debug
 28     int i;
 29 public:
 30     GList();
 31     ~GList();
 32 public:
 33     int GetLength(GLNode* gl);
 34     int GetDepth(GLNode* gl);
 35     void CreateGListByInput(GLNode*& gl);////// 
 36     void ClearGList();//清除广义表
 37     void PrintGList(GLNode* gl);//输入广义表
 38     GLNode* PrintHeadNode(GLNode* head);//h操作
 39     GLNode* PrintTailNode(GLNode* tail);//t操作
 40 };
 41 //panda
 42 //2013-4-24
 43 
 44 
 45 #include"GList.h"
 46 #include<iostream>
 47 using namespace std;
 48 
 49 //
 50 //构造函数
 51 //
 52 GList::GList()
 53 {
 54     head=NULL;
 55     //length=depth=0;
 56     ifinputover=0;
 57     //debug
 58     i=0;
 59 };
 60 //
 61 //析构函数
 62 //
 63 GList::~GList()
 64 {
 65     ClearGList();
 66 };
 67 //
 68 //通过输入构建广义表
 69 //ok     ///以#结束输入///       真正的广义表指针是gl->sublist
 70 //广义表的头指针需要处理head=gl->sublist;
 71 void GList::CreateGListByInput(GLNode*& gl)
 72 {
 73     //debug
 74     //int i=0;//
 75     ++i;//每调用一次加1  表示是第几次调用
 76 
 77     char ch;
 78     //读入一个字符,这里可能是‘(’,字符
 79     cin>>ch;
 80     //建立结点,并构造子表
 81     if (ch=='(')
 82     {
 83         ++ifinputover;//左括号数量+1
 84         gl=new GLNode();
 85         gl->tag=true;//结点为子表
 86         CreateGListByInput(gl->sublist);
 87     }else if (ch==')')//这里遇到),则表明为空表
 88     {
 89         --ifinputover;//右括号+1
 90         //子表的结束
 91         gl=new GLNode();
 92         gl->tag=false;
 93         gl->data=NULL;
 94         gl->next=NULL;
 95         //debug
 96         --i;
 97         return;
 98     }else//不是子表,输入数据/////////////    )?
 99     {
100         gl=new GLNode();
101         gl->tag=false;
102         gl->data=ch;
103     }
104 
105     if (ifinputover)//没结束输入 
106     {
107         //此处输入的字符可能为')',','
108         cin>>ch;
109     }else  //输入完毕
110     {
111         gl->next=NULL;
112         --i;
113         return ;
114     }
115 
116     //若为,则递归构造后续表
117     if (ch==',')
118     {
119         CreateGListByInput(gl->next);
120     }else if (ch==')')
121     {
122         --ifinputover;
123         gl->next=NULL;
124         //debug
125         //--i;
126         //return;
127     }
128     //debug
129     --i;//回到调用前的数值
130 };
131 //
132 //获得广义表深度
133 //
134 int GList::GetDepth(GLNode* gl)
135 {
136     //return depth;
137     int max=0;
138     while (gl!=NULL)
139     {
140         if (gl->tag=true)
141         {
142             int dep=GetDepth(gl->sublist);
143             if (dep>max)
144             {
145                 max=dep;
146             }
147         }
148         gl=gl->next;
149     }
150     return max+1;
151 };
152 //
153 //获得广义表的长度
154 //
155 int GList::GetLength(GLNode* gl)
156 {
157     //return length;
158     if (gl!=NULL)
159     {
160         return 1+GetLength(gl->next);
161     }else
162     {
163         return 0;
164     }
165 };
166 //
167 //清空广义表
168 //
169 void GList::ClearGList()
170 {
171 
172 };
173 //
174 //打印广义表
175 //ok
176 void GList::PrintGList(GLNode* gl)
177 {
178     if (gl->tag==true)
179     {
180         cout<<'(';
181         //输入子表
182         if (gl->sublist==NULL)
183         {
184             //output nothinig
185         }else
186         {
187             PrintGList(gl->sublist);
188         }
189         cout<<')';
190     }else//输入数据结点
191     {
192         cout<<gl->data;
193     }
194     if (gl->next!=NULL)
195     {
196         cout<<',';
197         PrintGList(gl->next);
198     }
199 };
200 //
201 //h操作
202 //ok
203 GLNode* GList::PrintHeadNode(GLNode* head)
204 {
205     if (head==NULL)
206     {
207         return NULL;
208     }
209     //deal glist
210     if (head->tag==true)
211     {
212         if (head->sublist->tag==true)
213         {
214             if(head->sublist->next!=NULL)
215             {
216                 head->sublist->next=NULL;
217             }else
218             {
219                 head=head->sublist;
220             }
221         }
222 
223     }else if (head->tag==false)
224     {
225         head->next=NULL;
226     }
227 
228     //output glist
229     PrintGList(head);
230     cout<<',';
231 
232     if (head->tag==true)
233     {
234         if (head->sublist->tag==false&&head->next==NULL)
235         {
236             return head;
237         }
238         return head->sublist;
239     }else if (head->tag==false)
240     {
241         return NULL;
242     }
243 };
244 //
245 //t操作
246 //ok
247 GLNode* GList::PrintTailNode(GLNode* tail)
248 {
249     if (tail==NULL)
250     {
251         return NULL;
252     }
253     //deal glist
254     if (tail->tag==true)
255     {
256         if (tail->sublist->tag==true)
257         {
258             if (tail->sublist->next!=NULL)
259             {
260                 //前移一个结点
261                 tail->sublist=tail->sublist->next;
262             }else
263             {
264                 tail=tail->sublist;
265             }
266         }else if (tail->sublist->tag==false)
267         {
268             tail->sublist=tail->sublist->next;
269         }
270 
271     }else if (tail->tag==false)
272     {
273         tail=tail->next;
274     }
275 
276     //output glist
277     PrintGList(tail);
278     cout<<',';
279 
280 
281     if (tail->tag==true)
282     {
283         if (tail->sublist->tag==true&&tail->sublist->next==NULL)
284         {
285             return tail;
286         }else if (tail->sublist->tag==true&&tail->sublist->next!=NULL)
287         {
288             //tail->sublist
289             return tail;
290         }
291         return tail->sublist;
292     }else if (tail->tag==false)
293     {
294         return NULL;
295     }
296 };
297 /////////////////////////////////
298 //递归函数,只能一步一步的调试了
299 
300 #include"GList.h"
301 #include<iostream>
302 using namespace std;
303 
304 void InputString(char *ch,int size)
305 {
306     cin.ignore(1);
307     char temp;
308     int i=0;
309     while (temp=getchar())
310     {
311         if (temp=='\n')
312         {
313             ch[i]='\0';
314             return;
315         }
316         if (i<size)
317         {
318             ch[i++]=temp;
319         }else
320         {
321             return;
322         }
323     }
324 };
325 
326 int main()
327 {
328     GList glist;
329     cout<<"input the GList:";
330     glist.CreateGListByInput(glist.head);
331     cout<<"PrintGList:";
332     glist.PrintGList(glist.head);
333     cout<<endl<<"请输入对广义表的操作序列(回车结束输入):";
334     GLNode* temp=glist.head;
335     char ch[50];
336     InputString(ch,50);
337     //operate glist
338     char tempch;
339     for (int i = 0; i < 50; i++)
340     {
341         tempch=ch[i];
342         if (tempch=='h'||tempch=='H')
343         {
344             temp=glist.PrintHeadNode(temp);
345         }else if (tempch=='t'||tempch=='T')
346         {
347             temp=glist.PrintTailNode(temp);
348         }else if (tempch=='\0')
349         {
350             break;
351         }else
352         {
353             cout<<"input error.";
354             break;
355         }
356     }
357 
358     system("pause");
359     return 0;
360 }

 

posted @ 2015-09-30 14:50  小熊猫小  阅读(510)  评论(0编辑  收藏  举报