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 (ADTs) queues stacks dynamic structures linked lists basic programming concepts object oriented programming topics in computer science syllabus
unit 11 2 Abstract Data Types H An abstract data type (ADT) is an organized collection of information and a set of operations used to manage that information H The set of operations define the interface to the ADT H As long as the ADT accurately fulfills the promises of the interface, it doesn't really matter how the ADT is implemented
unit 11 3 Queues H A queue is similar to a list but adds items only to the end of the list and removes them from the front H It is called a FIFO data structure: First-In, First-Out H Analogy: a line of people at a bank teller’s window enqueue dequeue
unit 11 4 Interface for ADT Queue Queue public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws QueueEmptyException; public void enqueue(Object o); public Object dequeue() throws QueueEmptyException; }
unit 11 5 Stacks H A stack ADT is also linear, like a list or queue H Items are added and removed from only one end of a stack H It is therefore LIFO: Last-In, First-Out H Analogy: a stack of plates
unit 11 6 Stacks H Stacks are often drawn vertically: poppush
unit 11 7 Interface for ADT Stack Stack public interface Stack { public int size(); public boolean isEmpty(); public Object peek() throws StackEmptyException; public void push(Object o); public Object pop() throws StackEmptyException; }
unit 11 8 Push Implementation top: 5 elements
unit 11 9 Push Implementation top: 5 elements obj if (isFull()) throw Exception;
unit Push Implementation top: 5 elements obj if (isFull()) throw Exception; elements[top] = obj;
unit Push Implementation top: 6 elements if (isFull()) throw Exception; elements[top] = obj; top++;
unit Queue Implementation - Musical Chairs H Use an array of size n, and a rear index H Elements are always inserted to rear H Elements are removed from front, all other elements move as in musical chairs
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Remove frontrear
unit Circular Queue - Remove frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Remove frontrear
unit Circular Queue - Insert frontrear
unit Circular Queue - Insert frontrear what will happen if we add one more element?
unit Circular Queue - Operations H front and rear are always incremented modulu n H queue is full if (rear + 1) % n = front H queue is empty if rear = front H in an array of size n, only n-1 cells can be utilized
unit Static vs. Dynamic Structures A static data structure has a fixed size (this meaning is different than those associated with the static modifier) H Arrays are static; once you define the number of elements it can hold, it doesn’t change H A dynamic data structure grows and shrinks as required by the information it contains H Example of dynamic data structures: linked lists
unit Object References H Object reference is a variable that stores the address of an object H A reference can also be called a pointer H They are often depicted graphically: student John Smith
unit References as Links H Object references can be used to create links between objects Suppose a Student class contained a reference to another Student object John Smith Jane Jones
unit References as Links H References can be used to create a variety of linked structures, such as a linked list: studentList element next } node
unit “Generic” Node Class Node class Node { private Object element; private Node next; // two overloaded constructors public Node() { this(null,null); } public Node(Object element, Node next) { this.element = element; this.next = next; } // functionality methods void setElement(Object element) { //...} void setNext(Node next) { //...} Object getElement() { //...} Node getNext() { //...} }
unit A Linked Node Chain head next element null SingleLinkedList
Stack Implementation: linked list size: 0 top: null
size: 0 top: push(Object obj) obj
size: 0 top: n push(Object obj) { Node n = new Node(); obj push(Object obj)
size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); obj
size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); obj
size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; obj
size: 1 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; size++; } obj
size: 1 top:
size: 1 top: obj push(Object obj)
size: 1 top: n obj push(Object obj) { Node n = new Node();
size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj);
size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top);
size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n;
push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; size++; } size: 2 top: n obj
size: 2 top:
size: 2 top: Object pop() { if (isEmpty()) throw Exception;
Object pop() { if (isEmpty()) throw Exception; Object temp; size: 2 top: temp
Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); size: 2 top: temp
Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size: 2 top: temp
Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size--; size: 1 top: temp
Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size--; return temp; } size: 1 top: temp
size: 1 top: temp
size: 1 top: temp
size: 1 top: temp
interface Stack ArrayStack implements Stack ArrayStack(int size) push() throws StackFullException pop() isFull() size()... SinglyLinkedStack implements Stack push() pop() size()... push() pop() size()...
unit Using a Stack: Balanced Strings H A single character string which is not an opening or a closing character is a balanced string H If the strings s 1 and s 2 are balanced, then s 1 + s 2 (i.e., the concatenation of s 1 and s 2 ) is a balanced string H If a string s is balanced, and c op,c cl is a matching pair of opening and closing characters, then c op + s + c cl is a balanced string
unit Balanced String Verification Algorithm boolean isBalanced(String str) for each character c in str[0]... str[length-1]: if c is an opening character then push it onto the stack; if c is a closing character then if the stack is empty, return false; else // stack not empty pop the topmost character t from the stack; if t and c do not match return false otherwise // c is not a closing character do nothing (skip the character). if the stack is empty, return true, else return false.
unit Using a Queue: ComputationSimulation public abstract void computeOneStep() - Performs a single computation step public abstract boolean isCompleted() - Returns true if the computation is completed, false otherwise public Object getResult() - Returns the result of this computation, null if computation is not completed H …
unit An Iteration of a Time Sharing Machine computation queue
unit computation queue
unit computation queue computeOneStep() K times
unit computation queue isCompleted() ?
unit computation queue No ?
unit computation queue Yes ? getResult()
unit Collection Classes H The Java 2 platform contains a Collections API H This group of classes represent various data structures used to store and manage objects Their underlying implementation is implied in the class names, such as ArrayList and LinkedList Several interfaces are used to define operations on the collections, such as List, Set, SortedSet, Map, and SortedMap
unit When do we use dynamic structures? static H insert and remove items from unsorted lists H examine elements of lists dynamic H insert and remove items from sorted lists H length of list can change in orders of magnitude