20/03/2023

Day 18 Queues And Stacks Hackerrank Solution in C++ | 30 Days

Day 18 Queues And Stacks Hackerrank Solution in C++. Welcome to Day 18! Today we're learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video! A palindrome is a word, phrase, number, or another sequence of characters which reads the same backwards and forwards. Can you determine if a given string, s, is a palindrome?

Day 18 Queues And Stacks Hackerrank Solution in C++

To solve this challenge, we must first take each character in s, enqueue it in a queue, and also push that same character onto a stack. Once that's done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeuing, popping, and comparing each character until our containers are empty (a non-match means s isn't a palindrome).

Write the following declarations and implementations:

  • Two instance variables: one for your stack, and one for your queue.
  • A void pushCharacter(char ch) method that pushes a character onto a stack.
  • A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
  • A char popCharacter() method that pops and returns the character at the top of the stack instance variable.
  • A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.

Input Format

You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string s. It then calls the methods specified above to pass each character to your instance variables.

Constraints

s is composed of lowercase English letters.

Output Format

You are not responsible for printing any output to stdout.

If your code is correctly written and s is a palindrome, the locked stub code will print The word, s, is a palindrome.; otherwise, it will print The word, s, is a palindrome.

Sample Input

racecar

Sample Output

The word, racecar, is a palindrome.

Submit Your Solution Here: Click Here

Day 18 Queues And Stacks Hackerrank Solution in C++


#include <iostream>
using namespace std;

class Solution
{
	string queue = "", stack = "";
	public:
		void pushCharacter(char c)
		{
			this->stack = c + this->stack;
		};

	void enqueueCharacter(char c)
	{
		this->queue += c;
	};

	char popCharacter()
	{
		char c = this->stack[0];
		this->stack = this->stack.substr(1);
		return c;
	};

	char dequeueCharacter()
	{
		char c = this->queue[0];
		this->queue = this->queue.substr(1);
		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;
}

Queues And Stacks Explanation 


Before going to solve first we have to know how Stack and Queue work 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).

Stack

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

Queue

Queues And Stacks Hackerrank Solution Output


Queues And Stacks Hackerrank Solution Output

Similar to Queues And Stacks


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

0 Comments: