Professional Documents
Culture Documents
Linked Lists: CSC 172 SPRING 2002
Linked Lists: CSC 172 SPRING 2002
CSC 172
SPRING 2002
LECTURE 3
Data Structures?
We make
Abstract
Dictionary, Stack,
Data
Model organization
List,
Data
Queue
Tree, Graph
Structure implementation
Array, Linked
Grocery
list
Laundry list
(a1,a2,,an)
Formally, we
Empty, or
An
Lists
Empty
(no elements)
An element (head), followed by a list (tail)
Head
of a list is
Arrays
An array is a collection of data items of the same type
Every element of the collection can be accessed
separately.
Object [] data = new Object[10];
Array Implementation
public class myInfo implements Dictionary{
private int defaultCapacity = 100, length = 0;
private Object[] datum;
public myInfo() {
datum = new Object[defaultCapacity];
}
public myInfo(int initCapacity) {
datum = new Object[initCapacity];
}
Implementation
public class myInfo implements Dictionary{
private int defaultCapacity = 100, length = 0;
private Object[] datum;
public myInfo() {
datum = new Object[defaultCapacity];
}
public myInfo(int initCapacity) {
datum = new Object[initCapacity];
}
Insert
Is this ok?
public void insert (Object o) {
datum[length++] = o;
}
What is the run time?
What if I have more than 100 elements?
Expand
private void expand(){
Object[] tempData =
new Object[datum.length * 2];
for (int j = 0 ; j<length;j++)
tempData[j] = datum[j];
datum = tempData;
}
So, what is the runtime of this
Insert
Better?
public void insert (Object o) {
if (length >= (datum.length 1)) expand();
datum[length++] = o;
}
This is what the Java class Vector gets you
What is the (worst case) run time, now?
next
// the data
// the link
Linked List
A linked
The
data
next
data
Next
data
Next
null
Linked-List Implementation
public class myInfo implements Dictionary{
private int length = 0;
private Node head = null;
Lookup: Array
public boolean lookup(Object o){
for (int j = 0 ; j < length;j++)
if (datum[j].equals(o)) return true;
return false;
}
Run time?
Delete: Array
We have
to look it up O(n)
Deletion is setting it to null
But, we have to shift all the remaining elements.
Delete: Array
public void delete(Object o){
for (int j = 0 ; j < length;j++)
if (datum[j].equals(o)) {
for (int k = j; k<(length-1);k++)
datum[k] = datum[k+1];
return;
};
}
Run time?
LINKED-LIST
Insert
N (in case of
expansion)
Lookup
Delete
N (lookup + shift)
N (for lookup)
Workshop sing-up
. The coefficient for the workshop differential is
positive and significant. On average, each
additional workshop attended in CSC 172
increases the final grade point by 0.19. In
substantive terms, this means that a student who
attends five additional workshops in CSC 172
should expect his/her grade to increase by almost
one full letter grade.