Abstract Data Types ADT Collection An object that
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(), is. Empty(), is. Full(), look(), reset() – look() is sometimes called peek() • Queue Operations – add(), remove(), is. Full(), is. Empty(), reset() • Tree Operations – insert(), remove(), find(), traverse(), breadth. First. Search(), depth. First. Search()
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/Stack. Demo. 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<String> vec = new Vector<String>(); // vector of Strings vec. add. Element("alpha"); vec. add. Element("beta"); System. out. println(vec. element. At(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 • Linked. List -- Note: List is an interface not a class List<Phone. Record> link = new Linked. List<Phone. Record>(); link. add(new Phone. Record("Roger", "(541) 997 -2918")); for (Phone. Record 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 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/Stack. Test. java demos/Stack. Of. Doubles. java shows a non-generic stack – it can store only double primitive values
Using dynamic storage Class Node { Object info; Node next; public Node(Object data) { info=data; next=null; } } node 1 = new Node("alpha"); node 1. next = new Node("beta"); node 1. next = new Node("gamma"); https: //webpages. sou. edu/~nordquip/cs 258 /demos/SLL. java "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 class Node {int info; Node[] next; }
Doubly Linked Class Node { Object info; Node next; Node prev; public Node(Object data) { info=data; next = prev =null; } } Node node 1 = new Node("alpha"); Node node 2 = new Node("beta"); Node node 3 = new Node("gamma"); node 1. next = node 2; node 2. next = node 3; node 3. prev = node 2; node 2. prev = node 1; null "alpha" "beta" "gamma" null
Algorithm: Evaluating Infix. Expressions Instantiate two stacks: operator, operand Remove white space from the string expression Break string expression into tokens (delimiters = "+-/*()") WHILE more tokens exist, get next. Token SWITCH next. Token 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 next. Token to operator stack * or /: WHILE top of operator() stack is '*' or '/' CALL eval() push next. Token to operator stack DEFAULT: Convert to double & push next. Token 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)
- Slides: 12