From f017f492841781e7ff16d6de9df01eabc74230b9 Mon Sep 17 00:00:00 2001 From: divyansh739 <115534606+divyansh739@users.noreply.github.com> Date: Sat, 22 Oct 2022 20:08:28 +0530 Subject: [PATCH] Create divyansh02.cpp --- divyansh02.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 divyansh02.cpp diff --git a/divyansh02.cpp b/divyansh02.cpp new file mode 100644 index 0000000..b7ffed6 --- /dev/null +++ b/divyansh02.cpp @@ -0,0 +1,88 @@ + + +#include +#include + +using namespace std; + +#define MAX 10 +int size = 0; + +// Creating a stack +struct stack { + int items[MAX]; + int top; +}; +typedef struct stack st; + +void createEmptyStack(st *s) { + s->top = -1; +} + +// Check if the stack is full +int isfull(st *s) { + if (s->top == MAX - 1) + return 1; + else + return 0; +} + +// Check if the stack is empty +int isempty(st *s) { + if (s->top == -1) + return 1; + else + return 0; +} + +// Add elements into stack +void push(st *s, int newitem) { + if (isfull(s)) { + cout << "STACK FULL"; + } else { + s->top++; + s->items[s->top] = newitem; + } + size++; +} + +// Remove element from stack +void pop(st *s) { + if (isempty(s)) { + cout << "\n STACK EMPTY \n"; + } else { + cout << "Item popped= " << s->items[s->top]; + s->top--; + } + size--; + cout << endl; +} + +// Print elements of stack +void printStack(st *s) { + printf("Stack: "); + for (int i = 0; i < size; i++) { + cout << s->items[i] << " "; + } + cout << endl; +} + +// Driver code +int main() { + int ch; + st *s = (st *)malloc(sizeof(st)); + + createEmptyStack(s); + + push(s, 1); + push(s, 2); + push(s, 3); + push(s, 4); + + printStack(s); + + pop(s); + + cout << "\nAfter popping out\n"; + printStack(s); +}