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

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

Implementation of Stack Adt

The document outlines a C program for implementing a Stack Abstract Data Type (ADT) using arrays. It includes an algorithm detailing the basic operations of the stack: PUSH, POP, and DISPLAY, along with a sample program code. The program successfully executes stack operations and provides a sample output demonstrating its functionality.

Uploaded by

RASHAD H
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)
7 views4 pages

Implementation of Stack Adt

The document outlines a C program for implementing a Stack Abstract Data Type (ADT) using arrays. It includes an algorithm detailing the basic operations of the stack: PUSH, POP, and DISPLAY, along with a sample program code. The program successfully executes stack operations and provides a sample output demonstrating its functionality.

Uploaded by

RASHAD H
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

Ex. No: 1a Date:……………..

IMPLEMENTATION OF STACK ADT

Aim:

To write a C program to implement Stack ADT by using arrays

Algorithm:

1. Create a Stack[ ] with MAX size as your wish.


2. Write function for all the basic operations of stack - PUSH(), POP() and DISPLAY().
3. By using Switch case, select push() operation to insert element in the stack.
● Step 1: Check whether stack is FULL. (top == SIZE-1)
● Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not
possible!!!" and terminate the function.
● Step 3: If it is NOT FULL, then increment top value by one (top++) and set
stack[top] to value (stack[top] = value).
4. Similarly, By using Switch case, select pop() operation to delete element from the
stack.
● Step 1: Check whether stack is EMPTY. (top == -1)
● Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
● Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top value
by one (top--).
5. Similarly, By using Switch case, select display() operation to display all element
from the stack.
● Step 1: Check whether stack is EMPTY. (top == -1)
● Step 2: If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the
function.
● Step 3: If it is NOT EMPTY, then define a variable 'i' and initialize with top.
Display stack[i] value and decrement i value by one (i--).
● Step 3: Repeat above step until i value becomes '0'.
6. Close the program

PROGRAM:

/* Stack Operation using Arrays */


#include <stdio.h>
#include <conio.h> #define
max 5
static int stack[max]; int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return (stack[top--]);
}

void view()
{
int i;
if (top < 0)
printf("\n Stack Empty \n"); else
{
printf("\n Top-->");
for(i=top; i>=0; i--)
{
printf("%4d", stack[i]);
}
printf("\n");
}
}

main()
{
int ch=0, val; clrscr();

while(ch != 4)
{
printf("\n STACK OPERATION \n");
printf("1.PUSH ");
printf("2.POP ");
printf("3.VIEW ");
printf("4.QUIT \n"); printf("Enter Choice
: ");

scanf("%d", &ch);

switch(ch)
{
case 1:
if(top < max-1)
{
printf("\nEnter Stack element : "); scanf("%d", &val);
push(val);
}
else
printf("\n Stack Overflow \n"); break;
case 2:
if(top < 0)
printf("\n Stack Underflow \n"); else
{
val = pop();
printf("\n Popped element is %d\n", val);
}
break; case 3:
view(); break;
case 4:
exit(0); default:
printf("\n Invalid Choice \n");
}
}
}

Sample output :
Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY


----------------------------1.
PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12

Enter the Choice:1


Enter a value to be pushed:24
Enter the Choice:1
Enter a value to be pushed:98
Enter the Choice:3
The elements in STACK

98
24
12
Press Next Choice
Enter the Choice:2

The popped elements is 98


Enter the Choice:3
The elements in
STACK 24
12
Press Next Choice
Enter the Choice:4

EXIT POINT

Result:

Thus the C program to implement Stack ADT by using array is executed successfully and the
output is verified

You might also like