Guest User

Untitled

a guest
Jan 17th, 2020
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Link {
  6. private:
  7. Link* next_;
  8. double value_;
  9. public:
  10. Link(Link *next, double value) : next_(next), value_(value) {}
  11. double getValue() { return value_; }
  12. Link *getNext() {return next_;}
  13. void setValue(double value) { value_ = value; }
  14. void setNext(Link* next) { next_ = next; }
  15. };
  16.  
  17.  
  18. class List {
  19. Link* first_;
  20. Link* current_;
  21. public:
  22.  
  23. List() : first_(nullptr), current_(nullptr) {}
  24. void show() {
  25. Link* tmp = first_;
  26. while (tmp) {
  27. cout << tmp->getValue() << " ";
  28. tmp = tmp->getNext();
  29. }
  30. }
  31.  
  32.  
  33. bool isEmpty() { return (first_ == nullptr) ? true : false; }
  34. void init() { current_ = first_;}
  35. void next() { current_ = current_ ? current_->getNext() : nullptr; }
  36. void add(double value) {
  37. first_ = new Link(first_, value);
  38. init();
  39.  
  40. while (current_) {
  41. if (current_->getNext()) {
  42. if (current_->getNext()->getValue() < current_->getValue()) {
  43. double tmp = current_->getValue();
  44. current_->setValue(current_->getNext()->getValue());
  45. current_->getNext()->setValue(tmp);
  46. }
  47. }
  48. current_ = current_->getNext();
  49. }
  50. }
  51.  
  52.  
  53.  
  54.  
  55. void removeFirst() {
  56. Link* tmp = first_;
  57. first_ = first_->getNext();
  58. delete tmp;
  59. }
  60.  
  61.  
  62. void pop2(double toRemove) {
  63. Link* tmp = nullptr;
  64. while (first_ && first_->getValue() == toRemove)
  65. removeFirst();
  66.  
  67. init();
  68. while (current_->getNext()) {
  69. if (current_->getNext()->getValue() == toRemove) {
  70. tmp = current_->getNext();
  71. current_->setNext(current_->getNext()->getNext());
  72. delete tmp;
  73. }
  74. else {
  75. current_ = current_->getNext();
  76. }
  77.  
  78.  
  79. }
  80. }
  81.  
  82.  
  83. };
  84.  
  85. int main()
  86. {
  87. List lista;
  88. lista.add(98);
  89. lista.add(33);
  90. lista.add(97);
  91. lista.add(111);
  92. lista.add(92);
  93. lista.add(92);
  94. lista.add(92);
  95. lista.add(178);
  96.  
  97. lista.pop2(92);
  98.  
  99. lista.show();
  100.  
  101. return 0;
  102. }
RAW Paste Data