We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It is wise to use the stack and queue library.but we can achieve it on our own way.
This is the code below.
classSolution{//Write your code hereprivate:vector<char>stack={};vector<char>queue={};intstackCursor=0;intqueueCursor=-1;public:voidpushCharacter(charc){stack.push_back(c);stackCursor++;}voidenqueueCharacter(charc){queue.push_back(c);}charpopCharacter(){stackCursor--;returnstack[stackCursor];}chardequeueCharacter(){queueCursor++;returnqueue[queueCursor];}};
your error is to write sentences after return .Upon performing return,the program go to the end and not perform the subsequent sentence.
I also did it using vector and there is no cursor.
classSolution{//Write your code herepublic:voidpushCharacter(chars){stack.push_back(s);}voidenqueueCharacter(chars){queue.insert(queue.begin(),s);}charpopCharacter(){charch=stack.back();stack.pop_back();returnch;}chardequeueCharacter(){charch=queue.back();queue.pop_back();returnch;}private:vector<char>stack;vector<char>queue;};
This will give the wrong output. Stack uses LIFO and queue uses FIFO concept. But you are removing elements in both the cases from the end while in the case of a queue you should remove from the front. The purpose was to compare from both the directions for the palindrome check.
Honestly speaking, it's not about what the intention of the exercise was. I feel you gain a lot more from implenting the solution without using any libraries.
They are still using the <vector>
library, in my opinion it would be better to use a data structure that is a better basis for a queue and stack, such as <list> or <deque>
Implementing a stack and a queue with an array is probably one of the essential container lessons you will find in any half-way decent programming book. You have to understand the principles of FIFO and FILO first, though.
Trying to bend a <vector> to serve as a queue just seems wrong. People should read up on the STL containers they are using and choose the correct one for the job.
you are right... But what about using deque to make a queue. we can use pop_front to remove the first/ front item in the container. It also correlates with the idea of FIFO(queue)
the container.
classSolution{//Write your code herevector<char>v{};size_ts{0};size_tq{0};public:voidpushCharacter(charc){v.emplace_back(c);}voidenqueueCharacter(charc){}charpopCharacter(){if(s==0){s=v.size();}--s;returnv[s];}chardequeueCharacter(){size_tindex=q;++q;returnv[index];}};
C++I MADE MY OWN QUEUE AND STACK!With custom dynamic memory allocation!Now.
YES! I know that I could have used C++ build in queue and stack templates, BUT! I wanted to understand this data structure 100%, so the best way to do this is to make it myself!
YES! I could have used vector, or list or any other build in dynamic array C++ system. BUT! For long time I wanted to understand how vector, lists and other similar things work! So I tried to guess and make my own! And seems like its working!
classSolution{Stackbooks;//stack of books!Queueppl;//queue of people!public:voidpushCharacter(charchr){books.Push(chr);}voidenqueueCharacter(charchr){ppl.Enqueue(chr);}charpopCharacter(){returnbooks.Pop();}chardequeueCharacter(){returnppl.Dequeue();}};
I was looking at these other solutions and was wondering if I did something wrong, not being able to return s.pop or q.pop, so ended up doing exactly what you did
Day 18: Queues and Stacks
You are viewing a single comment's thread. Return to all comments →
My solution in c++, hope to help you
Great! I submitted this one without stack and queue library:
Should we swap the function names?
The code works, but I think your function names are backwards; your stack appears to function like a queue and vice versa.
That's right, my bad :c
your code is wrong ,you can test abcdbba,your answer is yes,but it is not a palindrome.you can test it !
That's why it was wise to use the stack and queue library.
It is wise to use the stack and queue library.but we can achieve it on our own way. This is the code below.
your error is to write sentences after return .Upon performing return,the program go to the end and not perform the subsequent sentence.
I also did it using vector and there is no cursor.
This will give the wrong output. Stack uses LIFO and queue uses FIFO concept. But you are removing elements in both the cases from the end while in the case of a queue you should remove from the front. The purpose was to compare from both the directions for the palindrome check.
The code passed all the tests.
The same is possible using std::string:
The intention of this exercise was to use the stack and queue library.
Honestly speaking, it's not about what the intention of the exercise was. I feel you gain a lot more from implenting the solution without using any libraries.
They are still using the
<vector>
library, in my opinion it would be better to use a data structure that is a better basis for a queue and stack, such as<list>
or<deque>
Implementing a stack and a queue with an array is probably one of the essential container lessons you will find in any half-way decent programming book. You have to understand the principles of FIFO and FILO first, though.
Trying to bend a
<vector>
to serve as a queue just seems wrong. People should read up on the STL containers they are using and choose the correct one for the job.you are right... But what about using
deque
to make a queue. we can usepop_front
to remove the first/ front item in the container. It also correlates with the idea of FIFO(queue) the container.No point in reinventing the wheel, its about knowing STL. I think everyone could come up with what you did anyway :p
char popCharacter() { return stack[stackCursor]; stackCursor++; } char dequeueCharacter() { return queue[queueCursor - 1]; queueCursor - 1; }
stackCursor++;and queueCursor - 1;this statement can excute?
I also think the intention of exersize to use (or develop) stack and queue.
But if you goal just to create solution to pass all test case you can reduce your code to
and it also pass the test cases for sure. :)
One vector:
C++ I MADE MY OWN QUEUE AND STACK! With custom dynamic memory allocation! Now.
Full code of my custom Stack and Queue classes on GitHub.
Solution class:
I feel you!! Here, check my code too (just it isn't as nicely organized as yours)
https://www.hackerrank.com/challenges/30-queues-stacks/forum/comments/858731
I was looking at these other solutions and was wondering if I did something wrong, not being able to return s.pop or q.pop, so ended up doing exactly what you did
Thankyou so much!!!
solid ans.eassy to understand ,thanks.
I did this with vectors just to find out there's a queue and stack library wow lol