Tuesday, 3 October 2017

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.


Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.

Tip:- Copy the colored code or full code(According to Requirement) and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or Email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

Submit Your Solution Here:- Click Here

Solution:-

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

using namespace std;

class Solution 
{
    //Write your code here
    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() 
{
    // 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++) 
{
        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:-


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


Ghanendra Yadav
Ghanendra Yadav

I Am Ghanendra Yadav Owner Of This Website. I Love Programming, Web Developing, Logo Making, Banner And Website designing. Currently I Am Studying In NIT Warangal And Also Working With WSDC( As Web Application Developer ) In NIT Warangal For Any Query Mail Me I Will Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment