fork download
  1. //رودينا محمد هاشم محمد
  2. #include<iostream>
  3. using namespace std;
  4. typedef int ElmenetType;
  5. struct node
  6. {
  7. ElmenetType element;
  8. node* next;
  9. node* prev;
  10. };
  11. typedef node* Position;
  12.  
  13. class List
  14. {
  15. node* head;
  16. node* tail;
  17. int counter;
  18. public:
  19. List()
  20. {
  21. MakeNull();
  22. }
  23. void MakeNull()
  24. {
  25. head = new node;
  26. head->next = NULL;
  27. head->prev = NULL;
  28. tail = NULL;
  29. counter = 0;
  30. }
  31. Position End()
  32. {
  33. node* q = head;
  34. while (q->next != NULL)
  35. q = q->next;
  36. return q;
  37. }
  38. void Insert(ElmenetType x, Position pos)
  39. {
  40. Position t = new node;
  41. t->element = x;
  42.  
  43. if (pos == NULL)
  44. pos = End();
  45.  
  46. t->next = pos->next;
  47. t->prev = pos;
  48.  
  49. if (t->next != NULL)
  50. t->next->prev = t;
  51.  
  52. pos->next = t;
  53.  
  54. if (t->next == NULL)
  55. tail = t;
  56. counter++;
  57. }
  58. void Insertback(ElmenetType x)
  59. {
  60. Position p = End();
  61. Insert(x, p);
  62. }
  63. void Delete(Position p)
  64. {
  65. if (p == NULL || p == head)
  66. {
  67. cout << "Invalid position to delete \n";
  68. return;
  69. }
  70. if (p->next == NULL)
  71. tail = p->prev;
  72.  
  73. p->prev->next = p->next;
  74.  
  75. if (p->next != NULL)
  76. p->next->prev = p->prev;
  77.  
  78. delete p;
  79. counter--;
  80. }
  81. void PrintList()
  82. {
  83. cout << "List is : \n";
  84. Position q = head->next;
  85. while (q != NULL)
  86. {
  87. cout << q->element << " ";
  88. q = q->next;
  89. }
  90. cout << endl;
  91. }
  92. Position Locate(ElmenetType x)
  93. {
  94. Position p = head->next;
  95. while (p != NULL)
  96. {
  97. if (p ->element == x)
  98. return p;
  99. p = p->next;
  100. }
  101. return NULL ;
  102. }
  103. ElmenetType Retrieve(Position pos) {
  104. if (pos == NULL|| pos == head) {
  105. cout << "ERROR in reterive";
  106. return -1;
  107. }
  108. return pos->element;
  109. }
  110. Position First()
  111. {
  112. return head;
  113. }
  114. Position Next(Position pos)
  115. {
  116. if (pos == tail)
  117. return NULL;
  118. return pos->next;
  119. }
  120. Position Previous(Position pos)
  121. {
  122. if (pos == head)
  123. return NULL;
  124. return pos->prev;
  125. }
  126. int Size()
  127. {
  128. return counter;
  129. }
  130. friend void Reverse(List& l);
  131.  
  132.  
  133. };
  134. void Purge(List& l)
  135. {
  136. Position p = l.First()->next;
  137. while (p != NULL)
  138. {
  139. Position q = p->next;
  140. while (q != NULL)
  141. {
  142. if (p->element == q->element)
  143.  
  144. l.Delete(q);
  145.  
  146. else
  147. q = q->next;
  148. }
  149. p = p->next;
  150. }
  151. }
  152. void Reverse(List& l)
  153. {
  154. Position current = l.First();
  155. Position temp = NULL;
  156.  
  157. while (current != NULL) {
  158. temp = current->next;
  159. current->next = current->prev;
  160. current->prev = temp;
  161. current = temp;
  162. }
  163. }
  164.  
  165.  
  166.  
  167. int main()
  168. {
  169. List l;
  170. l.Insertback(10);
  171. l.Insertback(20);
  172. l.Insertback(30);
  173. l.Insertback(20);
  174. l.Insertback(60);
  175. l.Insertback(2);
  176. l.PrintList();
  177.  
  178. cout << "Size: " << l.Size() << endl;
  179. Purge(l);
  180. l.PrintList();
  181. Reverse(l);
  182. l.PrintList();
  183.  
  184. system("pause");
  185. return 0;
  186. }
Success #stdin #stdout #stderr 0.01s 5296KB
stdin
Standard input is empty
stdout
List is : 
10  20  30  20  60  2  
Size: 6
List is : 
10  20  30  60  2  
List is : 

stderr
sh: 1: pause: not found