简单的广义表实现

#ifndef GLLIST_H
#define GLLIST_H

struct GLNode{
    bool isNum;
    union {
        int data;
        struct {
            GLNode* hp,*tp;
        };
    };
};
class GLlist
{
    private:
        GLNode* pNode;
        GLNode* init_GLNode(int);
        GLNode* init_GLNode();
        unsigned travel_depth(GLNode*);
        void travel_input(GLNode**);
        void travel_output(GLNode*);
        GLlist(GLNode*);
        void destory(GLNode*);
    public:
        GLlist();
        ~GLlist();
        void input();
        void output();
        GLNode* getHead();
        GLlist getTail();
        unsigned depth();
        unsigned length();
};
 //all this definitions is just used to be a sign
#define PRIVATE
#define PUBLIC
#include "GLlist.h"
#include<iostream>
using namespace std;
PRIVATE GLNode* GLlist::init_GLNode(int element){
    GLNode* temp=new GLNode;
    temp->isNum=1;
    temp->data=element;
    return temp;
}
PRIVATE GLNode* GLlist::init_GLNode(){
    GLNode* temp=new GLNode;
    temp->isNum=0;
    temp->hp=temp->tp=0;
    return temp;
}
PRIVATE GLlist::GLlist(GLNode* p):pNode(p){}
PRIVATE unsigned GLlist::travel_depth(GLNode* p){
    unsigned dep=1,max=1;
    while(p!=0){
        if(p->isNum==0&&p->hp!=0&&p->hp->isNum==0){
            if((dep=travel_depth(p->hp)+1)>max)
                max=dep;
        }
        p=p->tp;
    }
    return max;
}
PRIVATE void GLlist::destory(GLNode* p){
    while(p!=0){
        if(p->isNum==0&&p->hp!=0&&p->hp->isNum==1)
            delete p->hp;
        else if(p->isNum==0&&p->hp!=0&&p->hp->isNum==0)
            destory(p->hp);
        GLNode* temp;
        temp=p;
        p=p->tp;
        delete temp;
    }
}
PRIVATE void GLlist::travel_output(GLNode* p){
    cout<<'(';
    while(p!=0){
        if(p->isNum==0&&p->hp!=0&&p->hp->isNum==0)
            travel_output(p->hp);
        else if(p->isNum==0&&p->hp!=0&&p->hp->isNum==1)
            cout<<p->hp->data;
        else
            break;
        if(p->tp!=0)
            cout<<',';
        p=p->tp;
    }
    cout<<')';
}
//TODO: reconstruct it!
//must use point**, because you will change the value of point
PRIVATE void GLlist::travel_input(GLNode** p){
    char ch;
    int data;
    cin.get(ch);//skip the first '('
    while(cin.get(ch)){
        /*
         *iterate the main GLlist,
         *if there is a sub GLlist,
         *then recurrent the function
        */
        if(ch==',')//skip comma
            continue;
        switch(ch){//aumatic computability
        case '('://create the sub GLlist
            cin.putback(ch);
            //you must putback '(',for you will skip it next
            *p=init_GLNode();
            (*p)->hp=init_GLNode();
            travel_input(&(*p)->hp);
            break;
        //if ch is a num, just putback it to the input stream, then use cin
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            cin.putback(ch);
            cin>>data;
            *p=init_GLNode();
            (*p)->hp=init_GLNode(data);
            break;
        case ')':
            return;
        }
        p=&(*p)->tp;
    }
}
PUBLIC GLlist::GLlist():pNode(0){}
PUBLIC GLlist::~GLlist(){
    if(pNode!=0){
        destory(pNode);
    }
}


PUBLIC void GLlist::input(){
    travel_input(&pNode);
    if(pNode==0)
        //if the input is "()",just give pNode an empty GLNode
        //TODO: reconstruct it, it a poor idea
        pNode=init_GLNode();
}
PUBLIC void GLlist::output(){
    if(pNode==0)
        cout<<"Not A GLlist!"<<endl;
    travel_output(pNode);
}
PUBLIC GLNode* GLlist::getHead(){
    if(pNode==0){
        cerr<<"ERROR: empty GLlist!\n";
        return 0;
    }
    return pNode->hp;
}
PUBLIC GLlist GLlist::getTail(){
    if(pNode==0){
        cerr<<"ERROR: empty GLlist!\n";
        return 0;
    }
    GLlist temp;
    temp.pNode=pNode->tp;
    if(temp.pNode==0)
        temp.pNode=init_GLNode();
    return temp;
}
PUBLIC unsigned GLlist::depth(){
    if(pNode==0){
        cerr<<"ERROR: empty GLlist!\n";
        return 0;
    }
    return travel_depth(pNode);
}
PUBLIC unsigned GLlist::length(){
    if(pNode==0){
        cerr<<"ERROR: empty GLlist!\n";
        return 0;
    }
    GLNode* p=pNode;
    unsigned count=0;
    while(p!=0){
        if(p->isNum==0&&p->hp!=0)
            ++count;
        p=p->tp;
    }
    return count;
}

 
 
 

 

 
 
 
 

  

转载于:https://my.oschina.net/codesun/blog/96087

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值