22
Transparent Queue via ArrayCirc Myth or Reality? By Andrew Breen and Dan Kottke

Transparent Queue via ArrayCirc Myth or Reality? By Andrew Breen and Dan Kottke

Embed Size (px)

Citation preview

Transparent Queue via ArrayCirc

Myth or Reality?

By Andrew Breen and

Dan Kottke

In a nutshell

• TransparentQueueViaArrayCirc

• This is derived from the existing class QueueViaArrayCirc– In this class a queue is implemented with an

array– It simulates a queue through algorithms that

allot a portion of the array to function as a virtual queue

The Transparency Possibility

• The QueueViaArrayCirc creates a queue structure that allows for the standard queue operations

• frontof(), enqueue(), dequeue(), isEmpty()

• Transparency will allow for alterations to be made to or allows the user to view the contents of objects anywhere in the queue.

Our Approach

• Needed methods - view(), insert(), replace(), remove()

• How do you go about doing this?

The Class

• The class extends QueueViaArrayCirc

• The class inherits all of the necessary components of the parent class (the exception is doubler())

• We used int numInLine for all four methods as a means to refer to the location of a specific item in the queue (numInLine starts at 1)

View

• Return actual[front+numInLine-1]

• this effectively returns the value of the specific place in the queue

Replace

• Object temp = actual[front+numInLine-1];

• actual[front+numInLine-1] = item;

• return temp;

• this will take the item that is currently in the numInLine spot in the queue, return it and change it to the item that is passed in.

• Easy for array implementation because items in queue do not need to shift

Insert explanation

• Insert allows for the possibility for the queue size to exceed the size of the array, for this we use the doubler method, which we copied from the parent class.

• We allow for the case of inserting at the end of the queue by calling enqueue().

Insert explanation cont

• We had an int j to represent the array location directly after the last value of the queue located in the array.

• We looped qSize - numInLine times and made the value at j in actual become the value of j-1 in actual, then decremented j.

• We took care of the case where j = 0 by making the data at j equal the data at actual.length-1, then decrementing j.

Insert explanation..

• We then set the data at the location in the array equivalent to numInLine to be the item inserted, and incremented qSize.

• With all this we took into account the loop around of the circular structure using the mod function (%)

• ie. Int j = (front+qSize)%actual.length;

• All of the elements in the queue after the inserted item need to be slid up.

Remove explanation

• With remove, all the elements of the queue in the array after the removed item need to be slid down

• We allow for the case of removing at position 1 by calling dequeue().

• The object in the array location equating to numInLine is held as temp. temp is returned at the end of the method

Remove...

• Int j is instantiated to equal this location

• A for loop is constructed to iterate the appropriate number of times to slide each value in the array down one spot to simulate removing in the queue.

• J is incremented to keep track of the data shift. Then queue size is decremented.

• As with insert the mod function is used to simulate the queue by looping around array

Remove…

• Ie. Actual[j]=actual[(j+1)%actual.length]

• j=(j+1)%actual.length

The Driver

• The Driver consists of three methods:– main()

• The heart of the driver and does most of the work

– fill()• Fills the queue with random number objects

– printout()• Prints out the objects in the queue in a readable

fashion

Fill(int fillsize)

• This method has a for loop that goes from 1 to fillsize and uses the insert method to put random number objects into each spot

Printout(int printsize)

• If the queue is empty this method says the queue is empty and aborts other actions

• This method uses a for loop starting at 1 and ending at printsize. Inside the loop the view method is used to view the current spot in the queue.

• The method print statements that make the code print out the information 5 per line with commas in between

Main()

• The main method uses int start=25 and passes that value to the fill method to fill the queue with 25 random number objects

• Then it uses the printout method to print out those items.

• Next it uses a for loop to replace all of the objects with new random number objects. This shows that the replace method works correctly

Main()...

• The main method then uses the printout method to print all the new items. This way the user can see that the replace method did really work

• Once that is done, it uses a for loop from 1 to start and prints out the remove method.

• Lastly it calls the printout method, which will say that the queue is empty as it should be

Timing Analysis

• View is constant timing. It’s quick because it’s an array. Probably the fastest possible implementation

• Replace is constant time. Also quick because of the properties of the array. This is probably the fastest implementation

Timing Analysis..

• Insert is linear timed depending on the size of the array and numInLine. We considered trying to make it constant time by creating an elaborate algorithm using tags to delineate objects inserted in the array out of sequential order. This proved too tedious a task and would probably create more buggy programing

Timing Analysis..

• Remove is also linear timed depending on size of the array and numInLine. We considered trying to make it constant time by creating an elaborate algorithm using tags to delineate objects removed from the array out of sequential order.

Closing Thoughts

• We feel we’ve created a fairly efficient class and driver.

• Simple enough to follow and hopefully errorproof