General List 广义表实现

ContractedBlock.gif ExpandedBlockStart.gif GenList.h
  1 #ifndef GENLIST_H
  2 #define GENLIST_H
  3 
  4 #include <iostream>
  5 
  6 #define HEAD 0
  7 #define INTGR 1
  8 #define CH 2
  9 #define LST 3
 10 
 11 class GenListNode
 12 {
 13 public:
 14     int nodeType(GenListNode *ptrElem){ return utype; }
 15     GenListNode &info(GenList *ptrElem);
 16     void setInfo(GenListNode *ptrElem, GenListNode &x);
 17 
 18 private:
 19     int utype;// 0 - GenList head node   1 - int node   2 - char node  3 - child list node
 20     union
 21     {
 22         int ref;
 23         int intinfo;
 24         char charinfo;
 25         GenListNode *listHead;
 26     }value;
 27     GenListNode *next;
 28 };
 29 
 30 
 31 class GenList
 32 {
 33 public:
 34     GenList();
 35     ~GenList();
 36     //GenListNode &fist();
 37     bool empty(){ return (head->next == NULL); }
 38     GenListNode *tail();
 39     GenListNode &head();
 40     GenListNode *next(GenListNode *ptrElem);
 41 
 42 
 43 
 44 
 45 private:
 46     int depth(GenListNode *ls);
 47     int equal(GenListNode *s, GenListNode *t);
 48     void clear(GenListNode *ls);
 49 
 50     GenListNode *start;// point to start node(start node not belong to GenList)
 51     
 52 
 53 }
 54 
 55 GenList::GenList() //Create head node
 56 {
 57     GenList *head = new GenListNode;
 58     start->utype = HEAD; 
 59     start->ref = 1;
 60     start->next = NULL;
 61 }
 62 
 63 GenList::~GenList()
 64 {
 65     clear();
 66     delete start;
 67 }
 68 
 69 GenListNode &GenList::head()//the first child-list 
 70 {
 71     if(empty())
 72     {
 73         cerr << "Empty, no first node: first() \n";
 74         return NULL;
 75     }
 76 
 77     GenListNode *firstHead = new GenListNode;
 78     firstHead->utype = start->next->utype;
 79     firstHead->value = start->next->value;
 80     firstHead->next = start->next->next;
 81 
 82     return *firstHead;
 83 }
 84 
 85 
 86 GenListNode *GenList::tail()//the tail child-list 
 87 {
 88     if(empty())
 89     {
 90         cerr << "Empty, no first node: first() \n";
 91         return NULL;
 92     }
 93 
 94     GenListNode *tailHead = new GenListNode;
 95     tailHead->utype = HEAD;
 96     tailHead->ref = 1;
 97     tailHead->next = first()->next;
 98 
 99     return tailHead;
100 }
101 
102 GenListNode *GenList::next(GenListNode *ptrElem)
103 {
104     if(ptrElem->next == NULL)
105     {
106         cerr << "No next elem: next()" << endl;
107         return NULL;
108     }
109 
110     return ptrElem->next;
111 }
112 
113 void GenList::push(GenListNode *newNode)
114 {
115     if(empty())
116     {
117         start->next = newNode;
118         return;
119     }
120 
121     newNode->next = start->next;
122     head->next = newNode;
123 }
124 
125 #endif

未完成....

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值