Download presentation
Presentation is loading. Please wait.
1
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
2
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()
3
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
4
A java.util.Stack Example
Reverse a String demos/StackDemo.java – Notice warning on Stack s=new Stack();
5
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) ")); for (PhoneRecord pr : link) System.out.println(pr); Java has many other generic interfaces and classes Examples: Set, Map, Hashtable
6
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 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.
7
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
8
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
9
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
10
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; }
11
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
12
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
13
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)
14
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.