Wednesday, December 18, 2019

C++ Program to Implement Stack Using Two Queues

/* Problem statement- Implement a Stack using two Queues. Explanation- stack has three operations namely push, pop and top where push pushes an input to stack, pop removes topmost element and top displays the topmost element of stack. Approach - Consider two queues queue1 and queue2. push(x) - First push element x to queue1 and one by one pop all elements in queue2 and add these elements to queue1. Then, exchange queue1 and queue2. pop()- pop an element from queue2. top() - show front of queue2. Example- Input : push(1), push(10), push(12), pop(),top(),pop(),top() initially: queue1: queue2: push(1): intermediate steps- step-1 : queue1: 1 queue2: step-2 : queue1: 1 queue2: finally : queue1: queue2: 1 push(10): intermediate steps- step-1 : queue1: 10 queue2: 1 steps-2 : queue1:10 1 queue2: finally : queue1: queue2: 10 1 push(12): intermediate steps- step-1 : queue1: 12 queue2: 10 1 steps-2 : queue1:12 10 1 queue2: finally : queue1: queue2:12 10 1 pop(): queue1: queue2:10 1 top(): 10 pop(): queue1: queue2:1 top(): 1 */ //code //Note- please push only natural numbers #include<iostream> #include<queue> //queue from STL using namespace std; class stack{ private: queue<int> queue1,queue2; public: stack(); void push(int); void pop(); int top(); ~stack(); }; stack::stack() { } void stack::push(int x) { queue1.push(x); while(queue2.size()!=0) { queue1.push(queue2.front()); queue2.pop(); } queue1.swap(queue2); } void stack::pop() { if(queue2.size()==0) cout<<"No elements in stack to pop"<<endl; else queue2.pop(); } int stack::top() { if(queue2.size()==0) { cout<<"No elements in stack to show"<<endl; return false; } else { return queue2.front(); } } stack::~stack() { while(queue2.size()!=0) queue2.pop(); } int main() { int ans; stack st; while(1) { cout<<"Press 1 to push, 2 to pop and 3 to view top and 4 to exit"<<endl; cin>>ans; if(ans==1) { int inp; cout<<"Enter element to push"<<endl; cin>>inp; st.push(inp); } else if(ans==2) { st.pop(); cout<<"Popped successfully"<<endl; } else if(ans==3) { if(st.top()) cout<<"topmost element is "<<st.top()<<endl; } else if(ans==4) break; } return 0; } /* Sample output of code- Press 1 to push, 2 to pop and 3 to view top and 4 to exit 1 Enter element to push 1 Press 1 to push, 2 to pop and 3 to view top and 4 to exit 1 Enter element to push 10 Press 1 to push, 2 to pop and 3 to view top and 4 to exit 1 Enter element to push 12 Press 1 to push, 2 to pop and 3 to view top and 4 to exit 2 Popped successfully Press 1 to push, 2 to pop and 3 to view top and 4 to exit 3 topmost element is 10 Press 1 to push, 2 to pop and 3 to view top and 4 to exit 2 Popped successfully Press 1 to push, 2 to pop and 3 to view top and 4 to exit 3 topmost element is 1 Press 1 to push, 2 to pop and 3 to view top and 4 to exit */

Arrays in Solidity Programming Language.

Arrays Solidity supports both generic and byte arrays. It supports both fixed size and dynamic arrays. It also supports multidimensional ...