Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. // UNIQ
  3.  
  4. using namespace std;
  5.  
  6. struct stack {
  7.  
  8. public:
  9.     int value;
  10.     stack* next;
  11.  
  12.     stack();
  13.     void print();
  14.     void push(int i);
  15.     bool is_empty();
  16.     void delete_if(bool (*p)(stack*));
  17.     void delete_all(bool (*p)(stack*));
  18.     void delete_if_value(int value);
  19.     stack* pop();
  20.     stack* top();
  21.     bool exists(int val);
  22.  
  23. };
  24.  
  25. int GLOBAL_VALUE=0;
  26.  
  27. stack::stack(){
  28.     next = NULL;
  29.     value = 0;
  30. }
  31.  
  32. bool stack::is_empty(){
  33.     return this->next == NULL ? true : false;
  34. }
  35.  
  36. void stack::push(int i){
  37.     stack* last = this->top();
  38.  
  39.     last->next = new stack();
  40.     last->next->value = i;
  41. }
  42.  
  43. stack* stack::pop(){
  44.  
  45.     stack* t = this;
  46.  
  47.     while(!this->is_empty()){
  48.  
  49.         if(t->next->next != NULL){
  50.             t = t->next;
  51.         } else {
  52.             stack* obj = t->next;
  53.             t->next = NULL;
  54.             return obj;
  55.         };
  56.  
  57.     }
  58.  
  59.     return NULL;
  60. }
  61.  
  62. stack* stack::top(){
  63.     stack* t = this;
  64.     while(true){
  65.  
  66.         if(t->next != NULL){
  67.             t = t->next;
  68.         } else {
  69.             break;
  70.         };
  71.  
  72.     }
  73.     return t;
  74. }
  75.  
  76. void stack::print(){
  77.     stack* t = this->next;
  78.     while(!this->is_empty()){
  79.         cout << t->value << endl;
  80.  
  81.         if(t->next != NULL){
  82.             t = t->next;
  83.         } else {
  84.             break;
  85.         };
  86.  
  87.     }
  88. }
  89.  
  90. void stack::delete_if_value(int value){
  91.     stack* t = this->next;
  92.     stack* prev = this;
  93.     while(!this->is_empty()){
  94.  
  95.         if(t->value == value){
  96.             prev->next = t->next;
  97.         }else{
  98.             prev=t;
  99.         }
  100.  
  101.  
  102.         if(t->next!=NULL){
  103.             t = t->next;
  104.         } else {
  105.             break;
  106.         };
  107.  
  108.     }
  109. }
  110.  
  111.  
  112. void stack::delete_if(bool (*p)(stack* el)){
  113.     stack* t = this->next;
  114.     stack* prev = this;
  115.     while(!this->is_empty()){
  116.  
  117.         if((*p)(t)){
  118.             prev->next = t->next;
  119.         }else{
  120.             prev=t;
  121.         }
  122.  
  123.  
  124.         if(t->next!=NULL){
  125.             t = t->next;
  126.         } else {
  127.             break;
  128.         };
  129.  
  130.     }
  131. }
  132.  
  133. void stack::delete_all(bool (*p)(stack* el)){
  134.     stack* t = this->next;
  135.     stack* prev = this;
  136.     while(!this->is_empty()){
  137.  
  138.         if((*p)(t)){
  139.             t->delete_if_value(t->value);
  140.             prev->next = t->next;
  141.         }else{
  142.             prev=t;
  143.         }
  144.  
  145.         if(t->next!=NULL){
  146.             t = t->next;
  147.         } else {
  148.             break;
  149.         };
  150.  
  151.     }
  152. }
  153.  
  154.  
  155. bool stack::exists(int value){
  156.  
  157.     stack* t = this->next;
  158.     if(this->is_empty()) return false;
  159.     while(true){
  160.  
  161.         if(t->value == value){
  162.             return true;
  163.         }
  164.  
  165.         if(t->next != NULL){
  166.             t = t->next;
  167.         } else {
  168.             break;
  169.         };
  170.  
  171.     }
  172.  
  173.     return false;
  174. }
  175.  
  176. bool condition(stack* s){
  177.     return s->exists(s->value);
  178. }
  179.  
  180. int main()
  181. {
  182.     int s;
  183.     stack a;
  184.  
  185.     cout << "Enter size: " << endl;
  186.     cin >> s;
  187.  
  188.     for(int i=0; i<s; i++){
  189.         cout << "Enter element #"<< i<< endl;
  190.         int tmp;
  191.         cin >> tmp;
  192.         a.push(tmp);
  193.     }
  194.  
  195.     cout << "--------------------" << endl;
  196.     a.print();
  197.  
  198.     a.delete_all(condition);
  199.     cout << "--------------------" << endl;
  200.     a.print();
  201.  
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement