Stack Implementation
Stack Implementation
Stack Implementation
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function prototypes
void push(); // Push element to the top of the stack
int pop(); // Remove and return the top most element of the
stack
int peek(); // Return the top most element of the stack
bool isEmpty(); // Check if the stack is in Underflow state or
not
bool isFull(); // Check if the stack is in Overflow state or
not
int main(){
printf("STATIC ARRAY (Total Capacity: %d)\n", N);
int choice;
while(1){
printf("\nChoose any of the following options:\n");
printf(" 0: Exit 1: Push 2: Pop
3: Peek\n");
printf(" 4: Check if the stack is empty 5: Check if the
stack is full\n\n");
scanf("%d", &choice);
switch(choice){
case 0: exit(0);
case 1: push(); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: isEmpty(); break;
case 5: isFull(); break;
default: printf("Please choose a correct option!");
}
}
return 0;
}
void push(){
// Checking overflow state
if(top == N-1)
printf("Overflow State: can't add more elements into the
stack\n");
else{
int x;
printf("Enter element to be pushed into the stack: ");
scanf("%d", &x);
top+=1;
stack[top] = x;
}
}
int pop(){
// Checking underflow state
if(top == -1)
printf("Underflow State: Stack already empty, can't remove
any element\n");
else{
int x = stack[top];
printf("Popping %d out of the stack\n", x);
top-=1;
return x;
}
return -1;
}
int peek(){
int x = stack[top];
printf("%d is the top most element of the stack\n", x);
return x;
}
bool isEmpty(){
if(top == -1){
printf("Stack is empty: Underflow State\n");
return true;
}
printf("Stack is not empty\n");
return false;
}
bool isFull(){
if(top == N-1){
printf("Stack is full: Overflow State\n");
return true;
}
printf("Stack is not full\n");
return false;
}
OUTPUT
STATIC ARRAY (Total Capacity: 1000)
1
Enter element to be pushed into the stack: 7
1
Enter element to be pushed into the stack: 6
2
Popping 6 out of the stack
// Function prototypes
void push(); // Push element to the top of the stack
int pop(); // Remove and return the top most element of the stack
int peek(); // Return the top most element of the stack
bool isEmpty(); // Check if the stack is in Underflow state or not
struct node{
int val;
struct node *next;
};
if(head == NULL){
ptr->val = val;
ptr->next = NULL;
head = ptr;
}
else{
ptr->val = val;
ptr->next = head;
head=ptr;
}
}
int pop(){
int item;
struct node *ptr;
if (head == NULL)
printf("Underflow State: can't remove any item");
else{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("%d is popped out of the stack", item);
return item;
}
return -1;
}
int peek(){
int x = head->val;
printf("%d is the top most element of the stack\n", x);
return x;
}
bool isEmpty(){
if(head == NULL){
printf("Stack is empty: Underflow State\n");
return true;
}
printf("Stack is not empty\n");
return false;
}
Output
DYNAMIC STACK
1
Enter the value to be pushed: 56
1
Enter the value to be pushed: 76
2
76 is popped out of the stack