时光飞逝~

仿双向链表实现(部分功能)
  1 ///////////////////////////////
  2 //标准C++库
  3 #include <iostream>
  4 #include <CString>
  5 ////////////////////////////////
  6 //双向链表模板类
  7 template <typename T>
  8 class MyList
  9 {
 10 public:
 11     //链表节点
 12     class Node
 13     {
 14     //构造,析构
 15     public:
 16         Node(T data, Node* next, Node* prev):m_data(data),m_next(next),m_prev(prev){}
 17         ~Node(){}
 18     public:
 19     //属性
 20     public:
 21         T m_data;
 22         Node* m_next;
 23         Node* m_prev;
 24     };
 25     ///////////////////////////////////
 26     //迭代器
 27     class Iterator
 28     {
 29     public:
 30         //构造,析构
 31         Iterator(Node* head=NULL,Node* tail=NULL,Node* node=NULL):m_head(head),m_tail(tail),m_node(node){}
 32         ~Iterator(){}
 33         //操作符重载
 34         //前++
 35         Iterator& operator++()
 36         {
 37             if(m_node->m_next)
 38             {
 39                 m_node = m_node->m_next;    
 40             }
 41             else
 42                 m_node = m_tail;
 43             return *this;
 44         }
 45         //后++
 46         const Iterator& operator++(int)
 47         {
 48             Iterator* old = this;
 49             if(m_node)
 50                 m_node = m_node->m_next;
 51             else
 52                 m_node = m_head;
 53             return *old;
 54         }
 55         //前--
 56         Iterator& operator--()
 57         {
 58             if(m_node->m_prev)
 59             {
 60                 m_node = m_node->m_prev;
 61             }
 62             else 
 63                 m_node = m_head;
 64             return *this;
 65         }
 66         //后--
 67         const Iterator& operator--(int)
 68         {
 69             Iterator *old = this;
 70             --(*this);
 71             return *old;
 72         }
 73         //赋值=
 74         Iterator& operator=(const Iterator& it)
 75         {
 76             if(this!=&it)
 77             {
 78                 m_node=it.m_node;
 79             }
 80             return *this;
 81         }
 82         //判等=
 83         bool operator==(const Iterator& it)const
 84         {
 85             return m_node == it.m_node;
 86         }
 87         bool operator!=(const Iterator& it)const
 88         {
 89             return !(*this==it);
 90         }
 91         T& operator *()
 92         {
 93             return m_node->m_data;
 94         }
 95 
 96     
 97     private:
 98         Node *m_head;
 99         Node *m_tail;
100         Node *m_node;
101         friend class MyList;
102     };
103     ////////////////////////////////////
104     //链表实现
105     //构造,析构,拷贝构造,拷贝赋值
106 public:
107     MyList():m_head(NULL),m_tail(NULL)
108     {
109 
110     }
111     ~MyList()
112     {
113         while(m_head)
114         {
115             Node * node = m_head;
116             m_head = m_head->m_next;
117             delete node;
118         }
119     }
120     MyList(const MyList& myList):m_head(NULL),m_tail(NULL)
121     {
122         for(Node *node = myList.m_head;node;node = node->m_next)
123         {
124             push_back(node->m_data);
125         }
126     }
127     MyList& operator=(const MyList& myList)
128     {
129         if(this!=&myList)
130         {
131             if(!empty())
132                 clear();
133             for(Node* node = myList.m_head;node;node = node->m_next)
134             {
135                 push_back(node->m_data);
136             }
137         }
138         return *this;
139     }
140     //清空
141     void clear()
142     {
143         for(Node* next; m_head;m_head=next )
144         {
145             next = m_head->m_next;
146             delete m_head;
147         }
148         m_head=NULL;
149         m_tail=NULL;
150     }
151     //判空
152     bool empty()
153     {
154         if(m_head)
155             return true;
156         return false;
157     }
158     //获取首元素
159     T& front()
160     {
161         if(m_head)
162             return m_head->m_data;
163                         
164     }
165     //尾端压入
166     void push_back(T data)
167     {
168         m_tail = new Node(data,NULL,m_tail);
169         if(m_head==NULL)
170         {
171             m_head = m_tail;
172         }
173         else
174         {
175             m_tail->m_prev->m_next=m_tail;
176         }
177     }
178     //尾端弹出
179     void pop_back()
180     {
181         if(m_head==NULL)
182         {
183             printf("链表下溢\n");
184             return;
185         }
186         Node * tail = m_tail;
187         m_tail = m_tail->m_prev;
188         if(m_tail)
189             m_tail->m_next = NULL;
190         else
191             m_head = NULL;
192         delete tail;
193     }
194     //首尾迭代器
195     Iterator begin (void) {
196         return Iterator (m_head, m_tail, m_head);
197     }
198     Iterator end (void) {
199             return Iterator (m_head, m_tail);
200     }
201 
202 
203 private:
204     Node *m_head;
205     Node *m_tail;
206 };
207 //打印
208 template <typename T,typename STL>
209 void Print(T &it,STL& stl)
210 {
211     it = stl.begin();
212     for(;it!=stl.end();it++)
213     {
214         std::cout << *it << ' ';
215     }
216     std::cout << std::endl;
217 }
218 //测试用例
219 int main(int argc, char* argv[])
220 {
221     MyList<int> list;
222     list.push_back(5);
223     MyList<int>::Iterator it;
224     Print(it,list);
225     list.push_back(6);
226     list.push_back(7);
227     list.push_back(8);
228     list.push_back(9);
229     Print(it,list);
230     list.clear();
231     Print(it,list);
232     MyList<int> list2;
233     list2.push_back(10);
234     list2.push_back(11);
235     list = list2;
236     Print(it,list);
237     list.pop_back();
238     Print(it,list);
239     return 0;
240 }
View Code

 

posted on 2013-07-16 23:10  时光飞逝~  阅读(189)  评论(0)    收藏  举报