Title: Abstract Data Types (ADT)
1Abstract Data Types (ADT) I
- Overview
- What is an Abstract Data type?
- What is Stack ADT?
- Stack ADT Specifications
- Array Implementation of Stack ADT
- Example Applications of Stack
- What is Queue ADT?
- Queue ADT Specifications
- Queue ADT Implementation
- Handling UnderFlow/OverFlow Exceptions
- Preview Abstract Data Types - II
2Abstract Data Types I
- An abstract data type (ADT) is a data type (a set
of values and a collection of operations on those
values) that is accessed only through an
interface. - Data structure on the other hand, is a
representation (in a computer) for a collection
of related information. Example Array. - An ADT may be implemented using different data
structures, but the client program must access it
through its interface. - This separation of interface (what) from
implementation (how) is very important and has
many advantages. Some of these are - Allows program code to be more generic/reusable
- Hide unnecessary information
- Allows Easier software maintenance
- Help manage software complexity
- In this lecture and the next two, we shall learn
how to design and implement some basic ADTs,
namely stack, queue and list, using different
data structures.
3What is Stack ADT?
- A stack is an ADT which accepts elements one at a
time for storage and presents them for retrieval
in reverse order - a LIFO (last in, first out) structure
- The following Operations are normally defined for
a Stack - push (place an element on the top of the stack)
- pop (remove an element from the top of the stack)
- peek (return top element on the stack)
- isEmpty (boolean function to check for an empty
stack) - isFull (optional boolean test to check if stack
is full)
4Stack..LIFO
- click here on this link Array Implementation of
Stack
5Stack
6Stack ADT Specification
public class StackArray private int
maxSize private char items private int
stackTop public StackArray(int size)
/ construct Stack of specified size /
public boolean isEmpty() / return TRUE if
empty else false / public boolean isFull()
/ return TRUE if Stack is full / public
void push(char d) / push element onto Stack
/ public void pop() / remove first
element from stack / public char peek()
/ return first element in Stack /
- For simplicity the above specification only
caters for Stacks of type char. Later on we
will consider how the stack can store any type.
7Stack ADT Implementation using Array
public StackArray(int s) maxSize s
items new charmaxSize stackTop -1
public boolean isEmpty() return
(stackTop -1) public boolean isFull()
return (stackTop maxSize - 1) public
void push(char d) itemsstackTop d
public void pop() stackTop-- public
char peek() return itemsstackTop
b
a
a
a
Empty Stack
Push a onto stack
Push b onto stack
Pop b from stack
8Example 1 - String Reversal
- Requirement method to reverse a string.
public String reverseInput (String input)
int len input.length() StackArray s
new StackArray(len) for(int i0 ilt
len i) s.push(input.charAt(i))
String output new String() while
(! s.isEmpty()) char ch s.peek()
outputoutput ch s.pop()
return output
9Example 2- Bracket Matching
- Stacks are often used in parsing algorithms used
by expression evaluators or compilers. - Consider an algorithm to validate the delimiters
in a line of text. The delimiters will include
square brackets and , braces and ,
and round brackets ( and ). Each opening
delimiter should be matched by a corresponding
closing delimiter. For example -
10Example 2- Bracket Matching (cont)
- Consider the following string a b ( c d e
) f
character read stack contents
- On reading opening delimiter, place it on stack.
On reading a closing delimiter remove top element
from stack and compare. If delimiter removed from
stack does not match closing delimiter then the
expression is invalid.
a b ( ( c ( ( d
( ( e ( ) f
11What is Queue ADT?
- A queue is a linear data type consisting of
elements of a single type. Elements join a queue
at one end and leave at the other. - a FIFO (first in first out) structure.
- What happens at an orderly bus stop is a
reasonable model for the operations that apply to
a queue. - enqueue (place element at end of queue)
- dequeue (remove element from start of queue)
- front (return first element in queue)
- isEmpty (boolean check for empty queue)
- size (return number of items in the queue)
- isFull (optional boolean test for a full queue)
12FIFO Queue
- Click on this this link Array Implementation of
Queue
13Circular Queue
14Queue Implementation
- 2 options for fixed size implementation as an
array - (1) Flat array - inefficient
J
J
J
J
Head
Tail
- (2) Circular array - much better
J
J
J
J
Head
Tail
15Queue ADT Specification (Circular Array)
public class QueueArray private int maxSize
private char queArray private int front,
rear, nItems public QueueArray(int s) /
create new queue of fixed size / public
boolean isEmpty() / test if Queue is empty
/ public boolean isFull() / test if Queue
is full / public int size() / return
length of Queue / public void enqueue(char c)
/ add element c to queue / public void
dequeue() / remove first element in queue/
public char front() / return first element
in Queue /
- For simplicity the above specification only
caters for Queues of type char. Later on we
will consider how the queue can store any type.
16Queue ADT Implementation
public char front() return
queArrayfront public boolean
isEmpty() return (nItems0)
public boolean isFull() return
(nItemsmaxSize) public int size()
return nItems
public QueueArray(int s) front 0
rear -1 nItems 0 maxSize s
queArray new charmaxSize public
void enqueue(char j) rear (rear1)
maxSize queArrayrear j nItems
public void dequeue() nItems--
front (front1) maxSize
17Dealing With Exceptional Conditions
- Neither of the implementations for Stacks or
Queues handled exceptional conditions. For
example - adding an element to a full stack or queue
- removing an element from an empty stack or queue
StackArray s new StackArray(2) s.pop()
// error stack underflow s.push(1) s.push(2)
s.push(3) // error stack overflow s.pop() s.
pop() s.top() // error stack empty
QueueArray q new QueueArray(2) q.dequeue()
// error queue underflow q.enqueue(1) q.enque
ue(2) q.enqueue(3) // error queue
overflow q.dequeue() q.dequeue) q.front()
// error queue empty
- We can use Java Exception Handling Mechanism to
deal with these unexpected events
18Throwing an Exception in Java
- The following implementation of the stack pop
method now checks for stack underflow (i.e the
stack is already empty) and if it occurs an
exception is thrown to indicate the error. NOTE
the exception is not handled locally but is
propagated to the calling method
public void pop() throws Exception if
(this.isEmpty()) // check for underflow throw
new Exception(stack underflow)
else stacktop--
- A generic Exception is thrown. In the next slide
we will see how to create a new
StackOutOfBoundsException for use in our Stack
class.
19Creating an Exception Class in Java
- If none of the standard Java exception classes
are suitable you can create your own by deriving
it from the base class Exception or a child class
such as IndexOutOfBoundsException
import java.lang.IndexOutOfBoundsException class
StackOutOfBoundsException extends
IndexOutOfBoundsException public
StackOutOfBoundsException() // default
constructor public StackOutOfBoundsException(S
tring problem) super(problem) // let
superclass constructor handle the argument
- As the above StackOutOfBoundsException inherits
from IndexOutOfBoundsException it is an example
of a runtime exception and therefore there is no
obligation to catch this type of exception, but
doing so will stop the program from terminating
with an error.