Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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

4 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; }

5 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

6 unit 11 6 Stacks H Stacks are often drawn vertically: poppush

7 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; }

8 unit 11 8 Push Implementation top: 5 elements

9 unit 11 9 Push Implementation top: 5 elements obj if (isFull()) throw Exception;

10 unit 11 10 Push Implementation top: 5 elements obj if (isFull()) throw Exception; elements[top] = obj;

11 unit 11 11 Push Implementation top: 6 elements if (isFull()) throw Exception; elements[top] = obj; top++;

12 unit 11 12 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

13 unit 11 13 Circular Queue - Insert frontrear

14 unit 11 14 Circular Queue - Insert frontrear

15 unit 11 15 Circular Queue - Insert frontrear

16 unit 11 16 Circular Queue - Insert frontrear

17 unit 11 17 Circular Queue - Remove frontrear

18 unit 11 18 Circular Queue - Remove frontrear

19 unit 11 19 Circular Queue - Insert frontrear

20 unit 11 20 Circular Queue - Insert frontrear

21 unit 11 21 Circular Queue - Insert frontrear

22 unit 11 22 Circular Queue - Insert frontrear

23 unit 11 23 Circular Queue - Insert frontrear

24 unit 11 24 Circular Queue - Remove frontrear

25 unit 11 25 Circular Queue - Insert frontrear

26 unit 11 26 Circular Queue - Insert frontrear what will happen if we add one more element?

27 unit 11 27 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

28 unit 11 28 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

29 unit 11 29 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 40725 3.57

30 unit 11 30 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 40725 3.57 Jane Jones 58821 3.72

31 unit 11 31 References as Links H References can be used to create a variety of linked structures, such as a linked list: studentList element next } node

32 unit 11 32 “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() { //...} }

33 unit 11 33 A Linked Node Chain head next element null SingleLinkedList

34 Stack Implementation: linked list size: 0 top: null

35 size: 0 top: push(Object obj) obj

36 size: 0 top: n push(Object obj) { Node n = new Node(); obj push(Object obj)

37 size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); obj

38 size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); obj

39 size: 0 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; obj

40 size: 1 top: n push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; size++; } obj

41 size: 1 top:

42 size: 1 top: obj push(Object obj)

43 size: 1 top: n obj push(Object obj) { Node n = new Node();

44 size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj);

45 size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top);

46 size: 1 top: n obj push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n;

47 push(Object obj) { Node n = new Node(); n.setElement(obj); n.setNext(top); top = n; size++; } size: 2 top: n obj

48 size: 2 top:

49 size: 2 top: Object pop() { if (isEmpty()) throw Exception;

50 Object pop() { if (isEmpty()) throw Exception; Object temp; size: 2 top: temp

51 Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); size: 2 top: temp

52 Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size: 2 top: temp

53 Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size--; size: 1 top: temp

54 Object pop() { if (isEmpty()) throw Exception; Object temp; temp = top.getElement(); top = top.getNext(); size--; return temp; } size: 1 top: temp

55 size: 1 top: temp

56 size: 1 top: temp

57 size: 1 top: temp

58 interface Stack ArrayStack implements Stack ArrayStack(int size) push() throws StackFullException pop() isFull() size()... SinglyLinkedStack implements Stack push() pop() size()... push() pop() size()...

59 unit 11 59 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

60 unit 11 60 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.

61 unit 11 61 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 …

62 unit 11 62 An Iteration of a Time Sharing Machine computation queue

63 unit 11 63 computation queue

64 unit 11 64 computation queue computeOneStep() K times

65 unit 11 65 computation queue isCompleted() ?

66 unit 11 66 computation queue No ?

67 unit 11 67 computation queue Yes ? getResult()

68 unit 11 68 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

69 unit 11 69 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


Download ppt "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."

Similar presentations


Ads by Google