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