-
Notifications
You must be signed in to change notification settings - Fork 0
/
templated-stack-palindrome
123 lines (112 loc) · 2.03 KB
/
templated-stack-palindrome
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<conio.h>
#define MAX_STACK 40
#define MIN_STACK 0
using namespace std;
template <class T>
class cStack
{
private:
T array[MAX_STACK];
int top;
int base;
public:
cStack();
~cStack();
void push(T var);
T pop();
void printStack();
T readStack(){return array[top];}
void checkPalindrome();
};
template <class T>
cStack<T>::cStack()
{
array[MAX_STACK]={0};
top=0;
base=0;
}
template <class T>
cStack<T>::~cStack()
{
}
template <class T>
void cStack<T>::push(T val)
{
if (top == MAX_STACK)
{
cout<<"Stack is full,cant push"<<endl;
return;
}
array[top]=val;
top++;
}
template <class T>
T cStack<T>:: pop()
{
if (top == MIN_STACK)
{
cout<<"Stack is empty, cant pop"<<endl;
return -1;
}
T retval=array[top-1];
top--;
return retval;
}
template <class T>
void cStack<T>::printStack()
{
for (int i=0;i<top;i++)
{
cout<<array[i];
}
cout<<endl;
}
template <class T>
void cStack<T>::checkPalindrome()
{
T *headptr=array;
T *tailptr=array+top-1;
cout<<endl<<"head is "<<*headptr<<endl<<"tail is "<<*tailptr<<endl;
bool checkPalindrome = true;
while (tailptr>headptr)
{
if(array[*headptr++]!=array[*tailptr--])
{
checkPalindrome = false;
break;
}
}
if (checkPalindrome == false)
cout<<"Not palindrome"<<endl;
else
cout<<"Palindrome"<<endl;
}
int main()
{
cStack<char>myStack;
myStack.push('n');
myStack.push('i');
myStack.push('t');
myStack.push('i');
myStack.push('n');
myStack.printStack();
cout<<endl;
cout<<"Popped value is "<<myStack.pop()<<endl;
myStack.printStack();
myStack.push('n');
myStack.printStack();
myStack.checkPalindrome();
cout<<endl<<"lets try one more"<<endl;
cStack<char>myStack2;
myStack2.push('d');
myStack2.push('i');
myStack2.push('w');
myStack2.push('a');
myStack2.push('k');
myStack2.push('a');
myStack2.push('r');
myStack2.printStack();
myStack2.checkPalindrome();
return 0;
}