-
Notifications
You must be signed in to change notification settings - Fork 27
/
lifo.c
75 lines (75 loc) · 1.67 KB
/
lifo.c
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
#include<stdio.h>
int stack [ 100 ] , x , top = -1 , n , i ;
void push()
{
if ( top >= n - 1 ) // Checking the overflow condition of the stack //
{
printf ( "Stack overflow! \n " ) ;
}
else
{
printf ( "Enter value to be pushed : " ) ;
scanf ( "%d" , &x ) ;
top = top + 1 ;
stack [ top ] = x ;
}
}
void pop()
{
if ( top == -1 )
{
printf ( "Stack underflow! \n " ) ;
}
else
{
printf ( "The popped element is %d \n " , stack [ top ] ) ;
top = top - 1 ;
}
}
void traverse()
{
if ( top == -1 )
{
printf ( "Stack empty!\n " ) ;
}
else
{
for ( i = top ; i >= 0 ; i-- )
{
printf ( "%d \n " , stack [ i ] ) ;
}
}
}
int menu()
{
int ch ;
printf ( " \t \t \t 1. PUSH OPERATION \n " ) ;
printf ( " \t \t \t 2. POP OPERATION \n " ) ;
printf ( " \t \t \t 3. TRAVERSE OPERATION \n " ) ;
printf ( " \t \t \t 4. EXIT \n " ) ;
printf ( "Enter your choice: " ) ;
scanf ( "%d" , &ch ) ;
return ch ;
}
void main()
{
printf ( "Enter size of stack : " ) ;
scanf ( "%d" , &n ) ;
while ( 1 )
{
switch ( menu() )
{
case 1 :
push ( ) ;
break ;
case 2 :
pop ( ) ;
break ;
case 3 :
traverse ( ) ;
break ;
case 4 :
exit( 0 ) ;
break ;
} }
}