Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I can't tell you what it really is,
- // I can only tell you what it feels like.
- #include "bits/stdc++.h"
- using namespace std;
- class node{
- public:
- int data;
- node *next;
- node() {
- next = NULL;
- }
- };
- node *top;
- void add(int x) {
- if(top -> next == NULL) {
- top -> data = x;
- top -> next = top;
- } else {
- node *temp = new node;
- temp -> data = x;
- temp -> next = top;
- top = temp;
- }
- }
- void del() {
- if(top -> next == NULL) {
- cout << "Stack is Empty\n";
- } else {
- if(top == top -> next) {
- top -> next = NULL;
- } else {
- node *temp = top;
- top = top -> next;
- delete temp;
- }
- }
- }
- int on() {
- if(top -> next == NULL)
- return -1;
- else
- return top -> data;
- }
- int main() {
- int ch, num;
- top = new node;
- while(1) {
- cin >> ch;
- if(ch == 1) {
- cout << "enter num ";
- cin >> num;
- add(num);
- }
- else if(ch == 2){
- del();
- }
- else if(ch == 3)
- cout << on() << '\n';
- else
- break;
- }
- }
Add Comment
Please, Sign In to add comment