Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created November 14, 2019 15:56
Show Gist options
  • Save surinoel/bc18263638c34744cac039d874ac1aaf to your computer and use it in GitHub Desktop.
Save surinoel/bc18263638c34744cac039d874ac1aaf to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node *top = NULL;
void push(int num) {
struct Node *tmp = (struct Node *)malloc(sizeof(struct Node));
tmp->data = num;
tmp->next = top;
top = tmp;
}
void pop() {
struct Node *tmp = top;
if (tmp != NULL) {
cout << "pop data : " << tmp->data << '\n';
top = top->next;
free(tmp);
}
}
void display() {
struct Node *ptr = top;
while (ptr != NULL) {
cout << ptr->data << '\n';
ptr = ptr->next;
}
}
int main() {
int ch, val;
cout << "1) Push in stack" << endl;
cout << "2) Pop from stack" << endl;
cout << "3) Display stack" << endl;
cout << "4) Exit" << endl;
do {
cout << "Enter choice: " << endl;
cin >> ch;
switch (ch) {
case 1: {
cout << "Enter value to be pushed:" << endl;
cin >> val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout << "Exit" << endl;
break;
}
default: {
cout << "Invalid Choice" << endl;
}
}
} while (ch != 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment