LinkList class 实现

LinkList.h
 1 #ifndef LINKLIST_H
 2 #define LINKLIST_H
 3 
 4 
 5 
 6 template <typename Type> class Node
 7 {
 8     //friend class LinkList;
 9     public:
10         Node();
11         Node(const Type &item);
12         ~Node();
13         //Node<Type> * next() const { return m_next; }
14         //Type data() const { return m_data; }
15 
16         void insertAfter(Node<Type> *p);
17         Type removeAfter();
18 
19     //private:
20         Type data;
21         Node<Type> *next;
22 };
23 
24 /* This link list include a head node, this node is not the first node
25  * of this list. The first node of list should be *(head->next).
26  * We can know if this list is null conveniently by compare (head->next == 0).
27  */
28 template <typename Type> class LinkList
29 {
30     public:
31         LinkList();
32         LinkList(const Type &val);
33         ~LinkList();
34 
35         int length();
36         bool empty(){ return head->next == NULL; }
37         Node<Type> *getHead(){ return head; }
38         Type getElem(int i);
39         Type operator[](int i);
40 
41         void makeEmpty();
42         Node<Type> *search(Type val);
43         Node<Type> *find(int i);
44         
45 
46         int insert(int i, Type val);
47         Type remove(int i);
48 
49         void push_back(const Type &val);
50         Type pop_back();
51         void visit();
52 
53 
54     private:
55         Node<Type> *head;
56 
57 };
58 
59 #include "LinkList.cpp"
60 
61 
62 
63 #endif

LinkList.cpp
  1 //for Node class implement
  2 ////////////////////////////////////////////////////////////////////////////////////
  3 template <typename Type> Node<Type>::Node() : next(NULL)
  4 {
  5 }
  6 
  7 template <typename Type> Node<Type>::Node(const Type &data) : data(data), next(NULL)
  8 {
  9 }
 10 
 11 template <typename Type> Node<Type>::~Node()
 12 {
 13 }
 14 
 15 template <typename Type> void Node<Type>::insertAfter(Node<Type> *p)
 16 {
 17     p->next = next;
 18     next = p;
 19 }
 20 
 21 template <typename Type> Type Node<Type>::removeAfter()
 22 {
 23     Node<Type> *ptr;
 24     Type val;
 25     if(next == NULL) return NULL;
 26 
 27     ptr = next;
 28     next = ptr->next;
 29     val = ptr->data;
 30     delete ptr;
 31     
 32     return val;
 33 }
 34 
 35 
 36 //for LinkList class implement
 37 ///////////////////////////////////////////////////////////////////////////////////
 38 
 39 
 40 template <typename Type> LinkList<Type>::LinkList()
 41 {
 42     head = new Node<Type>();//create a head node
 43 }
 44 
 45 template <typename Type> LinkList<Type>::LinkList(const Type &val)
 46 {
 47     head = new Node<Type>(val);
 48 }
 49 
 50 template <typename Type> LinkList<Type>::~LinkList()//no implement
 51 {
 52     makeEmpty();
 53     delete head;
 54 }
 55 
 56 template <typename Type> int LinkList<Type>::length()
 57 {
 58     int i = 0;
 59     Node<Type> *ptr = head;
 60     while(ptr->next != NULL)
 61     {
 62         ptr = ptr->next;
 63         i++;
 64     }
 65 
 66     return i;
 67 }
 68 
 69 template <typename Type> Type LinkList<Type>::getElem(int i)
 70 {
 71     if(i < 0 || i >= length())
 72     {
 73         cerr << "Invailed index: getElem(int i)\n";
 74         exit(-1);
 75     }
 76     
 77     Node<Type> *ptr = head->next;
 78     int j;
 79 
 80     for(j = 0; j < i; j++)
 81     {
 82         ptr = ptr->next;
 83     }
 84     
 85     return ptr->data;
 86 
 87 }
 88 
 89 
 90 template <typename Type> void LinkList<Type>::makeEmpty()
 91 {
 92     if(empty())
 93         return;
 94 
 95     Node<Type> *ptr;
 96     while(head->next != NULL)
 97     {
 98         ptr = head->next;
 99         head->next = ptr->next;
100         delete ptr;
101     }
102 
103 }
104 
105 template <typename Type> Node<Type> * LinkList<Type>::search(Type val)
106 {
107     Node<Type> *ptr = head->next;
108 
109     while(ptr != NULL && ptr->data != val)
110     {
111         ptr = ptr->next;
112     }
113 
114     return ptr;
115 
116 
117 }
118 
119 template <typename Type> Node<Type> * LinkList<Type>::find(int i)
120 {
121     if(i < 0 || i >= length())
122     {
123         cerr << "Invailed index: find(int i)\n";
124         return NULL;
125     }
126     
127     Node<Type> *ptr = head->next;
128     int j;
129 
130     for(j = 0; j < i; j++)
131     {
132         ptr = ptr->next;
133     }
134     
135     return ptr;
136 
137 }
138 
139 template <typename Type> int LinkList<Type>::insert(int i, Type val)
140 {
141     if(i < 0 || i >= length())
142     {
143         cerr << "Invailed index: insert(int i, Type val)\n";
144         exit(-1);
145     }
146 
147     Node<Type> *ptr;
148     Node<Type> *newNode = new Node<Type>(val);
149     
150     ptr = find(i-1);
151     ptr->insertAfter(newNode);
152 
153     return 0;
154 
155 }
156 
157 
158 template <typename Type> Type LinkList<Type>::remove(int i)
159 {
160     if(i < 0 || i >= length())
161     {
162         cerr << "Invailed index: remove(int i)\n";
163         exit(-1);
164     }
165 
166     Node<Type> *ptr;
167 
168     ptr = find(i-1);
169     return ptr->removeAfter();
170 
171 }
172 
173 
174 template <typename Type> void LinkList<Type>::push_back(const Type &val)
175 {
176     Node<Type> *newNode = new Node<Type>(val);
177     Node<Type> *ptr = head;
178     
179     if(empty())
180     {
181         ptr->insertAfter(newNode);
182         return;
183     }
184 
185     ptr = find(length() - 1);
186     ptr->insertAfter(newNode);
187 
188 }
189 
190 template <typename Type> Type LinkList<Type>::pop_back()
191 {
192     if(empty())
193     {
194         cerr << "Empty, no element to pop: pop_back()\n";
195         exit(-1);
196     }
197 
198     Node<Type> *ptr = find(length() - 2);
199     return ptr->removeAfter();
200 }
201 
202 
203 template <typename Type> void LinkList<Type>::visit()
204 {
205     Node<Type> *ptr = head->next;
206     while(ptr != NULL)
207     {
208         cout << ptr->data << " ";
209         ptr = ptr->next;
210     }
211     cout << "\n";
212 }
213 
214 template <typename Type> Type LinkList<Type>::operator[](int i)
215 {
216     return getElem(i);
217 }

测试main.cpp
 1 #include <iostream>
 2 #include <string>
 3 #include "LinkList.h"
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9 
10 
11 //test for class Node<Type>
12 /*
13     Node<int> *head = new Node<int>(1);
14 
15     Node<int> *Node1 = new Node<int>(3);
16     head->insertAfter(Node1);
17 
18     Node<int> *Node2 = new Node<int>(6);
19     Node1->insertAfter(Node2);
20 
21     Node<int> *ptr = head;
22 
23 
24     while(ptr != NULL)
25     {
26         cout << ptr->data <<" ";
27         ptr = ptr->next;
28         
29     }
30 
31 */
32 
33 
34 ///test for LinkList<int>
35 
36     LinkList<int> *= new LinkList<int>;
37     
38     //int val;
39     L->push_back(3);
40     L->push_back(5);
41     L->push_back(8);
42     L->push_back(67);
43     L->push_back(99);
44     L->push_back(158);
45     cout << L->length() << "\n";
46     L->visit();
47 
48     L->pop_back();
49     L->visit();
50 
51     L->pop_back();
52     L->visit();
53 
54     L->insert(222);
55     L->visit();
56 
57     L->remove(3);
58     L->visit();
59 
60 
61 ///test for LinkList<string>
62     LinkList<string> *strL = new LinkList<string>;
63     
64     strL->push_back("I");
65     strL->push_back("am");
66     strL->push_back("a");
67     strL->push_back("graduate");
68     strL->push_back("student");
69     strL->push_back("in");
70     strL->push_back("SJTU");
71     cout << strL->length() << "\n";
72     strL->visit();
73 
74     strL->pop_back();
75     strL->visit();
76 
77     strL->pop_back();
78     strL->visit();
79 
80     strL->insert(2"%%");
81     strL->visit();
82 
83     strL->remove(3);
84     strL->visit();
85 
86     cout << (*strL)[8];
87 
88     return 0;
89 
90 
91 }
posted @ 2007-06-03 01:20  中土  阅读(832)  评论(0编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.