140 likes | 167 Vues
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
E N D
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 (PhoneRecordpr : 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 guarantees 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. This identifier is a type-variable. • Convention: name with all caps – usually a single character • Declare variables with the generic name: TYPE data[size]; • Instantiate a generic object: Stack<Double> stack = new Stack<Double>(); Note: The type shown inside <> must be an object type. See demos/StackTest.java demos/StackOfDoubles.java shows a non-generic stack – it can store only double primitive values
"alpha" "beta" "gamma" null Using dynamic storage Class Node { Object info; 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"); https://webpages.sou.edu/~nordquip/cs258/demos/SLL.java
Doubly linked list using nodesLists have single parent and single child. class Node { String info; Node next; Node previous;} • Tree using nodes:Trees have single parent and multiple children class Node {int info; Node[] next; }
"alpha" "beta" "gamma" null Doubly Linked null Class Node { Object info; Node next; Node prev; public Node(Object data) { info=data; next = prev =null; } } Node node1 = new Node("alpha"); Node node2 = new Node("beta"); Node node3 = new Node("gamma"); node1.next = node2; node2.next = node3; node3.prev = node2; node2.prev = node1;
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 SWITCHnextTokenLeft 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 stackDEFAULT: 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 push '/' to operator, push 2 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)