ARRAY IMPLEMENTATION OF STACK ADT
Aim:
To write a C program for Array implementation of Stack ADT.
Algorithm :
1. Initialize the Stack
- Create an integer array `stack[MAX]` to hold stack elements.
- Set `top` to `-1` to indicate the stack is empty.
2. Check if the Stack is Empty (`isEmpty`)
- Return `true` if `top` is `-1`, otherwise `false`.
3. Check if the Stack is Full (`isFull`)
- Return `true` if `top` is `MAX - 1`, otherwise `false`.
4. Push an Element (`push`)
- If the stack is full (`isFull` is `true`), print an error message and stop.
- Otherwise, increment `top` and assign the new value to `stack[top]`.
5. Pop an Element (`pop`)
- If the stack is empty (`isEmpty` is `true`), print an error message and return `-1`.
- Otherwise, return `stack[top]` and decrement `top`.
6. Peek at the Top Element (`peek`)
- If the stack is empty (`isEmpty` is `true`), print an error message and return `-1`.
- Otherwise, return `stack[top]` without changing `top`.
7. Display Stack Elements (`display`)
- If the stack is empty (`isEmpty` is `true`), print a message indicating the stack is empty.
- Otherwise, print all elements from `stack[top]` to `stack[0]`.
8. Main Execution Loop
- Repeatedly display a menu with options (Push, Pop, Peek, Display, Exit).
- Perform the selected operation and display the result.
- Exit when the user chooses to do so.
Program
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 // Maximum size of the stack
// Global variables for the stack
int stack[MAX];
int top = -1; // Initialize top to -1 indicating the stack is empty
// Function to check if the stack is empty
int isEmpty()
{
return top == -1;
}
// Function to check if the stack is full
int isFull()
{
return top == MAX - 1;
}
// Function to add an element to the stack (push operation)
void push(int value)
{
if (isFull())
{
printf("Stack overflow. Cannot push %d.\n", value);
return;
}
stack[++top] = value;
printf("Pushed %d to stack.\n", value);
}
// Function to remove and return the top element of the stack (pop operation)
int pop()
{
if (isEmpty())
{
printf("Stack underflow. Cannot pop.\n");
return -1; // Indicating stack is empty
}
return stack[top--];
}
// Function to return the top element of the stack without removing it (peek operation)
int peek()
{
if (isEmpty())
{
printf("Stack is empty. Cannot peek.\n");
return -1; // Indicating stack is empty
}
return stack[top];
}
// Function to display all elements of the stack
void display()
{
if (isEmpty())
{
printf("Stack is empty.\n");
return;
}
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--)
{
printf("%d\n", stack[i]);
}
}
int main()
{
int choice, value;
while (1)
{
printf("\nStack Operations:\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1)
{
printf("Popped %d from stack.\n", value);
}
break;
case 3:
value = peek();
if (value != -1)
{
printf("Top element is %d.\n", value);
}
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Result:
Thus the C program for Array implementation of Stack ADT was successfully executed and
verified.