十六、广义表的建立与基本操作
十六、廣義表的建立與基本操作
文章目錄
- 十六、廣義表的建立與基本操作
- 題目描述
- 解題思路
- 上機代碼
- 補充說明
題目描述
采用"頭尾法存儲廣義表,實現以下廣義表的操作:
1.Status CreateGList( GList &L, char *S ) // 根據字符串 S 表示的廣義表內容建立廣義表數據結構;
2.GList GetHead( GList L) // 取表頭運算
3.GList GetTail( GList L) // 取表尾運算
4.void DestroyGList( GList &L) // 銷毀廣義表 L
5.void PrintGList( GList L) // 顯示廣義表 L 內容
程序運行時,首先輸入一個廣義表,表中的原子是小寫字母。隨后可以交替輸入取表頭或取表尾指令(分別用 1 和 2 表示),取的結果替代當前廣義表,并釋放相應的資源(需將釋放資源信息輸出)。當廣義表是空或是原子時,程序停止運行。
例:(下面的黑體為輸入)
((a,()),c,d)
generic list: ((a,()),c,d)
1
destroy tail
free list node
generic list: (a,())
2
free head node
free list node
generic list: (())
1
destroy tail
free list node
generic list: ()
| 測試用例 1 | (a,(b,(c,d)),e,f) 2 1 2 1 1 | generic list: (a,(b,(c,d)),e,f) free head node free list node generic list: ((b,(c,d)),e,f) destroy tail free list node generic list: (b,(c,d)) free head node free list node generic list: ((c,d)) destroy tail free list node generic list: (c,d) destroy tail free list node generic list: c | 1秒 | 64M | 0 |
| 測試用例 2 | (a,(b,(c,(d,())),e)) 2 1 2 1 2 1 2 1 | generic list: (a,(b,(c,(d,())),e)) free head node free list node generic list: ((b,(c,(d,())),e)) destroy tail free list node generic list: (b,(c,(d,())),e) free head node free list node generic list: ((c,(d,())),e) destroy tail free list node generic list: (c,(d,())) free head node free list node generic list: ((d,())) destroy tail free list node generic list: (d,()) free head node free list node generic list: (()) destroy tail free list node generic list: () | 1秒 | 64M | 0 |
| 測試用例 3 | ((a,s,(w,e)),q,c) 1 2 2 2 | generic list: ((a,s,(w,e)),q,c) destroy tail free list node generic list: (a,s,(w,e)) free head node free list node generic list: (s,(w,e)) free head node free list node generic list: ((w,e)) free head node free list node generic list: () | 1秒 | 64M | 0 |
解題思路
教材上雖然定義了廣義表的存儲結構,但是建立廣義表的 createList 有些過于復雜,而且其余關鍵運算也沒有給出具體算法。仔細分析題目發現,題目重點要實現的是取表頭和取表尾的過程。相比之下,與其大費周章建立廣義表,還不如用字符串直接操作來得更快。
用字符串直接存取待操作的廣義表,根據輸入的數字 1 或 2 進行去表頭或取表尾操作即可。
- 表中第一個逗號之前的,都是表頭
- 表中第一個逗號之后的,都是表尾
取表頭其實在逗號之前也可以判斷,找到第一個左括號后的第一對括號,這就是表頭。例如 ((a,b),c,(d,e)) 這個廣義表,第一對括號(a,b)就是表頭。
當然,這個判斷與找第一個逗號的差距僅僅是 O(1) 的時間,聊勝于無。我在下面的代碼中進行了書寫,方便大家理解。
上機代碼
#include<cstdio> #include<cstring> #include<cstdlib> char s[1010]; int len1 = 0, len2 = -1; //len1——len2表示每次需要取的區間長度 void gethead(); //取表頭 void gettail(); //取表尾int main() {int n = 0, m = 0;memset(s, 0, sizeof(s));scanf("%s", s);printf("generic list: %s\n", s);len2 = strlen(s) - 1;int ans = 0;while (~scanf("%d", &ans)){if (ans == 1) //取表頭gethead();else //取表尾gettail();}return 0; } void gethead() {printf("destroy tail\nfree list node\ngeneric list: ");int temp = -1;int i = len1; //每次從上一次的位置開始取while (s[i]!='\0'){if (s[i] == '('){temp++;if (temp == 0)len1 = i + 1;i++;continue;}if (s[i] == ')'){temp--;//if (temp == 0) //第一個左括號后面的第一對括號就是表頭//{// len2 = i;// break;//}i++;continue;}if (s[i] == ','&&temp == 0) //表中第一個逗號之前的,都是表頭{len2 = i - 1;break;}i++;}//輸出表頭for (int j = len1; j <= len2; j++){printf("%c", s[j]);}printf("\n"); } void gettail() {printf("free head node\nfree list node\ngeneric list: ");int temp = -1;int vis = 0;int i = len1;while (s[i] != '\0'){if (i == len2) //空表{vis = 1;break;}if (s[i] == '(')temp++;if (s[i] == ')')temp--;if (s[i] == ','&& temp == 0) //表中的第一個逗號之后的,都是表尾{s[i] = '(';len1 = i;break;}i++;}if (vis==1) //空表直接輸出{printf("()\n");return;}//輸出表尾for (int j = len1; j <= len2; j++){printf("%c", s[j]);}printf("\n"); }補充說明
相信細心的同學已經發現了上面算法的問題所在
就是當廣義表為 ((a,b)) 時,廣義表中沒有逗號,這時輸入 1 ,進行取表頭的操作。通過逗號判斷的方法就失效了,還是需要我們剛才說的對于第一對括號的判斷。
另外,如果廣義表為 (a) ,即僅有一個原子時,再輸入 1 取表頭。這時連括號判斷都失效了。對于這樣的問題,有一個解決辦法是掃描字符串給 len1 賦值時,同時也給 len2 賦相同的值。
- 如果廣義表只有單個原子,那么此時的 len2 就是唯一取值,可以直接輸出表頭
- 如果廣義表還有別的元素,len2 在后面必然會更改,不會影響到 len2 的取值
輸入數字的 while 循環結尾,判斷 len1 是否等于 len2 ,如果二者相等則證明此時廣義表僅有原子,程序應該停止運行。二者不等則廣義表中仍有元素,還可以進行取表頭操作。
至于上面代碼中把第一對括號判斷進行了注釋但是對結果不影響,是因為測試用例里沒有針對這種情況的測試,所以代碼可以順序通過學校的 OJ。
剛才列出來的兩種缺陷,有興趣的同學可以在此基礎上加點代碼將問題解決掉。
下面給出我的示例代碼:
#include<cstdio> #include<cstring> #include<cstdlib> char s[1010]; int len1 = 0, len2 = -1; //len1——len2表示每次需要取的區間長度 void gethead(); //取表頭 void gettail(); //取表尾int main() {int n = 0, m = 0;memset(s, 0, sizeof(s));scanf("%s", s);printf("generic list: %s\n", s);len2 = strlen(s) - 1;int ans = 0;while (~scanf("%d", &ans)){if (ans == 1) //取表頭gethead();else //取表尾gettail();if(len1 == len2) //廣義表僅剩原子,停止運行break;}return 0; } void gethead() {printf("destroy tail\nfree list node\ngeneric list: ");int temp = -1;int i = len1; //每次從上一次的位置開始取while (s[i]!='\0'){if (s[i] == '('){temp++;if (temp == 0)len2 = len1 = i + 1;i++;continue;}if (s[i] == ')'){temp--;if (temp == 0) //第一個左括號后面的第一對括號就是表頭{len2 = i;break;}i++;continue;}if (s[i] == ','&&temp == 0) //表中第一個逗號之前的,都是表頭{len2 = i - 1;break;}i++;}//輸出表頭for (int j = len1; j <= len2; j++){printf("%c", s[j]);}printf("\n"); } void gettail() {printf("free head node\nfree list node\ngeneric list: ");int temp = -1;int vis = 0;int i = len1;while (s[i] != '\0'){if (i == len2) //空表{vis = 1;break;}if (s[i] == '(')temp++;if (s[i] == ')')temp--;if (s[i] == ','&& temp == 0) //表中的第一個逗號之后的,都是表尾{s[i] = '(';len1 = i;break;}i++;}if (vis==1) //空表直接輸出{printf("()\n");return;}//輸出表尾for (int j = len1; j <= len2; j++){printf("%c", s[j]);}printf("\n"); }總結
以上是生活随笔為你收集整理的十六、广义表的建立与基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十五、稀疏矩阵的乘法运算
- 下一篇: 十七、二叉树的建立与基本操作