Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <bits/stdc++.h>
- using namespace std;
- struct StackNode {
- int data;
- StackNode *next;
- StackNode(int a) {
- data = a;
- next = NULL;
- }
- };
- class MyStack {
- private:
- StackNode *top;
- public:
- void push(int);
- int pop();
- MyStack() { top = NULL; }
- };
- int main() {
- int T;
- cin >> T;
- while (T--) {
- MyStack *sq = new MyStack();
- int Q;
- cin >> Q;
- while (Q--) {
- int QueryType = 0;
- cin >> QueryType;
- if (QueryType == 1) {
- int a;
- cin >> a;
- sq->push(a);
- } else if (QueryType == 2) {
- cout << sq->pop() << " ";
- }
- }
- cout << endl;
- }
- }
- // } Driver Code Ends
- //Function to push an integer into the stack.
- void MyStack ::push(int x)
- {
- // Your Code
- if(top==NULL)
- {
- StackNode* n=new StackNode(x);
- top=n;
- }
- else
- {
- StackNode* n=new StackNode(x);
- StackNode* tmp=top;
- n->next=top;
- top=n;
- }
- }
- //Function to remove an item from top of the stack.
- int MyStack ::pop()
- {
- // Your Code
- int ans=-1;
- if(top==NULL)
- {
- return -1;
- }
- else
- {
- ans=top->data;
- top=top->next;
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment