Advertisement
MaxTwain

Final_Node (LinkedList)

Jun 3rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct List
  5. {
  6.     int data;
  7.     List* next;
  8.     List* previous;
  9. };
  10.  
  11. class Node
  12. {
  13. private:
  14.     List* first;
  15.     List* last;
  16. public:
  17.     Node()
  18.     {
  19.         first = NULL;
  20.         last = first;
  21.     }
  22.     ~Node();
  23.     void push(int n);
  24.     void root();
  25.     void shift();
  26.     void unshift(int n);
  27.     void pop();
  28. };
  29.  
  30. void Node::push(int n)
  31. {
  32.     List *temp = new List;
  33.     temp->next = NULL;
  34.     temp->data = n;
  35.  
  36.     if (first != NULL)
  37.     {
  38.         temp->previous = last;
  39.         last->next = temp;
  40.         last = temp;
  41.     }
  42.     else
  43.     {
  44.         temp->previous = NULL;
  45.         first = last = temp;
  46.     }
  47.  
  48. }
  49.  
  50. void Node::root()
  51. {
  52.     List* current = first;
  53.     while (current)
  54.     {
  55.         cout << current->data << "->";
  56.         current = current->next;
  57.     }
  58.     cout << "NULL" << endl << endl;
  59. }
  60.  
  61. void Node::unshift(int n)
  62. {
  63.     List* temp = new List;
  64.     temp->data = n;
  65.     temp->next = first;
  66.     temp->previous = NULL;
  67.     first->previous = temp;
  68.     first = temp;
  69. }
  70.  
  71. void Node::shift()
  72. {
  73.     List* temp = first;
  74.     first = first->next;
  75.     delete[] temp;
  76. }
  77.  
  78. void Node::pop()
  79. {
  80.     List* temp = last;
  81.     last = last->previous;
  82.     last->next = NULL;
  83.     delete[] temp;
  84. }
  85.  
  86. Node::~Node()
  87. {
  88.     while (first)
  89.     {
  90.         last = first->next;
  91.         delete[] first;
  92.         first = last;
  93.     }
  94. }
  95.  
  96. int main()
  97. {
  98.     Node list;
  99.     int n;
  100.     cout << "-----Push-----" << endl;
  101.     for (int i=1;i<6;i++)
  102.     {
  103.         list.push(i);
  104.         list.root();
  105.     }
  106.     cout << "-----Shift-----" << endl;
  107.     for (int i=1;i<5;i++)
  108.     {
  109.         list.shift();
  110.         list.root();
  111.     }
  112.     cout << "-----Unshift-----" << endl;
  113.     for (int i=1;i<6;i++)
  114.     {
  115.         list.unshift(i);
  116.         list.root();
  117.     }
  118.     cout << "-----Pop-----" << endl;
  119.     for (int i=1;i<6;i++)
  120.     {
  121.         list.pop();
  122.         list.root();
  123.     }
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement