Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views4 pages

Array Impl of Stack ADT

The document outlines a C program for implementing a Stack Abstract Data Type (ADT) using an array. It details the algorithm for stack operations such as push, pop, peek, and display, along with the main execution loop for user interaction. The program successfully executes and verifies the stack operations as intended.

Uploaded by

rajuucet123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Array Impl of Stack ADT

The document outlines a C program for implementing a Stack Abstract Data Type (ADT) using an array. It details the algorithm for stack operations such as push, pop, peek, and display, along with the main execution loop for user interaction. The program successfully executes and verifies the stack operations as intended.

Uploaded by

rajuucet123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

You might also like