-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackAsArrayUsingGlobalVariable.c
More file actions
75 lines (74 loc) · 1.74 KB
/
StackAsArrayUsingGlobalVariable.c
File metadata and controls
75 lines (74 loc) · 1.74 KB
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
//Program to insert ,delete and display using stack as arrays ( global variables )
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Stack_size 5
int top; //global variable
int item,s[5];
// c function to an push integer element onto the stack using global variables
void PUSH()
{
if(top==Stack_size-1)
{
printf(" Stack is full \n");
return;
}
top=top+1;
s[top]=item;
// instead of line 16,17 we can use s[++top]=item;
}
// c function for pop :delete an item from the top of the stack using global variable
int POP()
{
int item_deleted;
if(top==-1)
return 0;
item_deleted=s[top--];
return item_deleted;
}
// c function to display the elements of the stack using global variable
void display()
{
if(top==-1)
{
printf(" Stack is empty\n");
return;
}
printf(" Elements of Stack are : ");
for(int j=0;j<=top;j++)
{
printf("%d\n",s[j]);
}
}
void main()
{
int choice;
top=-1;
int item_deleted;
for( ; ;)
{
printf(" 1: Push 2: Pop \n");
printf(" 3: Display 4: Exit \n");
printf(" Enter choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf(" Enter the item to be inserted \n");
scanf("%d",&item);
PUSH();
break;
case 2: item_deleted=POP();
if(item_deleted==0)
{
printf(" Stack is empty\n"); //underflow
}
else
printf(" Item deleted is %d\n",item_deleted);
break;
case 3:display();
break;
case 4: exit(0);
}// end of switch
}//end of for loop
}// end of main