Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.

Similar presentations


Presentation on theme: "1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation."— Presentation transcript:

1 1 Lecture 24 Abstract 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

2 2 Lecture 24 Abstract 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 ADT’s, namely stack, queue and list, using different data structures.

3 3 Lecture 24 What 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)

4 4 Lecture 24 Stack..LIFO

5 5 Lecture 24 Stack

6 6 Lecture 24 Stack ADT Specification  For simplicity the above specification only caters for Stacks of type ‘char’. Later on we will consider how the stack can store any type. 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 */ }

7 7 Lecture 24 Stack ADT Implementation using Array public StackArray(int s) { maxSize = s; items = new char[maxSize]; stackTop = -1; } public boolean isEmpty() { return (stackTop == -1); } public boolean isFull() { return (stackTop == maxSize - 1); } public void push(char d) { items[++stackTop] = d; } public void pop() { stackTop--; } public char peek() { return items[stackTop]; } Empty StackPush a onto stackPush b onto stack aa b a Pop b from stack

8 8 Lecture 24 Example 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 i=0; i< len; i++) { s.push(input.charAt(i)); } String output = new String(); while (! s.isEmpty()) { char ch = s.peek(); output=output + ch; s.pop(); } return output; }

9 9 Lecture 24 Example 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: 

10 10 Lecture 24 Example 2- Bracket Matching (cont)  Consider the following string:a { b ( c [ d ] e ) f } a{ b{ ({ ( c{ ( [{ ( [ d{ ( [ ]{ ( e{ ( ){ f{ } character readstack 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.

11 11 Lecture 24 What 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)

12 12 Lecture 24 FIFO Queue

13 13 Lecture 24 Circular Queue

14 14 Lecture 24 Queue Implementation  2 options for fixed size implementation as an array  (1) Flat array - inefficient Head Tail Head Tail  (2) Circular array - much better

15 15 Lecture 24 Queue 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.

16 16 Lecture 24 Queue ADT Implementation public QueueArray(int s) { front = 0; rear = -1; nItems = 0; maxSize = s; queArray = new char[maxSize]; } public void enqueue(char j) { rear = (rear+1) % maxSize; queArray[rear] = j; nItems++; } public void dequeue() { nItems--; front = (front+1) % maxSize; } public char front() { return queArray[front]; } public boolean isEmpty() { return (nItems==0); } public boolean isFull() { return (nItems==maxSize); } public int size() { return nItems; }

17 17 Lecture 24 Dealing 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.top(); // error stack empty QueueArray q = new QueueArray(2); q.dequeue(); // error queue underflow q.enqueue(‘1’); q.enqueue(‘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

18 18 Lecture 24 Throwing 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.

19 19 Lecture 24 Creating 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(String 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.


Download ppt "1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation."

Similar presentations


Ads by Google