广义表的建立与基本操作

广义表的建立与基本操作

题目信息

采用"头尾法存储广义表,实现以下广义表的操作:
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 表示),取的结果替代当前广义表,并释放相应的资源(需将释放资源信息输出)。当广义表是空或是原子时,程序停止运行。

测试用例

测试用例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

测试用例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: ()

测试用例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: ()

解答

#include<stdio.h>
#include<string.h>
#include <iostream>
#include <string>

using namespace std;

string Str;
int Point_1 = 0, Point_2;//表头表尾用作定位

void GetHead()
{
    cout << "destroy tail" << endl << "free list node" << endl << "generic list: ";
    int depth = -1;
    for (int i = Point_1; Str[i] != '\0'; i++)
    {
        if (Str[i] == '(')
        {//移动到第一个(
            depth++;
            if (depth == 0)
                Point_1 = i + 1;
            continue;
        }
        if (Str[i] == ')')
        {//移动到当前最大括号的第一个`,`前,即与`(`同层级的`)`
            depth--;
            if (depth == 0)
            {
                Point_2 = i;
                break;
            }
            continue;
        }
        if (Str[i] == ',' && depth == 0)
        {//也有可能这个之前没有括号,只是一个元素,所以找到同层次的','也可以
            Point_2 = i - 1;
            break;
        }
    }
    for (int i = Point_1; i <= Point_2; i++)
        cout << Str[i];
    cout << endl;
}

void GetTail()
{
    cout << "free head node" << endl << "free list node" << endl << "generic list: ";

    int depth = -1;
    int Flag = 0;
    for (int i = Point_1; Str[i] != '\0'; i++)
    {
        if (i == Point_2)
        {//开头和结尾在一起,即后面是个空表
            Flag = 1;
            break;
        }
        if (Str[i] == '(')
            depth++;
        if (Str[i] == ')')
            depth--;
        if (Str[i] == ',' && depth == 0)
        {//跨过第一个也就是表头元素,就找到了,并停止
            Str[i] = '(';
            Point_1 = i;
            break;
        }
    }
    if (Flag == 1)
    {
        cout << "()" << endl;
        return;
    }
    for (int i = Point_1; i <= Point_2; i++)
        cout << Str[i];
    cout << endl;
}

int main()
{
    //freopen("/Users/zhj/Downloads/test.txt", "r", stdin);
    cin >> Str;
    cout << "generic list: " << Str << endl;
    Point_2 = Str.length() - 1;

    int op;
    while (cin >> op)
    {
        if (op == 1)
            GetHead();
        else
            GetTail();
    }
    return 0;
}

想法

广义表((a,b),c,d)表头和表尾分别是什么?
公式:
(1)表头:当广义表LS非空时,称第一个元素为LS的表头;
(2)表尾:称广义表LS中除去表头后其余元素组成的广义表为LS的表尾。
还是没有发现表头和表尾的区别?
!!!表头是元素,表尾是广义表。!!!

举个栗子
广义表(a, (b))的表头是单元素a,表尾是广义表((b))。[要在(b)的外面加一层小括号,才能变成广义表。因此是((b))]

再举个栗子。
广义表(a)的表头是单元素a,表尾是广义表(),哇塞,为啥是(),不是空呢?明明a后面没有元素了。想想表尾一定是个广义表。潜台词就是一定带()有元素就有一个孤零零的()好了

再举个栗子。
广义表(a, b, c)的表头是单元素a,表尾是广义表(b,c)

好到现在已经清楚了:广义表的表头是单元素,表尾是个广义表。

总结:根据广义表对表头和表尾的定义可知:
(1)对任意一个非空的广义表,其表头可能是单元素,也可能是广义表,
(2)而其表尾一定是广义表。
(3)注意表尾的深度(即括号的嵌套层数)
(4)表尾是由除了表头以外的其余元素组成的广义表,所以,需要在表尾的直接元素外面再加一层括号。

来验证学习的怎么样:
求广义表的表头和表尾是广义表的基本操作,给定一个广义表,可以将某个单元素通过表头和表尾求出。
已知广义表L=(a,(b,(c,(d)), e), f )
L1=Tail(L)=((b,(c,(d)), e), f )
L2=Head(L1)= (b,(c,(d)), e)
L3=Tail(L2)=((c,(d)), e)
L4=Head(L3)=(c,(d))
L5=Head(L4)= c
如果以上5个都正确,那么恭喜已经掌握了广义表的头和尾了。

接下来再来看操作技巧:
1.因为要再次输出,所以先用数组储存。
2.取表头操作
建立两个指针P1、P2,分别指头和指尾
取表头即为P1移动到第一个(后,P2移动到当前最大括号的第一个,前,例原始(a(b,c),d(e,),f),取表头即为P1移动到(a(b,c),d(e,),f),P2移动到(a(b,c),d(e,),f)然后从P1到P2遍历输出即可
3.取表尾操作
建立两个指针P1、P2,分别指头和指尾
取表尾即为P1从第一个(移动到当前最大括号的最后一个,同时,变为(,如果P1等于P2,输出(),例原始(a,(b,c),e),取表尾即为P1移动到(a,(b,c),e)同时变符号即为(a,(b,c)(e),然后从P1到P2遍历输出即可
难点:判断”(“ , ”)” , ”,”符号等级,可先令depth=-1凡是遇到(depth++,遇到”)”就depth–即可。在遍历搜索时判断一下当前depth的等级即可,而我们要用的等级为depth==0,即当前最大括号的那个。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhj12399

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值