Last active
February 19, 2023 16:18
-
-
Save Hemant-Parihar/cadbd330b871c682d3ce to your computer and use it in GitHub Desktop.
Array Implementation of STACK (abstract data type) in C programming.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Array implementation of the stack */ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#define MAX_STACK_SIZE 5 | |
typedef int boolean; | |
#define TRUE 1 | |
#define FALSE 0 | |
struct arrayStack { | |
int stk[MAX_STACK_SIZE]; | |
int top; | |
}; | |
typedef struct arrayStack arrayStack; // defining a new type arraystack of struct arrayStack. | |
//Function prototpye or method on stack. | |
void push(arrayStack *stack, int element); // This will push the element into the stack. | |
int pop(arrayStack *stack);//Pop will return and delete the last element of the stack. | |
int peek(arrayStack *stack);// Peek will return the top most element of the stack. | |
boolean isEmpty(arrayStack *stack); // isEmpty will tells weather a stack is empty. | |
void display(arrayStack *stack); | |
int main() { | |
int element, choice; | |
arrayStack stack; | |
arrayStack *stackptr = &stack; | |
stack.top = -1; | |
while(1) { | |
printf("Choose the operation you want to perform\n"); | |
printf("1. push \n2. pop \n3. peek \n4. display \n5. exit\n"); | |
printf("Enter the choice\n"); | |
scanf("%d", &choice); | |
switch(choice) { | |
case 1: | |
printf("Enter the element you wish to push\n"); | |
scanf("%d", &element); | |
push(stackptr, element); | |
break; | |
case 2: | |
printf("poped element is %d\n", pop(stackptr)); | |
break; | |
case 3: | |
printf("Top most element is %d\n", peek(stackptr)); | |
break; | |
case 4: | |
display(stackptr); | |
break; | |
case 5: | |
exit(0); | |
break; | |
default: | |
printf("Invalid input\n"); | |
} | |
} | |
return EXIT_SUCCESS; | |
} | |
void push(arrayStack *stack, int n) { | |
if(stack->top == MAX_STACK_SIZE - 1) { | |
printf("Warning: Stack is full, You can't add'\n"); | |
return; | |
} else { | |
stack->stk[++stack->top] = n; | |
} | |
} | |
int pop(arrayStack *stack) { | |
if (isEmpty(stack)) { // here i don't use & sign while passing pointer because stack is already a pointer to the stack. | |
printf("Stack is empty\n"); | |
return NULL; | |
} else { | |
int temp; | |
temp = stack->stk[stack->top--]; | |
return temp; | |
} | |
} | |
int peek(arrayStack *stack) { | |
if (isEmpty(stack)) { | |
printf("Stack is empty\n"); | |
return NULL; | |
} else { | |
return stack->stk[stack->top]; | |
} | |
} | |
int isEmpty(arrayStack *stack) { | |
if (stack->top == -1) | |
return TRUE; | |
else | |
return FALSE; | |
} | |
void display(arrayStack *stack) { | |
int i = stack->top; | |
for (; i >= 0; --i) | |
printf("%d\t", stack->stk[i]); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment