General List 广义表实现
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
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
未完成....