成绩 | 10 | 开启时间 | 2022年09月26日 星期一 18:00 |
折扣 | 0.8 | 折扣时间 | 2022年10月16日 星期日 23:55 |
允许迟交 | 否 | 关闭时间 | 2022年10月26日 星期三 23:55 |
采用"头尾法存储广义表,实现以下广义表的操作:
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: ()
广义表,这玩意选择题我会做,但是他是啥,作用和优点又是啥,我忘记了
上课老师讲了用链表写,笑死根本听不懂,还介绍了三种方法,不过这题好像就用数组就行
还有这一手自动判断输入多少也挺骚
其实题目漏掉一种情况,就是假如求表尾为空的话还好说,求表头为空的话,他没说改打印啥,但是示例也没给,所以问题不大
输出是3n+1模式
首先主函数里打印一次
函数每次打印三行
初始化指针2为最后
然后while(scanf)循环到没东西
这里我们显然需要两个函数
取表头函数
这里需要一个深度初始为-1
分别是这么几种情况
1是(,深度++,特别的-1时碰到,指针1为此+1,continue
2是),深度--,特别的,深度为0时碰到,则为 指针二为此
3头元素没括号,则在深度为0的情况下碰到,指针2为此减一
记得每个IF都要加continue
最后输出
取表尾函数
他的指针2直接是尾
这时候会有空所以当指针1碰上指针2 了,就返回()
1(深度++
2)深度—
3深度为0碰到,把,转为(
最后分空和非空输出
/*
(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
*/
#include<bits/stdc++.h>
using namespace std;
int p1=0,p2;
string Str;
void GetHead(){
printf("destroy tail\nfree list node\ngeneric list: ");
int depth=-1;
for(int i=p1;Str[i]!='\0';i++){
if(Str[i]=='('){
depth++;
if(depth==0){
p1=i+1;
continue;
}
}
if(Str[i]==')'){
depth--;
if(depth==0){
p2=i;
break;
}
}
if(Str[i]==','&&depth==0){
p2=i-1;
continue;
}
}
for(int i=p1;i<=p2;i++){
printf("%c",Str[i]);
}
printf("\n");
}
void GetTail(){
printf("free head node\nfree list node\ngeneric list: ");
int depth=-1;
int flag=0;
for(int i=p1;Str[i]!='\0';i++){
if(i==p2){
flag=1;
break;
// printf("a\n");
}
if(Str[i]=='('){
depth++;
// printf("b\n");
continue;
}
if(Str[i]==')'){
depth--;
// printf("c\n");
continue;
}
if(Str[i]==','&&depth==0){
p1=i;
Str[i]='(';
//printf("d\n");
break;
}
}
//printf("%d\n",flag);
if(flag==1){
printf("()\n");
}
else{
for(int i=p1;i<=p2;i++){
printf("%c",Str[i]);
}
printf("\n");
}
}
int main(void){
cin >> Str;
cout << "generic list: " << Str << endl;
p2=Str.length()-1;
int c;
while(cin >> c){
if(c==1){
GetHead();
}
else {
GetTail();
}
}
return 0;
}