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

C Program for Stack ADT Implementation

The document outlines a C program that implements a stack Abstract Data Type (ADT) using an array. It includes an algorithm for stack operations such as creation, insertion, deletion, and display, along with the complete program code. The program successfully demonstrates these operations through a menu-driven interface and provides sample output for various stack manipulations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

C Program for Stack ADT Implementation

The document outlines a C program that implements a stack Abstract Data Type (ADT) using an array. It includes an algorithm for stack operations such as creation, insertion, deletion, and display, along with the complete program code. The program successfully demonstrates these operations through a menu-driven interface and provides sample output for various stack manipulations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

AIM:

To write a program in C to implement the stack ADT using array concept


that performs all the operations of stack.

ALGORITHM:
STEP 1: Define an array to store the element.
STEP 2: Get the users’ choice.
STEP 3: If the option is 1 perform creation operation and goto step4.
If the option is 2 perform insertion operation and goto step5.
If the option is 3 perform deletion operation and goto step6.
If the option is 4 perform display operation and goto step7.
STEP 4: Create the stack. Initially get the limit of stack and the get the items.
If the limit of stack is exceeds print the message unable to create the stack.
STEP 5: Get the element to be pushed. If top pointer exceeds stack capacity. Print
Error message that the stack overflow. If not, increment the top pointer by one and
store the element in the position which is denoted by top pointer.
STEP 6: If the stack is empty, then print error message that stack is empty. If not
fetch the element from the position which is denoted by top pointer and decrement
the top pointer by one
STEP 7: If the top value is not less than the 0 the stack is display otherwise
print the message “stack is empty”.
STEP 8: Stop the execution.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 20
int opt, a[20],i,top=0,n;
void main()
{
void create(),push(),pop(),disp();
int choice;
do
{
//clrscr();
printf("\nMENU");
printf("\[Link]\[Link]\[Link]\[Link]\[Link]\n");
printf("\nEnter your option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();
break;
case 2:push();
break;
case 3:pop();
break;
case 4:disp();
break;
case 5:
exit (0);
}
printf("\nDo u want to continue(1/0):");
scanf("%d",&choice);
}
while(choice==1);
}
void create()
{
printf("\n Enter the limit of stack");
scanf("%d",&n);
if(n<max)
{
printf("\nEnter the items");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
top=n-1;
}
else
printf("\nUnable to create the stack");
}
void push()
{
int x;
if(top<max)
{
printf("\nEnter the element to be pushed:");
scanf("%d",&x);
top=top+1;
a[top]=x;
n=top;
}
else
printf("\n Stack is full");
}
void pop()
{
if(top<0)
printf("\n Stack is empty");
else
{
printf("\nThe element popped is %d",a[top]);
top=top-1;
n=top;
}
}
void disp()
{
if(top<0)
printf("\n Stack is empty");
else
{
printf("\n The elements in the stack are:");
for(i=top;i>=0;i--)
printf("\n%d",a[i]);
}
}

OUTPUT:

MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option1
Enter the limit of stack5
Enter the items
10
20
30
40
50
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option4
The elements in the stack are:
50
40
30
20
10
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option2
Enter the element to be pushed:15
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option4
The elements in the stack are:
15
50
40
30
20
10
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option3
The element popped is 15
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option4
The elements in the stack are:
50
40
30
20
10
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option3
The element popped is 50
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option4
The elements in the stack are:
40
30
20
10
Do u want to continue(1/0):1
MENU
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your option5

RESULT:
Thus a C program for Stack using ADT was implemented successfully

You might also like