|
|
||
| You are here: | ||
|
HL Mastery aspects: ADT stack
To give an example of a stack, we use a character node class. A stack of any type could be built using similar techniques. A simple Class. CharNode.java This Class is very bare bones. For a better implementation, methods like: top() return top of stack without popping it size() return number of items in stack should be added. CharacterStack.java Exceptions should also be thrown for errors like an attempt to pop an empty stack. ReverseMe.java
|
On this page: [ Abstract Data Types | Character Node | Character Stack | Applet ] Abstract Data TypesA stack is an ADT (Abstract Data Type) - that is, we describe it by its external behaviour rather than its internal structure. We have looked at arrays, linked lists, records and lists of records - these structures have been well-defined in code ("concrete"). Stacks can be described in terms of operations like pop, push, empty and top. The actual stack can be represented by an array or by a linked list , for example. Its operations (push, pop) are still the same. Stack as Linked List Consider a simple, singly linked list:
We can simply view this list as:
and we have a stack. New items to be pushed are added at the front (top) and items to be popped are removed from the front (top). /** /** import java.applet.Applet; Exercises Add an EmptyStackException to the CharacterStack Class and handle it within the ReverseMe Applet. This one is slightly more tricky. Write an Applet that checks to see if the opening and closing brackets match in an expression such as (a + b) / (c - d) * ( x / (y - z) ). The AppletSimple template has the layout you need. If you succeed, consider different kinds of bracket like square ([ ]) and braces ({ }). Construct an Applet that takes a String and prints out it's components in dictionary order. For example, if the String "reverse" is input, the output would be "eeerrsv". This is not a problem that requires a stack (but the techniques in ReverseMe will come in handy).
Related: [ Java home | Previous: lists | Next: queues ] |
Stacks have many uses as described over in theory topic 5.
|
|
|
|||
|
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 |