-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpalindromeUsingStackQueue.cpp
More file actions
110 lines (91 loc) · 2.01 KB
/
palindromeUsingStackQueue.cpp
File metadata and controls
110 lines (91 loc) · 2.01 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <stack>
#include <queue>
#include <string.h>
#include <conio.h>
using namespace std;
bool isPalindrome (string givenString) {
/*
Summary
-------
To check if a givenString is Palindrome or not.
Extended Summary
----------------
Type - bool
This function will tells that if a given string is palindrome or not. Two data
structures namely STL Stack and STL Queue are used for this purpose.
Parameters
----------
givenString : string - the given user input string.
Returns
-------
true : bool - givenString is Palindrome.
false : bool - givenString is not Palindrome.
Description
-----------
isPalindrome function definition.
Approach
--------
The givenString is being pushed char by char into stack and queue. Then,
a char is popped out from stack and dequeued from queue. If they're equal,
we do the same until stack is empty, otherwise returns false.
*/
int length = givenString.length ();
stack <char> s;
queue <char> q;
char stackElement;
char queueElement;
for (int i = 0; i < length; i++) {
if (givenString[i] == '\t' || givenString[i] == ' ') {
continue;
}
else {
s.push (givenString[i]);
q.push (givenString[i]);
}
}
while (!s.empty()) {
stackElement = s.top ();
queueElement = q.front ();
if (stackElement == queueElement) {
s.pop ();
q.pop ();
continue;
}
else {
return false;
}
}
return true;
}
int main () {
/*
Summary
-------
Create main() function.
Extended Summary
----------------
main() function will invoke the isPalindrome function.
Parameters
----------
None
Returns
-------
0 : int - succesful termination of program.
Description
-----------
main() function definition.
Approach
--------
Invoke isPalindrome with argument `givenString`.
*/
string givenString;
bool answer;
cout << "Enter a string: ";
getline (cin, givenString);
answer = isPalindrome (givenString);
answer ? cout << givenString << " is Palindrome." : \
cout << givenString << " is NOT Palindrome.";
getch ();
return 0;
}