[ 数据结构作业 ] 有表头链表

虽然大部分是模仿的,但是总算是用了两个小时把这个作业弄完了。第一次写迭代器和template。迭代器吗~基本就是一个类型,把++ == != * ->重载一下就搞定了。
有表头链表
  1 template <class T>
  2 struct __head_list_node{
  3     typedef __head_list_node<T> node;
  4 
  5     T data;
  6     node* next;
  7 
  8     __head_list_node() { next = NULL; }
  9     __head_list_node(const T& t){
 10         data = t;
 11         next = NULL;
 12     }
 13 };
 14 template <class T, class Ref, class Ptr>
 15 struct __head_list_iterator{
 16     typedef __head_list_iterator <T, T&, T*> iterator;
 17     typedef __head_list_iterator <T, const T&const T*> const_iterator;
 18     typedef __head_list_iterator <T, Ref, Ptr> self;
 19 
 20     typedef __head_list_node <T> node;
 21 
 22     node* itx;
 23 
 24     __head_list_iterator() : itx(0) {}
 25     __head_list_iterator(node *x) : itx(x) {}
 26     __head_list_iterator(const_iterator& x) : itx(x.itx) {}
 27 
 28     bool operator == (const __head_list_iterator& x) { return itx == x.itx; }
 29     bool operator != (const __head_list_iterator& x) { return itx != x.itx; }
 30 
 31     Ref operator *() { return itx->data; }
 32     Ptr operator ->() { return &operator*(); }
 33 
 34     self& operator ++ (){
 35         itx = itx->next;
 36         return *this;
 37     }
 38     const self operator ++ (int){
 39         self tmp(*this);
 40         itx = itx->next;
 41 
 42         return tmp;
 43     }
 44 };
 45 template <class T>
 46 class __head_list{
 47 public:
 48     typedef __head_list_iterator <T, T&, T*> iterator;
 49     typedef __head_list_iterator <T, const T&const T*> const_iterator;
 50 private:
 51     typedef __head_list_node <T> node;
 52 
 53     size_t __list_size;
 54     node* head;
 55     node* tail;
 56 public:
 57     __head_list(){
 58         head = new node;
 59         head->next = NULL;
 60         tail = head;
 61         __list_size = 0;
 62     }
 63     ~__head_list(){
 64         clear();
 65     }
 66 
 67     iterator begin(){
 68         return iterator(head->next);
 69     }
 70     iterator end(){
 71         return iterator(0);
 72     }
 73     const_iterator begin() const{
 74         return const_iterator(head->next);
 75     }
 76     const_iterator end() const{
 77         return const_iterator(0);
 78     }
 79 
 80     size_t size() const{
 81         return __list_size;
 82     }
 83     bool empty() const{
 84         return head->next == NULL;
 85     }
 86 
 87     node* GetPrevious(node *q){
 88         node* p;
 89         for(p = head; p && p->next != q; p = p->next);
 90         return p;
 91     }
 92     void append(const T& t){
 93         node *= new node;
 94         p->data = t;
 95         p->next = NULL;
 96 
 97         tail->next = p;
 98         tail = p;
 99 
100         __list_size ++;
101     }
102     iterator insert(iterator pos, const T& t){
103         node *= new node;
104         p->data = t;
105         p->next = pos.itx;
106         GetPrevious(pos.itx)->next = p;
107 
108         __list_size ++;
109         return iterator(p);
110     }
111     iterator insert_after(iterator pos, const T& t){
112         node *= new node;
113         p->data = t;
114         p->next = pos.itx->next;
115         pos.itx->next = p;
116 
117         __list_size ++;
118         return iterator(p);
119     }
120     iterator locate(const T& t){
121         node *p;
122         for(p = head; p && p->data != t; p = p->next);
123         return iterator(p);
124     }
125     iterator modify(iterator pos, const T& t){
126         pos.itx->data = t;
127         return pos;
128     }
129     void erase(iterator pos){
130         node *= GetPrevious(pos.itx);
131         q->next = pos.itx->next;
132         __list_size --;
133         if(pos.itx == tail) tail = q;
134         delete pos.itx;
135     }
136     void clear(){
137         node *p, *q;
138         for(p = head; p; ){
139             q = p;
140             p = p->next;
141             delete q;
142         }
143         __list_size = 0;
144     }
145 };


posted @ 2010-04-16 13:34  刘一佳  阅读(269)  评论(0编辑  收藏  举报