Skip to content

Instantly share code, notes, and snippets.

@ahmedalkabir
Last active November 10, 2018 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedalkabir/2062e2d1b79244994e35223fec613477 to your computer and use it in GitHub Desktop.
Save ahmedalkabir/2062e2d1b79244994e35223fec613477 to your computer and use it in GitHub Desktop.
remove 4 element
#include<iostream>
#include<array>
using namespace std;
// Stack Container to simplify Stack
// operations
template<typename T,int SIZE>
struct Stack{
// array<T, SIZE> elements;
T elements[SIZE];
int &operator[](int i) {
if(top > -1)
return elements[i];
}
void push(T el){
if(top >= SIZE){
cout << "STACK IS FULL" << endl;
}else{
elements[++top] = el;
}
}
int pop(void){
if(top <= -1){
cout << "STACK is empty" << endl;
}else{
return elements[top--];
}
}
void show(void){
if(top >= 0){
cout << "The elements of STACK is ......" << endl;
for(int i=top; i >= 0; i--)
cout << "The Element : " << elements[i] << endl;
}
}
private:
int top = -1;
};
int main(void){
Stack<int, 4> s1;
Stack<int, 4> s2;
Stack<int, 4> s3;
s1.push(4);
s1.push(3);
s1.push(2);
s1.push(1);
cout << "STACK 1" << endl;
s1.show();
// we moved 1 to s2 and 2 to s3
s2.push(s1.pop());
s3.push(s1.pop());
// show the stack 1
cout << "STACK 1" << endl;
s1.show();
// we moved 1 to s3 top of 2 because it's smaller than it
// and s2 now is empty
s3.push(s2.pop());
cout << "STACK 3" << endl;
s3.show();
// we moved 3 to s2
s2.push(s1.pop());
// show the stack 1
cout << "STACK 1" << endl;
s1.show();
// now we delete 4 from s1
cout << "POP UP THE " << s1.pop() << " FROM STACK 1" << endl;
// let's move the 1, 2, and 3 to s1
s1.push(s2.pop());
s2.push(s3.pop());
s1.push(s3.pop());
s1.push(s2.pop());
// show the stack 1
cout << "STACK 1" << endl;
s1.show();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment