Wednesday, January 20, 2010

Implementing a Queue: Data Structures in Programming

Implementing a Stack is a simpler task compared to a Queue. In this post, i will explain how to implement a Queue. One can imagine a queue as a Water Pipe where water (elements, integers/objects in terms of computer programmer like us) enters from one end and leaves the other end. It follows the concept of "First In First Out"........

Implementing a queue through a program in python is quite easy, and is quite similar (only in python) and is described as follows:



Now, we see that it is so simple to implement in python. Now we will do the same in JAVA. There is a way through which we can implement the same as above in JAVA too (by importing java.util.Queue)!!

But what fun is it, when we let JAVA take care of everything (like overflow/empty.... kind of situations).

I will implement a small Queue of Integers and insert integers.

The size of the queue is read dynamically at the console (command prompt or the Bash shell or whatever).




The above code can be implemented in a more precise, professional manner using the Queue class.

Why re-invent the wheel when the stuff is pre-coded in the API?

Its because, we need to know what is going on behind the scenes, what the logic is all about. Thinking from scratch helps us understand and see stuff. This helps us professionally.


Cheers!!!
Rahul Kavi.

No comments:

Post a Comment