Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. struct Double_node{
  6. int val;
  7. Double_node * next;
  8. Double_node * prev;
  9.  
  10. };
  11.  
  12.  
  13. class Double_list{
  14. private:
  15.     Double_node *tail;
  16. public:
  17.     Double_list();
  18.     Double_list(int);
  19.     ~Double_list();
  20.     bool empty();
  21.     void push_front(int);
  22.     void pop_front();
  23.     void push_back(int);
  24.     void pop_back();
  25.     void clear();
  26.     int& front();
  27.     int& back();
  28.     int size();
  29.     void print();
  30. };
  31.  
  32. Double_list::Double_list()
  33. {
  34.     tail=0;
  35.  
  36. }
  37. Double_list::Double_list(int a)
  38. {
  39.             Double_node *p = new Double_node;
  40.             p->val=0;
  41.             p->prev=tail;
  42.             p->next=tail;
  43. }
  44.  
  45. bool Double_list::empty()
  46. {
  47.     if(tail->next==tail->prev)
  48.         return true;
  49.         else return false;
  50. }
  51. void Double_list::push_front(int a)
  52. {
  53.     Double_node *p= new Double_node;
  54.     p->val = a;
  55.     p->prev = tail;
  56.     p->next = tail->next;
  57. }
  58. void Double_list::pop_front()
  59. {
  60.     if(tail->next!=tail->prev)
  61.     {
  62.         Double_node *node = tail->next->next;
  63.         delete tail->next;
  64.         tail->next = node;
  65.     }
  66. }
  67. void Double_list::clear()
  68. {
  69.     while(tail->next!= tail->prev)
  70.     {
  71.         Double_node *node = tail->next->next;
  72.         delete tail->next;
  73.         tail->next = node;
  74.     }
  75.  
  76. }
  77. int& Double_list::front()
  78. {
  79.     return tail->next->val;
  80. }
  81. int Double_list::size()
  82. {
  83.     Double_node *p=tail->next;
  84.     Double_node *t=tail;
  85.     int k=0;
  86.     while(p->next != t)
  87.     {
  88.         p=p->next;
  89.         k++;
  90.     }
  91.     return k;
  92. }
  93. void Double_list::print()
  94. {
  95.     Double_node *p=tail->next;
  96.     while(p != tail)
  97.     {
  98.         cout<<p->val<<" ";
  99.         Double_node *node = p->next;
  100.         p = node;
  101.     }
  102.     cout<<"\n";
  103. }
  104. Double_list::~Double_list()
  105. {
  106.  
  107.    while(tail->prev != tail->next )
  108.     {
  109.         Double_node *node = tail->next->next;
  110.         delete tail->next;
  111.         tail->next = node;
  112.     }
  113.  
  114. }
  115.  
  116.  
  117. int main()
  118. {
  119.     Double_list();
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement