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++.

Hacker Rank Solution For Day 18: Queues and Stacks

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).

Hacker Rank Solution For Day 18: Queues and Stacks

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

Hacker Rank Solution For Day 18: Queues and Stacks

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.

Submit Your Solution Here:- Click Here


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

using namespace std;

class Solution 
    //Write your code here
    stack<char> stk;
    queue<char> que;
char c;
    void pushCharacter(char ch) 
        char popCharacter() 
        c = stk.top();
        return c;
    void enqueueCharacter(char ch) 
    char dequeueCharacter() 
        c = que.front();
        return c;

int main() 
    // read the string s.
    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++) 
    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;
    // 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;


Hacker Rank Solution For Day 18: Queues and Stacks

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

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me