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:

Advertisements

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s