Abstract Data Types (ADT)

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 24 Lists, Stacks, and Queues
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.
CS Data Structures II Review COSC 2006 April 14, 2017
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
TCSS 342, Winter 2005 Lecture Notes
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
STACKS & QUEUES for CLASS XII ( C++).
Stack: a Linked Implementation
The List ADT.
Computing with C# and the .NET Framework
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Chapter 19 Java Data Structures
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks.
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.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Stacks Chapter 4.
Chapter 17 Object-Oriented Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Introduction to Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Stacks, Queues, and Deques
Arrays and Linked Lists
Stacks and Queues.
Dynamic Data Structures and Generics
Stacks with Dynamic Memory
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CSC 1052 Stacks 1/11/2019.
Abstract Data Types (ADT)
Stacks.
Dynamic Data Structures and Generics
More Data Structures (Part 1)
Stacks, Queues, and Deques
Stacks and Queues.
5.3 Implementing a Stack Chapter 5 – The Stack.
LINEAR DATA STRUCTURES
Presentation transcript:

Abstract Data Types (ADT) Collection An object that holds other objects Homogeneous Collection All objects are of the same type Example: Array of integers Heterogeneous Collection At least two objects in the collection have different types Example: An array of objects Abstract Data Type A data structure and associated methods for manipulating the underlying data Examples: stack, queue, list, tree, graph In some sense, all Java classes are ADTs, but we usually think of an ADT as a collection of objects

Stacks/Queues/Dynamic lists Key: a well defined interface limiting operations on the underlying data Key: We can change the implementation of interface without effecting users of the interface Stack Operations push(), pop(), isEmpty(), isFull(), look(), reset() look() is sometimes called peek() Queue Operations add(), remove(), isFull(), isEmpty(), reset() Tree Operations insert(), remove(), find(), traverse(), breadthFirstSearch(), depthFirstSearch()

Implementation Possibilities Use java.util.Stack Advantage: standard data type supplied by Java Disadvantage: inefficient Using Arrays Advantages: fast, native Java data structure Disadvantage: Maximum size must be specified in advance or new underlying array created and old elements copied to new array when old array full

A java.util.Stack Example Reverse a String demos/StackDemo.java – Notice warning on Stack s=new Stack();

Java Parameterized (Generic) Classes Paraphrased from Wikipedia: a class or method that can operate on objects of various types while providing compile-time type safety. Often used for collections to ensure all elements are the same type. Examples from the java library: Vector Vector<String> vec = new Vector<String>(); // vector of Strings vec.addElement("alpha"); vec.addElement("beta"); System.out.println(vec.elementAt(0)); Stack (of Doubles) Stack<Double> stk = new Stack<Double>(); stk.push(2.5); stk.push(42.42); // Autoboxing converts primitive double System.out.println(stk.pop()); // values to Double objects LinkedList -- Note: List is an interface not a class List<PhoneRecord> link = new LinkedList<PhoneRecord>(); link.add(new PhoneRecord("Roger", "(541) 997-2918")); for (PhoneRecord pr : link) System.out.println(pr); Java has many other generic interfaces and classes Examples: Set, Map, Hashtable

Create your own Generic class Purpose: Create a homogenous ADT that is guaranteed all objects in the collection are of compatible types. Define class: public class Stack<TYPE> TYPE: can be any sequence of letters, numbers, and underscores. It represents the particular data type Convention: name with all caps Declare variables with the generic name: TYPE data[size]; Instantiate a generic object: Stack<Double> stack = new Stack<Double>(); Note: Generics cannot use primitives; use Double not double Note: Java version 8: Stack<Double> stack = new Stack<>(); is legal. See demos/StackTest.java demos/StackOfDoubles.java shows a non-generic stack – it can store only doubles … Generics and ‘wildcard generics’ From: Tor Iver Wilhelmsen, Apr 14, 2005, on http://www.thecodingforums.com/threads/e-and-extends-e.142735/ Ex 1: public class TreeSet<E> extends AbstractSet<E> implements SortedSet<E>, Cloneable, java.io.Serializable Generic class declaration: The "parameter" E is replaced by the programmer when using the class, e.g. TreeSet<String>. Ex 2: public boolean addAll(Collection<? extends E> c) Wildcard generic parameter: It means that the parameter can be any generic Collection where the parametrized type extends the collection. So if you have class Foo { ... class Fie extends Foo { ... set = new TreeSet<Foo>(); otherCollection = new ArrayList<Fie>(); you can do a set.addAll(otherCollection); because the generic parameter for the second declaration is a class that extends the parameter class of the first.

An Efficient Fixed-size Queue A circular array store data in consecutive locations When we reach the bottom, wrap around to the top if it is available Example Add 4.2, 9.2, 7.5, 1.9, 0.4 to the queue Remove an element from the queue (the 4.2) Where should the next element go? Is the queue full? 4.2 1 9.2 2 7.5 3 1.9 4 0.4 data

How do we check if the queue is full? Insert: Instance variables head and tail hold array indices init both to -1 int head, int tail, double data[] update tail before insertion, and update head after removal How do we check if the queue is full? (tail + 1)%data.length == head Insert: If (full), throw exception. Non-fixed-size or could create new extended array at this point If (head == -1), head = 0; tail = (tail+1)%data.length data[tail] = value to insert Remove: If (head == -1) throw exception retVal = data[head] If (head == tail), head = tail = -1; Else head = (head + 1)% data.length Return retVal

Using dynamic storage Class Node { Object info; "alpha" Node next; public Node(Object data) { info=data; next=null; } } node1 = new Node("alpha"); node1.next = new Node("beta"); node1.next.next = new Node("gamma"); "alpha" "beta" "gamma" null

Doubly linked list using nodes Lists have single parent and single child. class Node { String info; Node next; Node previous; } Tree using nodes: Trees have single parent and multiple children {int info; Node[] next; }

Doubly Linked Class Node null { Object info; Node next; "alpha" public Node(Object data) { info=data; next = prev =null; } } Node node1 = new Node("alpha"); Node node2 = new Node("gamma"); Node node3 = new Node("gamma"); node1.next = node2; node2.next = node3; node3.prev = node2; node2.prev = node1; null "alpha" "beta" "gamma" null

Algorithm: Evaluating InfixExpressions Instantiate two stacks: operator, operand Remove white space from the string expression Break string expression into tokens (delimiters = "+-/*()") WHILE more tokens exist, get nextToken SWITCH nextToken Left paren: push '(' on operator stack : Right paren: WHILE top of operator stack !='(' CALL Eval() Pop the matching '(' + or –: WHILE top of operator stack is '+', '-', '*', or '/' CALL eval() push nextToken to operator stack * or /: WHILE top of operator() stack is '*' or '/' CALL eval() push nextToken to operator stack DEFAULT: Convert to double & push nextToken to operand stack WHILE operator stack is not empty CALL eval() Result is on top of operand stack Note: eval() method pop two operands and one operator, calculate, and push result onto operand stack

Example Expression Algorithm Evaluate: "1+2*16/4" push 1 to operand push '+' to operator push 2 to operand push '*' to operator push 16 to operand Call Eval: pop 16, pop 2, pop '*', 2*16==32, push 32 to operand push '/' to operator push 4 to operand Call Eval: pop 4, pop 32, pop '/,' 32/4==8, push 8 to operand Call Eval: pop 8, pop 1, pop '+,' 1+8==9, push 9 to operand Result is at top of operand stack (9)

Example Expression Algorithm Evaluate: "3+(5+6/2)*3/2-4" push 3 to operand, push '+' to operator, push '(' to operator push 5 to operand, push '+' to operator, push 6 to operand push '/' to operator, push 2 to operand Call Eval: pop 2, pop 6, pop '/', 6/2 == 3, push 3 to operand Call Eval: pop 3, pop 5, pop '+', 5+3 == 8, push 8 to operand pop ‘(' push '*' to operator, push 3 to operand Call Eval: pop 3 pop 8, pop ‘*’, 8*3 == 24, push 24 to operand Call Eval: pop 2, pop 24, pop ‘/’, 24/2 ==12, push 12 to operand Call Eval: pop 12, pop 3, pop ‘+’, 3+12 == 15, push 15 to operand Push ‘-’ to operator, push 4 to operand Call Eval: pop 4, pop 15, pop ‘-’, 15-4==11, push 11 to operand Result is at top of operand stack (11)