Advertisement
Guest User

Untitled

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