Title: Unit 11: Data Structures
1Unit 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
2Abstract 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
3Queues
- 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
4Interface 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
-
5Stacks
- 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
6Stacks
- Stacks are often drawn vertically
7Interface 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
-
8Push Implementation
elements
top 5
9Push Implementation
obj
elements
if (isFull()) throw Exception
top 5
10Push Implementation
obj
elements
if (isFull()) throw Exception elementstop
obj
top 5
11Push Implementation
elements
if (isFull()) throw Exception elementstop
obj top
top 6
12Queue 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
13Circular Queue - Insert
14Circular Queue - Insert
15Circular Queue - Insert
16Circular Queue - Insert
17Circular Queue - Remove
18Circular Queue - Remove
19Circular Queue - Insert
20Circular Queue - Insert
21Circular Queue - Insert
22Circular Queue - Insert
23Circular Queue - Insert
24Circular Queue - Remove
25Circular Queue - Insert
26Circular Queue - Insert
what will happen if we add one more element?
27Circular 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
28Static 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
29Object 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
30References as Links
- Object references can be used to create links
between objects - Suppose a Student class contained a reference to
another Student object
31References as Links
- References can be used to create a variety of
linked structures, such as a linked list
32Generic 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() // ...
33A Linked Node Chain
head
null
next
element
SingleLinkedList
34Stack Implementation linked list
null
top
size 0
35push(Object obj)
top
size 0
obj
36push(Object obj) Node n new Node()
push(Object obj)
top
size 0
n
obj
37push(Object obj) Node n new Node()
n.setElement(obj)
top
size 0
n
obj
38push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top)
top
size 0
n
obj
39push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
top
size 0
n
obj
40push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
size
top
size 1
n
obj
41top
size 1
42push(Object obj)
top
size 1
obj
43push(Object obj) Node n new Node()
top
size 1
n
obj
44push(Object obj) Node n new Node()
n.setElement(obj)
top
size 1
n
obj
45push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top)
top
size 1
n
obj
46push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
top
size 1
n
obj
47push(Object obj) Node n new Node()
n.setElement(obj) n.setNext(top) top n
size
top
size 2
n
obj
48top
size 2
49Object pop() if (isEmpty()) throw Exception
top
size 2
50Object pop() if (isEmpty()) throw
Exception Object temp
top
size 2
temp
51Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement()
top
size 2
temp
52Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement() top top.getNext()
top
size 2
temp
53Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement() top top.getNext() size--
top
size 1
temp
54Object pop() if (isEmpty()) throw
Exception Object temp temp
top.getElement() top top.getNext()
size-- return temp
top
size 1
temp
55top
size 1
temp
56top
size 1
temp
57top
size 1
temp
58push() pop() size() ...
interface Stack
ArrayStack implements Stack ArrayStack(int
size) push() throws StackFullException pop()
isFull() size() ...
SinglyLinkedStack implements Stack push()
pop() size() ...
59Using 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
60Balanced 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.
61Using 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 -
62An Iteration of a Time Sharing Machine
computation queue
63computation queue
64computation queue
K times
computeOneStep()
65computation queue
?
isCompleted()
66computation queue
No
?
67computation queue
?
Yes
getResult()
68Collection 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
69When 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