Advertisement
poohitan

list_with_iterator

Mar 7th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct Node {
  4.     int value;
  5.     Node * next;
  6.     Node * prev;
  7.     Node(): value(NULL), next(NULL), prev(NULL) {}
  8.     Node(int v): value(v), next(NULL), prev(NULL) {}
  9.     friend ostream & operator<<(ostream & os, const Node * n) {
  10.         os<<n->value;
  11.         return os;
  12.     }
  13.     friend istream & operator>>(istream & is, Node & n) {
  14.         is>>n.value;
  15.         return is;
  16.     }
  17. };
  18.  
  19. class List {
  20.     Node * head;
  21.     Node * tail;
  22. public:
  23.     List(): head(NULL), tail(NULL) {}
  24.     Node * begin()const {return head;}
  25.     Node * end()const {return tail;}
  26.     void pushfront(int value) {
  27.         if (head==NULL) {head=tail=new Node(value);}
  28.         else {
  29.             head->prev=new Node(value);
  30.             head->prev->next=head;
  31.             head=head->prev;
  32.         }
  33.     }
  34.     void pushback(int value) {
  35.         if (tail==NULL) {head=tail=new Node(value);}
  36.         else {
  37.             tail->next=new Node(value);
  38.             tail->next->prev=tail;
  39.             tail=tail->next;
  40.         }
  41.     }
  42.     void removefront() {
  43.         if (head==NULL) return;
  44.         Node * temp=head;
  45.         head=head->next;
  46.         head->prev=NULL;
  47.         delete temp;
  48.     }
  49.     void removeback() {
  50.         if (head==NULL) return;
  51.         Node * temp=tail;
  52.         tail=tail->prev;
  53.         tail->next=NULL;
  54.         delete temp;
  55.     }
  56. /*  void insert(int value, unsigned pos) {
  57.         Node * temp=head;
  58.         unsigned po=0;
  59.         if (pos==0) {
  60.             Node * p=new Node(value);
  61.             head=p;
  62.             head->next=temp;
  63.             temp->prev=head;
  64.         }
  65.         else {
  66.             while (po<pos) {
  67.                 temp=temp->next;
  68.                 po++;
  69.             }
  70.             Node * p=new Node(value);
  71.             temp->prev->next=p;
  72.             p->prev=temp->prev;
  73.             p->next=temp;
  74.         }
  75.     }*/
  76.     bool empty()const {return (head==NULL);}
  77.     unsigned size() {
  78.         unsigned size=1;
  79.         Node * temp=head;
  80.         while (temp->next!=NULL) {
  81.             size++;
  82.             temp=temp->next;
  83.         }
  84.         return size;
  85.     }
  86.     friend ostream & operator<<(ostream & os, const List & l) {
  87.         if (l.head!=NULL) {
  88.             Node * temp=l.head;
  89.             cout<<temp;
  90.             while (temp->next!=NULL) {
  91.                 cout<<temp->next;
  92.                 temp=temp->next;
  93.             }
  94.         }
  95.         else cout<<"NULL";
  96.         return os;
  97.     }
  98.  
  99.     class Iter {
  100.         Node * curr;
  101.         friend List;
  102.     public:
  103.         Iter(): curr(NULL) {}
  104.         Iter(const List & l): curr(l.begin()) {}
  105.         Iter(const Iter & it): curr(it.curr) {}
  106.         Iter & operator=(const Iter & it) {
  107.             if(this!=&it) curr=it.curr;
  108.             return *this;
  109.         }
  110.         Iter & operator++() {curr=curr->next;}
  111.         Iter & operator--() {curr=curr->prev;}
  112.         int & operator*()const {return ((Node*)curr)->value;}
  113.         void operator*(int v) {((Node*)curr)->value=v;}
  114.         Node * current()const {return curr;}
  115.     };
  116.  
  117.     void insert(int value, Iter it) {
  118.         if (head==NULL) head=new Node(value);
  119.         else {}
  120.     }
  121. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement