[数据结构]广义表的建立与基本操作 解题报告

Problem Description

采用"头尾法”存储广义表,实现以下广义表的操作:

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: ()


测试输入

(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

AcCode

//
//  main.cpp
//  广义表的建立与基本操作
//
//  Created by jetviper on 2017/3/26.
//  Copyright © 2017年 jetviper. All rights reserved.
//

#include 
#include
void GetTail(char *str){
    int List_lenth = strlen(str);
    int deep = 0;
    int endIndex;
    for(int i = 1;i0){
            if(str[i] == '('){
                deep++;
                continue;
            }
            else if(str[i] == ')'){
                deep--;
                continue;
            }
            else continue;
            
        }
        
        else {
            if(str[i]=='('){
                deep++;
                continue;
            }
            else if(str[i] == ','){
                endIndex = i;
                break;
            }
            else if(i == List_lenth-1){
                str[1] = ')';
                str[2] = '\0';
                return;
            }
            else continue;
        }
        
    }
    for(int i = 1;i<=List_lenth-endIndex;i++){
        str[i] = str[i+endIndex];
    }
    str[List_lenth - endIndex + 1] = '\0';
    
    return;
    
}
void GetHead(char *str){
    int List_lenth = strlen(str);
    int deep = 0;
    int endIndex;
    char tmp[100];
    int k =0;
    for(int i = 1;i0){
            if(str[i] == '('){
                tmp[k] = str[i];
                k++;
                deep++;
                continue;
            }
            else if(str[i] == ')'){
                tmp[k] = str[i];
                k++;
                deep--;
                continue;
            }
            else {
                tmp[k] = str[i];
                k++;
                continue;
            }
            
            
        }
        
        else {
            if(str[i]=='('){
                tmp[k] = str[i];
                k++;
                deep++;
                continue;
            }
            else if(str[i] == ','){
                break;
            }
            else if(i == List_lenth-1){
                break;
            }
            else {
                tmp[k] = str[i];
                k++;
                continue;
            }}
        
    }
    for(int j=0;j

你可能感兴趣的:([数据结构]广义表的建立与基本操作 解题报告)