Skip to content

Commit 52902a7

Browse files
committed
18 queue and stacks
1 parent 901340d commit 52902a7

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Diff for: 18QueuesAndStacks.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <queue>
4+
#include <stack>
5+
6+
class Solution {
7+
//Write your code here
8+
std::stack<char> stack;
9+
std::queue<char> queue;
10+
public:
11+
void pushCharacter(char ch) { stack.push(ch); }
12+
void enqueueCharacter(char ch) { queue.push(ch); }
13+
char popCharacter() {
14+
char c {stack.top()};
15+
stack.pop();
16+
return c;
17+
}
18+
char dequeueCharacter() {
19+
char c {queue.front()};
20+
queue.pop();
21+
return c;
22+
}
23+
};
24+
25+
int main() {
26+
// read the string s.
27+
std::string s;
28+
std::getline(std::cin, s);
29+
30+
// create the Solution class object p.
31+
Solution obj;
32+
33+
// push/enqueue all the characters of string s to stack.
34+
for (size_t i = 0; i < s.length(); i++) {
35+
obj.pushCharacter(s[i]);
36+
obj.enqueueCharacter(s[i]);
37+
}
38+
39+
bool isPalindrome {true};
40+
41+
// pop the top character from stack.
42+
// dequeue the first character from queue.
43+
// compare both the characters.
44+
for (size_t i = 0; i < s.length() / 2; i++) {
45+
if (obj.popCharacter() != obj.dequeueCharacter()) {
46+
isPalindrome = false;
47+
break;
48+
}
49+
}
50+
51+
// finally print whether string s is palindrome or not.
52+
if (isPalindrome)
53+
std::cout << "The word, " << s << ", is a palindrome.\n";
54+
else
55+
std::cout << "The word, " << s << ", is not a palindrome.\n";
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)