Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Link
  6. {
  7. Link *next_;
  8. int *value_;
  9. public:
  10.  
  11. Link(Link *next, int *value)
  12. {
  13. next_=next;
  14. value_=value;
  15. }
  16.  
  17. int *getValue()
  18. {
  19. return value_;
  20. }
  21.  
  22. Link *getNext()
  23. {
  24. return next_;
  25.  
  26. }
  27.  
  28. void setNext(Link *link)
  29. {
  30. next_=link;
  31. }
  32.  
  33.  
  34. };
  35.  
  36. class Lista{
  37. Link *first_;
  38.  
  39. Link *current_;
  40.  
  41. public:
  42.  
  43. Lista()
  44. {
  45. first_=nullptr;
  46. }
  47.  
  48. void display()
  49. {
  50. Link *temp;
  51. temp=first_;
  52. while(temp!=nullptr)
  53. {
  54. cout<< *temp->getValue()<<" ";
  55. temp=temp->getNext();
  56.  
  57. }
  58.  
  59. }
  60.  
  61. void dodaj(int *toAdd)
  62. {
  63. first_=new Link (first_, toAdd);
  64. }
  65.  
  66. void init()
  67. {
  68. current_=first_;
  69. }
  70.  
  71. void removeFirst()
  72. {
  73. Link *temp;
  74. temp=first_;
  75. first_= first_->getNext();
  76. delete temp;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. void usun(int toDel)
  83.  
  84. {
  85.  
  86. while((first_!=nullptr ) && (*first_->getValue()!= toDel))
  87. {
  88. removeFirst();
  89. }
  90. init();
  91. Link *next;
  92. Link *next_v2;
  93. while(current_!=nullptr)
  94. {
  95. next=current_->getNext();
  96. if(*next->getValue()==toDel)
  97. {
  98.  
  99. next_v2=next;
  100. current_->setNext(next->getNext());
  101.  
  102. delete next_v2;
  103. }
  104. else
  105. {
  106. current_=current_->getNext();
  107.  
  108. }
  109.  
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116. };
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. int main()
  130. {
  131. Lista lista;
  132.  
  133.  
  134.  
  135.  
  136. lista.dodaj(new int(5));
  137. lista.dodaj(new int(10));
  138.  
  139. lista.display();
  140. cout<<endl;
  141. lista.usun(5);
  142.  
  143. lista.display();
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement