fork download
  1. // #include <bits/stdc++.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. //التيل باشر ع الهيد عشان تصير سيركيولار
  6.  
  7. class node {
  8. public:
  9. node* next, * prev;
  10. int data;
  11. node(int d, node* n = 0, node* p = 0) {
  12. data = d;
  13. next = n;
  14. prev = p;
  15. }
  16. };
  17.  
  18. class list{
  19. node *tail;//need only tail
  20.  
  21. public :
  22. list();
  23. bool is_empty();
  24. void printf();
  25. void printb();
  26. int size();
  27. void add_begin(int el);
  28. void add_end(int el);
  29. void add_pos( int el , int pos);
  30. void delete_begin();
  31. void delete_end();
  32. void delete_pos(int pos);
  33. void delete_el(int el);
  34. void add_sorted(int el);
  35. int search(int el);
  36. ~list();//destructer to delete the pointers from the dynamic memory to save it
  37. void operator=(list &o);//when we let p1=p2 , we have 2 pointers point at the same place , when we delete the first
  38. // //, the want to delete the second , there is no pointer to delete
  39. list(list &o);//the same as operator
  40. };
  41. bool list::is_empty() {
  42. return (tail == 0);
  43. }
  44. int list::size() {
  45. if (is_empty())
  46. return 0;
  47. else {
  48. node* t = tail->next;
  49. int c = 1;
  50. for (; t != tail; t = t->next, c++);
  51. return c;
  52. }
  53. }
  54. list::~list(){
  55. while(!is_empty())
  56. delete_end();
  57. cout<<"list is deleted"<<endl;
  58. }
  59. void list::delete_end(){
  60. if(tail==0){
  61. cout<<"the list is empty"<<endl;
  62. }
  63. else if(tail->next == tail){//tail point to tail , delete only tail
  64. delete tail;
  65. tail =0;
  66. }
  67. else {
  68. node *tmp = tail->next;//point to the first node
  69. for( ;tmp->next!=tail;tmp=tmp->next);//loop until reach tail -1
  70. tmp->next = tail->next;
  71. delete tail;
  72. tail = tmp;
  73. }
  74. }
  75. void list::add_begin(int el){
  76. if(tail==0){
  77. tail= new node(el);
  78. tail->next = tail->prev = tail;
  79. }
  80. else {
  81. tail->next = tail->next->prev = new node(el , tail->next , tail);
  82. }
  83. }
  84. list ::list(){
  85. tail =0;
  86. }
  87.  
  88. void list::printf(){
  89. node *tmp = tail->next;//ref to first point
  90. while(tmp!=tail){
  91. cout<<tmp->data<<" ";
  92. tmp = tmp->next;
  93. }
  94. cout<<tmp->data<<" ";//for the tail
  95. cout<<endl;
  96. }
  97. void list::printb(){
  98. node *tmp = tail;//ref to first point
  99. while(tmp->prev!=tail){
  100. cout<<tmp->data<<" ";
  101. tmp = tmp->prev;
  102. }
  103. cout<<tmp->data<<" ";//for the tail
  104. cout<<endl;
  105. }
  106. void list::add_end(int el){
  107. if(tail==0){
  108. tail->next = tail->prev = new node(el);
  109. }
  110. else {
  111. tail = tail->next = new node(el ,tail->next ,tail);
  112. tail->next->prev = tail;
  113. }
  114. }
  115. void list::add_pos(int el , int pos){
  116. if(pos<1 || pos>size()+1){
  117. cout<<"invalid pos "<<endl;
  118. }
  119. else if (pos==1){
  120. add_begin(el);
  121. }
  122. else if (pos==size()+1){
  123. add_end(el);
  124. }
  125. else {
  126. node *t = tail->next;
  127. int i = 1;
  128. for( ; i<pos-1;i++)t=t->next;
  129. t->next = t->next->prev = new node(el , t->next , t);
  130.  
  131. }
  132.  
  133. }
  134.  
  135.  
  136.  
  137. int main() {
  138. list l;
  139. l.add_begin(1);
  140. l.add_end(1);
  141. l.add_pos(2 , 2);
  142. l.add_pos(4 , 4);
  143. l.printf();
  144. l.printb();
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
1 2 1 4 
4 1 2 1 
list is deleted