IMPLEMENTATION OF STACK AND ITS OPERATION
Exercise.No:3
TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube
AIM:
To implement the program for Push, Pop and Display operations in Stack.
ALGORITHM:
PUSH OPERATION:
Step 1: If Top=Max-1
Print “Overflow : Stack is full” and Exit
End If
Step 2: Top=Top+1
Step 3: Stack[TOP]=Element
Step 4: End
POP OPEARTION:
Step 1: If TOP=-1
Print “Underflow: Stack is empty” and Exit
End if
Step 2: Set Del element=Stack[Top]
Step 3: Top=Top-1
Step 4: Del Element
Step 5: End
NOTE :
In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken
1.)In "in it" remove the space between "in" and "it".
2.)In "print f" remove the space between "print" and "f".
3.)In "scan f" remove the space between "scan" and "f".
4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".
5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".
PROGRAM :
#include<std io. h>
#include<con io. h>
#define MAX 15
typedef struct stack
{
int data[MAX];
int top;
}stack;
stack s;
void in it(stack*s);
void push(stack*s, int x);
void pop(stack*s);
int full(stack*s);
int empty(stack*s);
void print(stack*s);
int main()
{
int op,op1;
do
{
print f("\n\n1)Initialize\n2)Push\n3)Pop\n4)Full");
print f("\n5)Empty\n6)Print\n7)Quit");
print f("\n Enter your Choice:");
scan f("%d", &op);
switch(op)
{
case 1:init(&s);
break;
case 2:printf("\n Enter your element to push : ");
scan f("%d",&op1);
push(&s,op1);
break;
case 3:pop(&s);
break;
case 4:full(&s);
break;
case 5:empty(&s);
break;
case 6:print(&s);
break;
}
}while(op<7);
}
void in it(stack*s)
{
s->top=-1;
}
void push(stack*s, int x)
{
if(s->top==MAX-1)
{
print f("\n Stack is full");
}
else
{
s->top=s->top+1;
s->data[s->top]=x;
}
}
void pop(stack*s)
{
if(s->top==-1)
{
print f("\n Stack is empty");
}
else
{
if(s->top==0)
{
s->top=-1;
}
else
{
s->top=s->top-1;
}
}
}
int empty(stack*s)
{
if(s->top==-1)
{
print f("\n Stack is empty");
}
else
{
print f("\n Stack is not empty");
}
return(0);
}
int full(stack*s)
{
if(s->top==MAX-1)
{
print f("\n Stack is full");
}
else
{
print f("\n Stack is not full");
}
return(0);
}
void print(stack*s)
{
int a;
if(s->top==-1)
{
print f("Stack is empty");
}
else
{
for(a=s->top; a>=0;a--)
{
print f("\n %d", s->data[a]);
}
}
}
OUTPUT :
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:1
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:2
Enter your element to push : 12
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:2
Enter your element to push : 13
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:2
Enter your element to push : 14
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:6
14
13
12
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:3
1)Initialize
2)Push
3)Pop
4)Full
5)Empty
6)Print
7)Quit
Enter your Choice:6
13
12
RESULT:
Thus the program for Push, Pop and Display operations in Stack was implemented and
executed successfully.