十六、广义表的建立与基本操作

您所在的位置:网站首页 广义表示法 十六、广义表的建立与基本操作

十六、广义表的建立与基本操作

2024-07-10 09:48| 来源: 网络整理| 查看: 265

十六、广义表的建立与基本操作

文章目录 十六、广义表的建立与基本操作题目描述解题思路上机代码补充说明

题目描述

采用"头尾法存储广义表,实现以下广义表的操作: 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)21211generic list: (a,(b,(c,d)),e,f)free head nodefree list nodegeneric list: ((b,(c,d)),e,f)destroy tailfree list nodegeneric list: (b,(c,d))free head nodefree list nodegeneric list: ((c,d))destroy tailfree list nodegeneric list: (c,d)destroy tailfree list nodegeneric list: c1秒64M0测试用例 2(a,(b,(c,(d,())),e))21212121generic list: (a,(b,(c,(d,())),e))free head nodefree list nodegeneric list: ((b,(c,(d,())),e))destroy tailfree list nodegeneric list: (b,(c,(d,())),e)free head nodefree list nodegeneric list: ((c,(d,())),e)destroy tailfree list nodegeneric list: (c,(d,()))free head nodefree list nodegeneric list: ((d,()))destroy tailfree list nodegeneric list: (d,())free head nodefree list nodegeneric list: (())destroy tailfree list nodegeneric list: ()1秒64M0测试用例 3((a,s,(w,e)),q,c)1222generic list: ((a,s,(w,e)),q,c)destroy tailfree list nodegeneric list: (a,s,(w,e))free head nodefree list nodegeneric list: (s,(w,e))free head nodefree list nodegeneric list: ((w,e))free head nodefree list nodegeneric list: ()1秒64M0 解题思路

教材上虽然定义了广义表的存储结构,但是建立广义表的 createList 有些过于复杂,而且其余关键运算也没有给出具体算法。仔细分析题目发现,题目重点要实现的是取表头和取表尾的过程。相比之下,与其大费周章建立广义表,还不如用字符串直接操作来得更快。

用字符串直接存取待操作的广义表,根据输入的数字 1 或 2 进行去表头或取表尾操作即可。

表中第一个逗号之前的,都是表头表中第一个逗号之后的,都是表尾

取表头其实在逗号之前也可以判断,找到第一个左括号后的第一对括号,这就是表头。例如 ((a,b),c,(d,e)) 这个广义表,第一对括号(a,b)就是表头。

当然,这个判断与找第一个逗号的差距仅仅是 O(1) 的时间,聊胜于无。我在下面的代码中进行了书写,方便大家理解。

上机代码 #include #include #include 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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3