Stack and Queue in Lists

Ruby lists:
li = [1,2,3,4]

use li as stack,
li.push(5)
[1,2,3,4,5]

li.pop
5
li
[1,2,3,4]

use li as queue,
li.shift
1
li
[2,3,4]

li.unshift(0)
[0,2,3,4]

Blogged with Flock

Tags:

2 thoughts on “Stack and Queue in Lists

  1. I notice one thing. In the queue example you have given, both insertions and deletions seem to be happening in the same end ? Both the examples are stacks and not a queue.

    PS: i know this is a very old post. chumma commenting 🙂

Leave a reply to Vignesh Cancel reply