Using the language RUBY Create a RUBY class QUEUE that inplements methods enqueu
ID: 3841965 • Letter: U
Question
Using the language RUBY Create a RUBY class QUEUE that inplements methods enqueue, dequeue, and show, Instance variables are size -
size of array that implements queue
q - array that represents queue (fixed size)
inp- index that specifies that an input value that is going in th queue should be stored in
q[inp] out- index that specifies that an output value that is leaving the queue should be taken from
q[out] empty- a logic flag which is true if the queue is empty (no elements) f
ull- logic flag which is true, if the que is full and has no free space for new elements.
The method q.enqueue(x) should store the value x in the queue q. The method x = q.dequeue should take the appropriate value from the queue q and store it in the vriable x. The method q.show should display the FIFO queue in the form <1|2|3|4|5 where 5 is the last element stored. Write a main program that creates a queue size of 5 and uses a loop to crrate the out put shown here.
<0|
<0|1|2|
<0|1|2|3|
<0|1|2|3|4|
Explanation / Answer
#!/user/bin/evn ruby
p = Array.new(5)
$inp = 0
$out
$empty = 1
$full = 0
def enque(p,element)
p[element] = element
end
def show(p,size)
print "<"
for i in 0..size
print p[i] /* here don't use puts. Because it automatically adds a new line at the end of the printing. Then every number will be added to new line which is not our required pattern*/
print "|"
end
print " "
end
for i in 0..4
enque(p,i)
show(p,i)
end
/*
If you just want output as you given, above program is enough to print it
Since we our array (queue) size is fixed, we can continuously call enqueue and show methods
In addition to that here, I am writing remaining methods to dequeue and to check queue empty and full condition
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.