Array Queues - Java Data Structures [Link]
Special : Web Developer's Collection CD-ROM
50,000+ Web Templates, Logos, Cliparts, Scripts. Order Now!
Add to Favorites Make Home Page 635 Online
Language Categories Advertisements
Source Codes Home Java Data Structures - Contents
Project Ideas New! ADVT
Interview Questions
FAQs Home
ASP Home
ASP Source Codes
Array Queues... Jobs & Career
ASP Script Freshers
Directory New! ADVERTISEM ENT
ASP .Net Script
Jobs
Directory New! Jobs
ASP Interview Search Projects & Source Codes: Google Search Newsletter
Questions
ASP FAQs Placement
ASP How Tos Papers
C Home A queue is a FIFO (First In, First Out) structure. Anything that's inserted Placement
C Source Codes
C Script Directory
first, will be the first to leave (kind of like the real world queues.) This is Papers
New! totally the opposite of what a stack is. Although that is true, the queue GATE Preparation
C Interview implementation is quite similar to the stack one. It also involves pointers to
Questions Analysis &
C FAQs specific places inside the array. Design Of
C How Tos
Algo.
C++ Home With a queue, we need to maintain two pointers, the start and the end.
C++ Source Operating
We'll determine when the queue is empty if start and end point to the same
Codes System
C++ Script element. To determine if the queue is full (since it's an array), we'll have a Lexical
Directory New!
C++ Interview
boolean variable named full. To insert, we'll add one to the start, and mod Analysis
Questions (the % operator) with the size of the array. To remove, we'll add one to the GRE Preparation
C++ FAQs end, and mod (the % operator) with the size of the array. Simple? Well, lets
C++ How Tos GRE Home
Java Home write it. 1208
Java Source Codes
Java Directory public class pArrayQueue{ Antonyms
New! protected Object[] array; Test
Java Interview 5000 Word's
Questions
protected int start,end;
Java FAQs protected boolean full; List
Java How Tos Top 100
JavaScript Home
JavaScript
public pArrayQueue(int maxsize){ Words' List
array = new Object[maxsize]; Scholarships
Directory New!
start = end = 0;
JavaScript Source Top 100 CS
Codes full = false;
JavaScript FAQs } Univ.
JavaScript How Top 126 EE
Tos
COBOL Home
public boolean isEmpty(){ Univ.
COBOL Source return ((start == end) && !full); Tutorials
Codes } Hardware
COBOL FAQs
COBOL How Tos Tutorial
public void insert(Object o){
Pascal Home 1500 Free
Pascal Source if(!full)
array[start = (++start % [Link])] = o; eBooks
Codes
Pascal FAQs if(start == end) XML Tutorial
Pascal How Tos full = true; Webmaster
PHP Script Directory New!
Python Script Directory
} Resources
New! EzTraffic
Perl & CGI Script Directory public Object remove(){ Articles
New! if(full)
Flash Script Directory New! Fun
full = false;
CFML Script Directory
else if(isEmpty()) Send FREE
New!
Remotely Hosted Scripts return null; SMS!
New! return array[end = (++end % [Link])]; SMS Jokes
Tools & Utilities Directory } Love SMS
New!
XML Script Directory New!
} Funny Jokes
Best Programmers
Amit Mathur Well, that's the queue class. In it, we have four variables, the array, the
Vishal Bhardwaj
Deepesh Jain
start and end, and a boolean full. The constructor pArrayQueue(int
Vyom NetWork maxsize) initializes the queue, and allocates an array for data storage. The
isEmpty() method is self explanatory, it checks to see if start and end are
Our Services equal; this can only be in two situations: when the queue is empty, and when
the queue is full. It later checks the full variable and returns whether this
Get 9,000 Interview queue is empty or not.
Questions & Answers in
an eBook.
The insert(Object) method, accepts an Object as a parameter, checks
1 of 3 07/04/2010 12:59 PM
Array Queues - Java Data Structures [Link]
whether the queue is not full, and inserts it. The insert works by adding one
to start, and doing a mod with [Link] (the size of the array), the
resulting location is set to the incoming object. We later check to see if this
insertion caused the queue to become full, if yes, we note this by setting the
full variable to true.
The Object remove() method, doesn't accept any parameters, and
returns an Object. It first checks to see if the queue is full, if it is, it sets
9500+ Pages
full to false (since it will not be full after this removal). If it's not full, it
9000 Question &
checks if the queue is empty, by calling isEmpty(). If it is, the method
Answers
returns a null, indicating that there's been an error. This is usually a pretty
All Tech. Categories
bad bug inside a program, for it to try to remove something from an empty
14 MB Content
queue, so, you might want to do something more drastic in such a situation
(like an exception throw). The method continues by removing the end object
Get it now !!
from the queue. The removal is done in the same way insertion was done. By
adding one to the end, and later mod it with [Link] (array size), and
that position is returned.
There are other implementations of the same thing, a little
re-arrangement can make several if() statements disappear. The reason it's
like this is because it's pretty easy to think of it. Upon insertion, you add one
to start and mod, and upon removal, you add one to end and mod... easy?
Well, now that we know how it works, lets actually test it! I've modified
that pretty cool test driver from the stack example, and got it ready for this
queue, so, here it comes:
import [Link].*;
import pArrayQueue;
class pArrayQueueTest{
public static void main(String[] args){
pArrayQueue q = new pArrayQueue(10);
Integer j = null;
int i;
[Link]("starting...");
for(i=0;i<10;i++){
j = new Integer((int)([Link]() * 100));
[Link](j);
[Link]("insert: " + j);
}
while(![Link]()){
[Link]("remove: " + ((Integer)[Link]()));
}
[Link]("Done ;-)");
}
}
As you can see, it inserts ten random [Link] Objects onto
the queue, and later prints them out. The output from the program follows:
starting...
insert: 3
insert: 70
insert: 5
insert: 17
insert: 26
insert: 79
insert: 12
insert: 44
insert: 25
insert: 27
remove: 3
remove: 70
remove: 5
remove: 17
remove: 26
remove: 79
remove: 12
remove: 44
remove: 25
remove: 27
Done ;-)
2 of 3 07/04/2010 12:59 PM
Array Queues - Java Data Structures [Link]
I suggest you compare this output to the one from stack. It's almost
completely different. I guess that's it for this array implementation of this
FIFO data structure. And now, onto something more complex...
Back to Table of Contents
ADVERTISEM ENT
Ads by Google Java String Array Array Int Ocaml Array Char Array
Subscribe to SourceCodesWorld - Techies Talk
Email: Subscribe
Free eBook - Interview Questions: Get over 1,000 Interview
Questions in an eBook for free when you join JobsAssist. Just click on
the button below to join JobsAssist and you will immediately receive the
Free eBook with thousands of Interview Questions in an ebook when you
join.
Join & Get 1000 Interview Questions eBook Free!
Free Tutorials - HTML, Java, SQL, PHP, ASP, Perl, SQL Tutorials
Discuss Programming Problems and Freshers Jobs related issues
here.
Discuss Final Year Project Ideas here.
Google Search
Search
is a part of Vyom Network.
Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions |
Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS
Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting |
GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs
Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2010 [Link], All Rights Reserved.
Page URL: /articles/java/java-data-structures/Array_Queue.asp
Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review
3 of 3 07/04/2010 12:59 PM