Unit 11: Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Unit 11: Data Structures

Description:

... line of people at a bank teller's window. enqueue. dequeue. 4. unit 11 ... queue is empty if rear = front. in an array of size n, only n-1 cells can be utilized ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 70
Provided by: daphnawe
Category:
Tags: data | structures | unit

less

Transcript and Presenter's Notes

Title: Unit 11: Data Structures


1
Unit 11 Data Structures
  • We explore some simple techniques for organizing
    and managing information
  • This unit focuses on
  • Abstract Data Types (ADTs)
  • queues
  • stacks
  • dynamic structures
  • linked lists

syllabus
basic programming concepts
object oriented programming
topics in computer science
2
Abstract Data Types
  • An abstract data type (ADT) is an organized
    collection of information and a set of operations
    used to manage that information
  • The set of operations define the interface to the
    ADT
  • As long as the ADT accurately fulfills the
    promises of the interface, it doesn't really
    matter how the ADT is implemented

3
Queues
  • A queue is similar to a list but adds items only
    to the end of the list and removes them from the
    front
  • It is called a FIFO data structure First-In,
    First-Out
  • Analogy a line of people at a bank tellers
    window

4
Interface for ADT 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
Stacks
  • A stack ADT is also linear, like a list or queue
  • Items are added and removed from only one end of
    a stack
  • It is therefore LIFO Last-In, First-Out
  • Analogy a stack of plates

6
Stacks
  • Stacks are often drawn vertically

7
Interface for ADT 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
Push Implementation
elements
top 5
9
Push Implementation
obj
elements
if (isFull()) throw Exception
top 5
10
Push Implementation
obj
elements
if (isFull()) throw Exception elementstop
obj
top 5
11
Push Implementation
elements
if (isFull()) throw Exception elementstop
obj top
top 6
12
Queue Implementation - Musical Chairs
  • Use an array of size n, and a rear index
  • Elements are always inserted to rear
  • Elements are removed from front, all other
    elements move as in musical chairs

13
Circular Queue - Insert
14
Circular Queue - Insert
15
Circular Queue - Insert
16
Circular Queue - Insert
17
Circular Queue - Remove
18
Circular Queue - Remove
19
Circular Queue - Insert
20
Circular Queue - Insert
21
Circular Queue - Insert
22
Circular Queue - Insert
23
Circular Queue - Insert
24
Circular Queue - Remove
25
Circular Queue - Insert
26
Circular Queue - Insert
what will happen if we add one more element?
27
Circular Queue - Operations
  • front and rear are always incremented modulu n
  • queue is full if (rear 1) n front
  • queue is empty if rear front
  • in an array of size n, only n-1 cells can be
    utilized

28
Static vs. Dynamic Structures
  • A static data structure has a fixed size (this
    meaning is different than those associated with
    the static modifier)
  • Arrays are static once you define the number of
    elements it can hold, it doesnt change
  • A dynamic data structure grows and shrinks as
    required by the information it contains
  • Example of dynamic data structures linked lists

29
Object References
  • Object reference is a variable that stores the
    address of an object
  • A reference can also be called a pointer
  • They are often depicted graphically

30
References as Links
  • Object references can be used to create links
    between objects
  • Suppose a Student class contained a reference to
    another Student object

31
References as Links
  • References can be used to create a variety of
    linked structures, such as a linked list

32
Generic Node Class
  • 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
A Linked Node Chain
head
null
next
element
SingleLinkedList
34
Stack Implementation linked list
null
top
size 0
35
push(Object obj)
top
size 0
obj
36
push(Object obj) Node n new Node()
push(Object obj)
top
size 0
n
obj
37
push(Object obj) Node n new Node()
n.setElement(obj)
top
size 0
n
obj
38
push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top)
top
size 0
n
obj
39
push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
top
size 0
n
obj
40
push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
size
top
size 1
n
obj
41
top
size 1
42
push(Object obj)
top
size 1
obj
43
push(Object obj) Node n new Node()
top
size 1
n
obj
44
push(Object obj) Node n new Node()
n.setElement(obj)
top
size 1
n
obj
45
push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top)
top
size 1
n
obj
46
push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
top
size 1
n
obj
47
push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
size
top
size 2
n
obj
48
top
size 2
49
Object pop() if (isEmpty()) throw Exception
top
size 2
50
Object pop() if (isEmpty()) throw
Exception Object temp
top
size 2
temp
51
Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement()
top
size 2
temp
52
Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement() top top.getNext()
top
size 2
temp
53
Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement() top top.getNext() size--
top
size 1
temp
54
Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement() top top.getNext()
size-- return temp
top
size 1
temp
55
top
size 1
temp
56
top
size 1
temp
57
top
size 1
temp
58
push() pop() size() ...
interface Stack
ArrayStack implements Stack ArrayStack(int
size) push() throws StackFullException pop()
isFull() size() ...
SinglyLinkedStack implements Stack push()
pop() size() ...
59
Using a Stack Balanced Strings
  • A single character string which is not an opening
    or a closing character is a balanced string
  • If the strings s1 and s2 are balanced, then s1
    s2 (i.e., the concatenation of s1 and s2) is a
    balanced string
  • If a string s is balanced , and cop,ccl is a
    matching pair of opening and closing characters,
    then cop s ccl is a balanced string

60
Balanced String Verification Algorithm
  • boolean isBalanced(String str)
  • for each character c in str0 ... strlength-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
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

62
An Iteration of a Time Sharing Machine
computation queue
63
computation queue
64
computation queue
K times
computeOneStep()
65
computation queue
?
isCompleted()
66
computation queue
No
?
67
computation queue
?
Yes
getResult()
68
Collection Classes
  • The Java 2 platform contains a Collections API
  • 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
When do we use dynamic structures?
  • static
  • insert and remove items from unsorted lists
  • examine elements of lists
  • dynamic
  • insert and remove items from sorted lists
  • length of list can change in orders of magnitude
Write a Comment
User Comments (0)
About PowerShow.com