C program to implement stack operations using array

Below is the C program to implement stack operations using array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define size 100
int top=-1;
int flag=0;
int stack[size];
void push(int *,int);
int pop(int *);
void display(int *);
void push(int s[],int d)
{
if(top==(size-1))
flag=0;
else
{
flag=1;
++top;
s[top]=d;
}
}
int pop(int s[])
{
int popped_element;
if(top==-1)
{
popped_element=0;
flag=0;
}
else
{
flag=1;
popped_element=s[top];
--top;
}
return(popped_element);
}
void display(int s[])
{
int i;
if(top==-1)
{
printf("\n stack is empty");
}
else
{
for(i=top;i>=0;--i)
printf("\n %d",s[i]);
}
}
/* this is the main function */
void main()
{
int data;
char choice;
int q=0;
int top=-1;
clrscr();
do
{
printf("\n push->i pop->p quit->q:");
printf("enter your choice");
do
{
choice=getchar();
choice=tolower(choice);
}
while(strchr("ipq",choice)==NULL);
printf("your choice is %c",choice);
switch(choice)
{
case'i':printf("\n input element to push");
scanf("%d",&data);
push(stack,data);
if(flag)
{
printf("\n after inserting ");
display(stack);
if(top==(size-1))
printf("\n stack is full");
}
else
printf("\n stack is overflown after pushing");
break;
case 'p':data=pop(stack);
if(flag)
{
printf("\n data is popped:%d",data);
printf("\n now the stack is as follows :\n");
display(stack);
}
else
printf("\n stack is underflown");
break;
case'q':q=1;
}
}
while(!q);
}
Drithi

3 thoughts on “C program to implement stack operations using array

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in