Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // implements a doubly-linked list
  6. template <class T>
  7. class MyList
  8. {
  9.     class NullPointer {};
  10.    
  11.     class Node
  12.     {
  13.         public:
  14.             T     data;
  15.             Node* prev;
  16.             Node* next;
  17.  
  18.             Node()
  19.                 : data(T())
  20.                 , prev(NULL)
  21.                 , next(NULL)
  22.             {
  23.                 return;
  24.             }
  25.  
  26.             Node(T const& _data)
  27.                 : data(_data)
  28.                 , prev(NULL)
  29.                 , next(NULL)
  30.             {
  31.                 return;
  32.             }
  33.  
  34.             Node(T const& _data, Node* _prev, Node* _next)
  35.                 : data(_data)
  36.                 , prev(_prev)
  37.                 , next(_next)
  38.             {
  39.                 return;
  40.             }
  41.  
  42.             ~Node()
  43.             {
  44.                 // memory leaks are bad, mmmk
  45.                 //delete data;
  46.             }
  47.  
  48.             void insert_after(Node* new_node)
  49.             {
  50.                 // bail, we got a null pointer
  51.                 if (new_node == NULL)
  52.                     throw NullPointer();
  53.                
  54.                 // new node's next is current next
  55.                 new_node->next = next;
  56.                
  57.                 // new node's previous becomes me
  58.                 new_node->prev = this;
  59.                
  60.                 // current next's previous is the new node
  61.                 next->prev = new_node;
  62.                
  63.                 // hold onto next so we don't lose it
  64.                 Node* next_ref = next;
  65.  
  66.                 // next one after us is now the new node we're inserting
  67.                 next = new_node;
  68.             }
  69.     };
  70.    
  71.     // stores the size of the linked list
  72.     size_t _size;
  73.    
  74.     // head and tail pointers
  75.     // simply point to next and prev nodes, respectively, do not store data
  76.     Node* head;
  77.     Node* tail;
  78.    
  79.     public:
  80.         MyList();
  81.         ~MyList();
  82.        
  83.         // push new data onto the front
  84.         void push_front(T const& data);
  85.        
  86.         // push new data onto the back
  87.        
  88.         void print()
  89.         {
  90.             Node* cursor = head;
  91.            
  92.             while (cursor != NULL)
  93.             {
  94.                 cout << cursor->data << endl;
  95.                 cursor = cursor->next;
  96.             }
  97.         }
  98.  
  99. };
  100.  
  101. template<class T>
  102. MyList<T>::MyList()
  103.                  : _size(0)
  104.                  , head(new Node())
  105.                  , tail(new Node())
  106. {
  107.     head->next = tail;
  108.     tail->prev = head;
  109.     return;
  110. }
  111.  
  112. template<class T>
  113. MyList<T>::~MyList()
  114. {
  115.     delete head;
  116.     delete tail;
  117. }
  118.  
  119. template<class T>
  120. void MyList<T>::push_front(T const& data)
  121. {
  122.     Node* new_node = new Node(data);
  123.  
  124.     head->insert_after(new_node);
  125. }
  126.  
  127. int main()
  128. {
  129.     MyList<string> my_list;
  130.    
  131.     my_list.push_front("one");
  132.     my_list.push_front("two");
  133.     my_list.push_front("three");
  134.    
  135.     my_list.print();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement