Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. //test: 2 1 2 1 2
  4. using namespace std;
  5. class List;
  6.  
  7.  
  8. class Element
  9. {
  10. Element *next;
  11. int value;
  12. public:
  13. Element()
  14. {
  15. next = NULL;
  16. }
  17.  
  18.  
  19. friend class List;
  20. };
  21.  
  22. class List
  23. {
  24. Element *first;
  25.  
  26. public:
  27.  
  28. void Add(int number)
  29. {
  30. Element *new_element = new Element;
  31. new_element->value = number;
  32.  
  33. if (first == NULL)
  34. {
  35. first = new_element;
  36. }
  37. else
  38. {
  39. new_element->next = first;
  40. first = new_element;
  41. }
  42. }
  43.  
  44. void Display()
  45. {
  46. Element *temp = first;
  47.  
  48. while (temp)
  49. {
  50.  
  51. cout << temp->value << " ";
  52. temp = temp->next;
  53. }
  54. }
  55.  
  56. void Delete(int number)
  57. {
  58.  
  59. if (first->value == number)
  60. {
  61.  
  62. first = first->next;
  63. }
  64. Element *temp = first;
  65. while (temp)
  66. {
  67.  
  68. if (first->value == number)
  69. {
  70.  
  71. if (temp->next == NULL)
  72. {
  73.  
  74. temp = NULL;
  75. }
  76. else
  77. {
  78.  
  79. temp = temp->next;
  80. }
  81. }
  82. else
  83. temp = temp->next;
  84.  
  85. }
  86.  
  87. }
  88.  
  89. List()
  90. {
  91. first = NULL;
  92. }
  93.  
  94.  
  95. friend class Element;
  96. };
  97.  
  98.  
  99. int main()
  100. {
  101.  
  102. List myList;
  103. int M, value;
  104. cin >> M;
  105. for (int i = 0; i < M; i++)
  106. {
  107. cin >> value;
  108. myList.Add(value);
  109. }
  110.  
  111. int N;
  112. cin >> N;
  113. /*for (int i = 0; i < N; i++)
  114. {
  115. cin >> value;
  116. myList.Delete(value);
  117. }
  118. */
  119. myList.Display();
  120.  
  121. system("PAUSE");
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement