Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- // UNIQ
- using namespace std;
- struct stack {
- public:
- int value;
- stack* next;
- stack();
- void print();
- void push(int i);
- bool is_empty();
- void delete_if(bool (*p)(stack*));
- void delete_all(bool (*p)(stack*));
- void delete_if_value(int value);
- stack* pop();
- stack* top();
- bool exists(int val);
- };
- int GLOBAL_VALUE=0;
- stack::stack(){
- next = NULL;
- value = 0;
- }
- bool stack::is_empty(){
- return this->next == NULL ? true : false;
- }
- void stack::push(int i){
- stack* last = this->top();
- last->next = new stack();
- last->next->value = i;
- }
- stack* stack::pop(){
- stack* t = this;
- while(!this->is_empty()){
- if(t->next->next != NULL){
- t = t->next;
- } else {
- stack* obj = t->next;
- t->next = NULL;
- return obj;
- };
- }
- return NULL;
- }
- stack* stack::top(){
- stack* t = this;
- while(true){
- if(t->next != NULL){
- t = t->next;
- } else {
- break;
- };
- }
- return t;
- }
- void stack::print(){
- stack* t = this->next;
- while(!this->is_empty()){
- cout << t->value << endl;
- if(t->next != NULL){
- t = t->next;
- } else {
- break;
- };
- }
- }
- void stack::delete_if_value(int value){
- stack* t = this->next;
- stack* prev = this;
- while(!this->is_empty()){
- if(t->value == value){
- prev->next = t->next;
- }else{
- prev=t;
- }
- if(t->next!=NULL){
- t = t->next;
- } else {
- break;
- };
- }
- }
- void stack::delete_if(bool (*p)(stack* el)){
- stack* t = this->next;
- stack* prev = this;
- while(!this->is_empty()){
- if((*p)(t)){
- prev->next = t->next;
- }else{
- prev=t;
- }
- if(t->next!=NULL){
- t = t->next;
- } else {
- break;
- };
- }
- }
- void stack::delete_all(bool (*p)(stack* el)){
- stack* t = this->next;
- stack* prev = this;
- while(!this->is_empty()){
- if((*p)(t)){
- t->delete_if_value(t->value);
- prev->next = t->next;
- }else{
- prev=t;
- }
- if(t->next!=NULL){
- t = t->next;
- } else {
- break;
- };
- }
- }
- bool stack::exists(int value){
- stack* t = this->next;
- if(this->is_empty()) return false;
- while(true){
- if(t->value == value){
- return true;
- }
- if(t->next != NULL){
- t = t->next;
- } else {
- break;
- };
- }
- return false;
- }
- bool condition(stack* s){
- return s->exists(s->value);
- }
- int main()
- {
- int s;
- stack a;
- cout << "Enter size: " << endl;
- cin >> s;
- for(int i=0; i<s; i++){
- cout << "Enter element #"<< i<< endl;
- int tmp;
- cin >> tmp;
- a.push(tmp);
- }
- cout << "--------------------" << endl;
- a.print();
- a.delete_all(condition);
- cout << "--------------------" << endl;
- a.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement