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

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Stacks, Queues, and Linked Lists
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks, Queues & Deques CSC212.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
TCSS 342, Winter 2005 Lecture Notes
Implementing and Using Stacks
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Topic 3 The Stack ADT.
1 / 23 COP 3540 Data Structures with OOP Chapter 4 Stacks and Queues.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Stack and Queue.
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Data Structures & Algorithms
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Stacks and Queues.
Stacks and Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
Stack and Queue APURBO DATTA.
Stacks and Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Stacks, Queues, and Deques
Stacks and Queues.
Stacks and Queues CLRS, Section 10.1.
slides created by Alyssa Harding
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
Stacks and Queues.
Lecture 9: Stack and Queue
Presentation transcript:

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 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 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 Lecture 24 Stack..LIFO

5 Lecture 24 Stack

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 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 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 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 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 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 Lecture 24 FIFO Queue

13 Lecture 24 Circular Queue

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 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 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 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 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 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.