- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to push and pop elements in a queue in Ruby?
In this article, we will learn how to push (insert) and pop (remove) elements in a queue in Ruby.
In Ruby, we can insert elements into a queue with the help of the push function. The syntax of the push function is shown below.
Notice that the above syntax accepts one argument, which is the element that you want to insert into the queue. The result of the above command is that a new element will be added into the front of the queue.
To remove an element from the queue, we can make use of the pop function. The syntax of the pop function is shown below.
Notice that the above syntax doesn't accept any argument and the result of the above command is that one element will be removed from the front of the queue.
Now let's take a couple of examples to demonstrate how to utilize these functions.
# push() and pop() function in Queue # Create a new QUEUE queueOne queueOne = Queue.new # push 11 queueOne.push(11) # push 22 queueOne.push(22) # pop top element puts queueOne.pop puts queueOne.pop
# pop() and push() function in Queue # Create a new QUEUE queueOne queueOne = Queue.new # push 11 queueOne.push(11) # push 15 queueOne.push(15) # Print element puts queueOne.pop # Again pushes 17 queueOne.push(17) # Print element puts queueOne.pop # Print element puts queueOne.pop
11 15 17
- queue::push() and queue::pop() in C++ STL
- Array push(), pop() and clear() functions in Ruby
- stack push() and pop() in C++ STL
- Program to implement a queue that can push or pop from the front, middle, and back in Python
- How to use Push-Location and Pop-Location command in PowerShell?
- C# Program to Implement Stack with Push and Pop operations
- Push vs pop in stack class in C#
- Program to check given push pop sequences are proper or not in python
- How to Read and Writes Files in Ruby
- How do I push elements to an existing array in MongoDB?
- How to pop a single value in MongoDB?