Advertisement
ilnazEPTA

Untitled

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