The Artima Developer Community
Sponsored Link

Java Answers Forum
IntegerQueue

2 replies on 1 page. Most recent reply: Mar 15, 2005 2:49 AM by Timothy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Timothy

Posts: 4
Nickname: houston
Registered: Mar, 2005

IntegerQueue Posted: Mar 15, 2005 2:15 AM
Reply to this message Reply
Advertisement
Hi,

I would like to use an array to implement an IntegerQueue class to store a queue of integers. I believe this class should have the same methods as the IntegerStack class which I have created below except that the push and pop methods should be renamed addToQueue and removeFromQueue respectively.

Here is what I have so far but it is more of a stack then a queue currently:

public class IntegerQueue
{
private int [] queue;
private int total; // to track number of items

public IntegerQueue (int sizeIn)
{ // size array with parameter
stack = new int [sizeIn];
// set number of items to zero
total = 0;
}

// add an item to the array
public boolean addToQueue (int j)
{
if( isFull() == false) // checks if space is stock
{
stack[total] = j; // add item
total++; // increment item counter
return true; //to indicate success
}
else
{
return false; // to indicate failure
}
}

// remove an item by obeying LIFO rule
public boolean removeFromQueue()
{
if( isEmpty() == false) // makes sure stack is not empty
{
total 0;
return true; // to indicate success
}
else
{
return false; // to indicate failure
}
}

// checks if array is empty
public boolean isEmpty()
{
if(total==0)
{
return true;
}
else
{
return false;
}
}

// checks if array is full
public boolean isFull()
{
if (total==stack.length)
{
return true;
}
else
{
return false;
}
}

// returns the ith item
public int getItem(int i)
{
return stack[i-1]; // ith item at position i-1
}

// return the number of items in the array
public int getTotal()
{
return total;
}
}


Any suggestions would be appreciated.

Thanks


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: IntegerQueue Posted: Mar 15, 2005 2:24 AM
Reply to this message Reply
Hi,

Jdk1.5 now provides implementation for Queue. So you can use that instead of implementing one your self.

Regards,
Amol Brid.

Timothy

Posts: 4
Nickname: houston
Registered: Mar, 2005

Re: IntegerQueue Posted: Mar 15, 2005 2:49 AM
Reply to this message Reply
Thanks for your suggestion but i would like to implement it myself to understand clearly.

Flat View: This topic has 2 replies on 1 page
Topic: Hi all Previous Topic   Next Topic Topic: Free and programing is possible with a new PC. No questions just promotiona

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use