-
Notifications
You must be signed in to change notification settings - Fork 448
/
Stack problem in c++
61 lines (55 loc) · 1.54 KB
/
Stack problem in 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
ARRAY AND LINKED IMPLEMENTATION OF STACK
A Program that exercise the operations on Stack Implementing Array
// i.e. (Push, Pop, Traverse)
#include <conio.h>
#include <iostream.h>
#include <process.h>
#define STACKSIZE 10 // int const STACKSIZE = 10; // global variable and array declaration
int Top=-1;
int Stack[STACKSIZE];
void Push(int); // functions prototyping int Pop(void);
bool IsEmpty(void);
bool IsFull(void);
void Traverse(void);
int main( )
{ int item, choice;
while( 1 )
{
cout<< "\n\n\n\n\n";
cout<< " ******* STACK OPERATIONS ********* \n\n"; cout<< " 1- Push item \n 2- Pop Item \n";
cout<< " 3- Traverse / Display Stack Items \n 4- Exit."; cout<< " \n\n\t Your choice ---> ";
cin>> choice;
switch(choice)
{ case 1: if(IsFull())cout<< "\n Stack Full/Overflow\n";
else
{ cout<< "\n Enter a number: "; cin>>item; Push(item); }
break;
case 2: if(IsEmpty())cout<< "\n Stack is empty) \n";
else
{item=Pop();
cout<< "\n deleted from Stack = "<<item<<endl;}
break;
case 3: if(IsEmpty())cout<< "\n Stack is empty) \n";
else
{ cout<< "\n List of Item pushed on Stack:\n";
Traverse();
}
break;
case 4: exit(0);
default:
cout<< "\n\n\t Invalid Choice: \n"; } // end of switch block
} // end of while loop
} // end of of main() function void Push(int item)
{
Stack[++Top] = item;
}
int Pop( )
{
return Stack[Top--];
}
bool IsEmpty( )
{ if(Top == -1 ) return true else return false; } bool IsFull( )
{ if(Top == STACKSIZE-1 ) return true else return false; } void Traverse( )
{ int TopTemp = Top;
do{ cout<< Stack[TopTemp--]<<endl;} while(TopTemp>= 0);
}