• + 6 comments

    My solution in c++, hope to help you

    #include <iostream>
    #include <stack>
    #include <queue>
    
    using namespace std;
    
    class Solution {
        //Write your code here
        stack<char> s;
        queue<char> q;
        public:
        void pushCharacter(char i) {
            s.push(i);
        }
        void enqueueCharacter(char i) {
            q.push(i);
        }
        char popCharacter() {
            char temp = s.top();
            s.pop();
            return temp;
        }
        char dequeueCharacter() {
            char temp = q.front();
            q.pop();
            return temp;
        }
    };
    
    • + 7 comments

      Great! I submitted this one without stack and queue library:

      #include <iostream>
      #include <string>
      #include <vector>
      using namespace std;
      
      
      class Solution {
      
      	private:
      		vector<char> stack  = {};
      		vector<char> queue = {};
      		int stackCursor = 0;
      		int queueCursor = 0;
      	public: 
      	void pushCharacter(char c) {
      		stack.push_back(c);
      	}
      
      	void enqueueCharacter(char c) {
      		queue.push_back(c);
      		queueCursor++;
      	}
      	
      	char popCharacter() {
      		return stack[stackCursor];
      		stackCursor++;
      	}
      	char dequeueCharacter() {
      		return queue[queueCursor - 1];
      		queueCursor - 1;
      	}
      };
      
      • + 0 comments

        Should we swap the function names?

      • [deleted]
        + 1 comment

        The code works, but I think your function names are backwards; your stack appears to function like a queue and vice versa.

        • + 0 comments

          That's right, my bad :c

      • + 1 comment

        your code is wrong ,you can test abcdbba,your answer is yes,but it is not a palindrome.you can test it !

        • + 1 comment

          That's why it was wise to use the stack and queue library.

          • + 2 comments

            It is wise to use the stack and queue library.but we can achieve it on our own way. This is the code below.

            class Solution {
                //Write your code here
            private:
            		vector<char> stack  = {};
            		vector<char> queue = {};
            		int stackCursor = 0;
            		int queueCursor = -1;
            	public: 
            	void pushCharacter(char c) {
            		stack.push_back(c);
                    stackCursor++;
            	}
            
            	void enqueueCharacter(char c) {
            		queue.push_back(c);
            		
            	}
            	
            	char popCharacter() {
                    stackCursor--;
            		return stack[stackCursor];
            		
            	}
            	char dequeueCharacter() {
                    queueCursor++;
            		return queue[queueCursor];
                    
            		
            	}
            };
            

            your error is to write sentences after return .Upon performing return,the program go to the end and not perform the subsequent sentence.

            • + 2 comments

              I also did it using vector and there is no cursor.

              class Solution {
                  //Write your code here
                  public:
                  void pushCharacter(char s){
                      stack.push_back(s);
                  }
                  void enqueueCharacter(char s){
                      queue.insert(queue.begin(),s);
                  }
                  char popCharacter(){
                      char ch = stack.back(); 
                      stack.pop_back(); 
                      return ch;
                  }
                  char dequeueCharacter(){
                      char ch = queue.back(); 
                      queue.pop_back(); 
                      return ch;
                  }
                  private:
                  vector<char> stack;
                  vector<char> queue;
              };
              
              • + 1 comment

                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.

                • + 0 comments

                  The code passed all the tests.

              • + 0 comments

                The same is possible using std::string:

                string stack;
                string queue;
                
      • + 1 comment

        The intention of this exercise was to use the stack and queue library.

        • + 3 comments

          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.

          • + 0 comments

            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>

          • + 1 comment

            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.

            • + 0 comments

              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.

          • + 0 comments

            No point in reinventing the wheel, its about knowing STL. I think everyone could come up with what you did anyway :p

      • + 0 comments

        char popCharacter() { return stack[stackCursor]; stackCursor++; } char dequeueCharacter() { return queue[queueCursor - 1]; queueCursor - 1; }

        stackCursor++;and queueCursor - 1;this statement can excute?

      • + 0 comments

        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

        class Solution 
        {
        
        private:
            vector<char> my_hack_container;
           
        public:
            void pushCharacter(char c) {
                my_hack_container.push_back(c);
            }
        
            void enqueueCharacter(char) {
            }
        
            char popCharacter() 
            {
                static size_t my_hack_container_pos = my_hack_container.size();
                return my_hack_container[--my_hack_container_pos];
            }
            char dequeueCharacter() 
            {
                static size_t queue_pos = 0;
                return my_hack_container[queue_pos++];
            }
        };
        

        and it also pass the test cases for sure. :)

      • + 0 comments

        One vector:

        class Solution {
            //Write your code here
            vector<char> v{};
            size_t s{0};
            size_t q{0};
            
            public:
            void pushCharacter(char c) {
                v.emplace_back(c);
            }
                
            void enqueueCharacter(char c) {
            }
                
            char popCharacter() {
                if (s == 0) {
                    s = v.size();
                }
                
                --s;
                return v[s];
            }
            
            char dequeueCharacter() {
                size_t index = q;
                ++q;
                return v[index];
            }
        };
        
    • + 1 comment

      C++ I MADE MY OWN QUEUE AND STACK! With custom dynamic memory allocation! Now.

      1. 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!
      2. 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!

      Full code of my custom Stack and Queue classes on GitHub.

      Solution class:

      class Solution
      {
          Stack books; //stack of books!
          Queue ppl; //queue of people!
          public:
              void pushCharacter(char chr)
              {
                  books.Push(chr);
              }
              void enqueueCharacter(char chr)
              {
                  ppl.Enqueue(chr);
              }
              char popCharacter()
              {
                  return books.Pop();
              }
              char dequeueCharacter()
              {
                  return ppl.Dequeue();
              }
      };
      
      • + 0 comments

        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

    • + 0 comments

      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

    • + 0 comments

      Thankyou so much!!!

    • + 0 comments

      solid ans.eassy to understand ,thanks.

    • [deleted]
      + 0 comments

      I did this with vectors just to find out there's a queue and stack library wow lol