Advertisement
Guest User

Untitled

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