# Hacker Rank Solution For Day 18: Queues and Stacks

Problem:- Write a Hackerrank Solution For Day 18: Queues and Stacks or Hacker Rank Solution Program In C++ For " Day 18: Queues and Stacks " or Hackerrank 30 days of code Java Solution:Day 18: Queues and Stacks solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 18: Queues and Stacks solution, or C/C++ Logic & Problem Solving: Day 18: Queues and Stacks or Hacker Rank Solution For Day 18: Queues and Stacks.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Explanation:- Before going to solve first we have to know how Stack and Queue works and what is the difference between Stack and Queue so let's take an into of both Stack and Queue. here we have to check string is Palindrome or Not. First, see this C++ Program To Check String Is Palindrome Or Not and also See this C++ Program To Find Whether A Number Is Palindrome Or Not.

Stack:- Stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element of the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out).

source:- Wikipedia

Queue:- A Queue  is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure

source:- Wikipedia

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

We can create a whole Stack and Queue program by using an array but here with the help of #include <stack> and #include <queue> header file or we can use one Master header file #include<bits/stdc++.h> instead of using multiple header files.

Solution:-

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

class Solution
{
stack<char> stk;
queue<char> que;
public:

char c;

void pushCharacter(char ch)
{
stk.push(ch);
}
char popCharacter()
{
c = stk.top();
stk.pop();
return c;
}
void enqueueCharacter(char ch)
{
que.push(ch);
}
char dequeueCharacter()
{
c = que.front();
que.pop();
return c;
}
};

int main()
{
string s;
getline(cin, s);

// create the Solution class object p.
Solution obj;

// push/enqueue all the characters of string s to stack.
for (int i = 0; i < s.length(); i++)
{
obj.pushCharacter(s[i]);
obj.enqueueCharacter(s[i]);
}

bool isPalindrome = true;

// pop the top character from stack.
// dequeue the first character from queue.
// compare both the characters.
for (int i = 0; i < s.length() / 2; i++)
{
if (obj.popCharacter() != obj.dequeueCharacter())
{
isPalindrome = false;

break;
}
}

// finally print whether string s is palindrome or not.
if (isPalindrome)
{
cout << "The word, " << s << ", is a palindrome.";
} else
{
cout << "The word, " << s << ", is not a palindrome.";
}

return 0;
}

Output:-

You May Also Like:-

1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 19: Interfaces

6. Hackerrank Solution For Day 20: Sorting

7. Hackerrank Solution For Day 21: Generics

8. Hackerrank Solution For Day 22: Binary Search Trees

9. Hackerrank Solution For Day 23: BST Level-Order Traversal

10. Hackerrank Solution For Day 24: More Linked Lists