From 6cf42d7b95d220bb3da1e7485c4584e8fe491257 Mon Sep 17 00:00:00 2001 From: asad-mansoor <116182678+asad-mansoor@users.noreply.github.com> Date: Wed, 19 Oct 2022 21:26:17 +0530 Subject: [PATCH] Create dsa_asad.c --- dsa_asad.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 dsa_asad.c diff --git a/dsa_asad.c b/dsa_asad.c new file mode 100644 index 0000000..00795f2 --- /dev/null +++ b/dsa_asad.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +// size of the stack +int size = 8; + +// stack storage +int intArray[8]; + +// top of the stack +int top = -1; + +// Operation : Pop +// pop item from the top of the stack +int pop() { + //retrieve data and decrement the top by 1 + return intArray[top--]; +} +// Operation : Peek +// view the data at top of the stack +int peek() { + //retrieve data from the top + return intArray[top]; +} +//Operation : isFull +//return true if stack is full +bool isFull(){ + return (top == size-1); +} + +// Operation : isEmpty +// return true if stack is empty +bool isEmpty(){ + return (top == -1); +} +// Operation : Push +// push item on the top of the stack +void push(int data) { + if(!isFull()){ + // increment top by 1 and insert data + intArray[++top] = data; + } else { + printf("Cannot add data. Stack is full.\n"); + } +} +main() { + // push items on to the stack + push(3); + push(5); + push(9); + push(1); + push(12); + push(15); + + printf("Element at top of the stack: %d\n" ,peek()); + printf("Elements: \n"); + + // print stack data + while(!isEmpty()){ + int data = pop(); + printf("%d\n",data); + } + printf("Stack full: %s\n" , isFull()?"true":"false"); + printf("Stack empty: %s\n" , isEmpty()?"true":"false"); +}