-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack implementation.cpp
More file actions
89 lines (89 loc) · 1.11 KB
/
stack implementation.cpp
File metadata and controls
89 lines (89 loc) · 1.11 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
int stack[6],rev[6];
int top=-1,k=0;
int size;
void push();
void pop();
void display();
int pali();
int main()
{
int choice,f;
cout<<"Enter the size for stack\n";
cin>>size;
cout<<"1.Push\n2.Pop\n3.Display\n4.Check for Palindrome\n5.Exit\n";
while(1)
{
cout<<"Enter the choice\n";
cin>>choice;
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:f=pali();
if(f==1)
cout<<"It's Palindrome\n";
else
cout<<"It's not a Palindrome\n";
break;
case 5:return 0;
default:cout<<"Wrong choice...\n";
}}}
void push()
{
int num;
if(top==(size-1))
{
printf("Stack Overflow\n");
}
else
{
cout<<"Enter the number to be pushed\n";
cin>>num;
top++;
stack[top]=num;
}}
void pop()
{
int num;
if(top==-1)
{
cout<<"Stack Underflow\n";
}
else
{
num=stack[top];
cout<<"Popped element is \n"<<num<<endl;
top--;
}}
void display()
{
int i;
if(top==-1)
{
cout<<"Stack Underflow\n";
}
else
{
cout<<"Stack Contents....\n";
for(i=top;i>=0;i--)
{
cout<<stack[i]<<endl;
rev[k++]=stack[i];
}}}
int pali()
{
int i,flag=1;
for(i=top;i>=0;i--)
{
if(stack[i]!=rev[--k])
{
flag=0;
}}
return flag;
}