|
|
||
| You are here: | ||
|
5.2
Efficiency is discussed later on in this topic, basically, option 1 is an O(0) operation - does not depend on the size of the stack, whereas the second option is an O(n) operation.
The point being, there might not be any space left on the stack!
same static structure different pointers much the same process as for the stack
The flags Set at start
Main loop
modulo operator used to wrap around if we just added an item, the queue can't be empty
|
on this page: [ ibio | stacks | queues ] You should be able to compile these examples in any Java editor, such as BlueJ or JCreator. All of the examples assume that the IBIO methods exist in the file. An example class using these methods is found here . Make sure you can compile and run this source code if you want to implement any of the examples given in these pages. Any code you want to try out should be placed in the Constructor: /** You may want to change the Class name, in which case, remember that:
An array is going to be used to hold a sequence of characters:
Initially, a pointer, size , is set to 0, so stack[size] is the next available element. To push an item onto the stack we can work one of two ways:
It should be apparent that the second option involves shuffling up elements whereas the first only requires the stack pointer to be moved. Given: private static final int SIZE = 12; // a constant for the stack size A possible push operation is: // method to push an item onto the stack Which works up to a point. Is there any check that needs to be done? Add it. So far, the code might look like this: /** Exercises Write the method pop, to remove an item from the stack - being sure to watch out for underflow (when there are not any items on the stack). Write the algorithm which reverses a character String in place (a discussion of this is found on the previous page). To take an input String and split it into characters, you might need something like: String s = input("Please input a String"); To concatenate characters into a String try something like: String s; A useful method, top, returns the top element from the stack, if it exists, without popping it. Implement this. We have seen a possible way of implementing a queue using a linked list , but this section is concerned with static data structures. So let's see how we might go about using the same array as above, except that we will need two pointers into the array, front and rear. Recall the items are added at the rear ( enqueued ) and removed from the front ( dequeued ).
It seems reasonable to have both front and rear point to element 0 initially. To enqueue an item, place it at rear then increment rear to point at the next free location. Check to see if the end of the array has been reached: ** Exercise Implement the dequeue method, be sure to test if there is anything in the queue to remove. You might want to change the Constructor to allow the dequeue to be called: c = inputChar("Input a character ('/' to end, '#' to dequeue)"); You can either return a character from the dequeue method or output it within the method body. Once you have got that working, there are a few problems here - try adding asnd removing enough items and we get to a point where the rear pointer has run up against the end of the array, yet there is still space at the front of the array:
So, what we need to do is wrap the front pointer around to the start again. However, we have to be able to detect when the queue is full , when the rear pointer catches up with the front pointer. There are several ways to deal with this problem, the simplest of which is probably to maintain a special flag qFull which we will set true when the front reaches the rear and unset whenever we dequeue an item. It is also possible to use a qEmpty flag in a similar way. /** Exercises Write self-contained Queue Class that can enque and dequeue Strings or other Objects of your own choosing. The Class should implement Exception handling so that EmptyQueue, FullQueue and other custom exceptions can be thrown. Additional methods mught be getFront, getRear to examine, but not remove these items. A method to return the current size of the queue would also be useful. related: [ ADT home | previous: fundamentals | next: dynamic data structures ] |
Remember that you need not know how the IBIO methods work, only that you can use, eg, inputDouble() to input a double value. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||
|
Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: May 31, 2009 |