Lec 4
Lec 4
Fang Yu
¡ Additional methods:
¡ size()
¡ isEmpty()
Array-based Implementation
¡ Use an array A of size N
A
0 1 2 i n
A
0 1 2 i n
A o
0 1 2 i n
Element Removal
¡ In operation remove(i), we need to fill the hole left by the
removed element by shifting backward the n - i - 1
elements A[i + 1], …, A[n - 1]
¡ In the worst case (i = 0), this takes O(n) time
A o
0 1 2 i n
A
0 1 2 i n
A
0 1 2 i n
Performance
¡ In the array based implementation of an array list:
¡ The space used by the data structure is O(n)
¡ size, isEmpty, get and set run in O(1) time
¡ add and remove run in O(n) time in the worst case
¡ In an add operation, when the array is full, instead of throwing an
exception, we can replace the array with a larger one
Growthable Array-based Array
List
¡ In an add(o) operation (without an index), we always
add at the end
3n - 1
8
¡ T(n) is O(n)
A B C D
The Node Class for Singly
Linked List
public class Node { // Instance variables
private Object element;
private Node next;
public Node() { // Creates a node with null
this(null, null); // references to its element and next node.
}
public Node(Object e, Node n) { // Creates a node with the given element
element = e;
next = n;
// and next node.
}
public Object getElement() { // Accessor methods
return element;
}
public Node getNext() {
return next;
}
public void setElement(Object newElem) {// Modifier methods
element = newElem;
}
public void setNext(Node newNext) {
next = newNext;
}
}
Inserting at the Head
1. Allocate a new node
¡ There is no constant-time
way to update the tail to
point to the previous node
Stack as a Linked List
¡ We can implement a stack with a singly linked list
¡ The space used is O(n) and each operation of the Stack ADT takes O(1)
time
nodes
t ∅
elements
Queue as a Linked List
¡ We can implement a queue with a singly linked list
¡ The front element is stored at the first node
¡ The rear element is stored at the last node
¡ The space used is O(n) and each operation of the Queue ADT takes O(1)
time
r
nodes
f
∅
elements
Position ADT
¡ The Position ADT models the notion of place within a data
structure where a single object is stored
elements
Insertion
¡ We visualize operation insertAfter(p, X), which returns position q
A B C
A B q C
X
p q
A B X C
Insertion Algorithm
Algorithm addAfter(p,e):
Create a new node v
v.setElement(e)
v.setPrev(p) //link v to its predecessor
v.setNext(p.getNext()) //link v to its successor
(p.getNext()).setPrev(v) //link p’s old successor to v
p.setNext(v) //link p to its new successor, v
return v //the position for the element e
Deletion
¡ We visualize remove(p), where p = last()
p
A B C D
A B C p
A B C
Deletion Algorithm
Algorithm remove(p):
t = p.element //a temporary variable to hold the return value
(p.getPrev()).setNext(p.getNext()) //linking out p
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null) //invalidating the position p
p.setNext(null)
return t
HW4 (Due on 10/26)
Maintain an ordered keyword list.
¡ A keyword is a tuple of [String name, Integer count, Double
weight]
¡ Keep the list in order by its count while adding or deleting
elements
¡ For the list structure, you can
¡ Use java.util.ArrayList, or
¡ java.util.LinkedList, or
¡ Develop it by yourself
Yu, 5 Fang, 3
operations description
deleteIndex(int i) Delete the ith keyword in the list
deleteCount(int c) Delete all keywords whose count is equal to c
deleteHas(string s) Delete all keywords whose name contains s
deleteName(string s) Delete all keywords whose name is equal to s
deleteFirst(int n) Delete the first n keywords
Delete operations
deleteCount 3
deleteName Yu
deleteHas U
deleteIndex 1
deleteFirstN 2
deleteAll
add Fang 3 1.5
add Yu 5 1.2
add NCCU 3 0.8
add UCSB 2 2.2
An input file add MIS 2 1.2
add Badminton 4 2.3
add Food 3 0.1
add Data 3 0.3
1. You need to read the sequence add Structure 4 2.1
of operations from a txt file outputScore
2. The format is firm deleteCount 3
3. Raise an exception if the input outputCount 2
does not match the format outputName Yu
deleteName Yu
outputHas a
deleteHas a
outputIndex 2
deleteIndex 4
deleteFirstN 1
outputFirstN 3
deleteAll
Coming up…
¡ We will discuss stacks and queues on Oct. 26