Issues in ATM Network Control - Washington University in

Download Report

Transcript Issues in ATM Network Control - Washington University in

CSE 131 Computer Science 1
Module 9: Linked Lists



Using references to link objects
Basic operations on linked lists
Implementing a linked list of integers
Reference in Java
A must read: http://stackoverflow.com/questions/40480/is-java-pass-by-reference
Study all the examples there!
‹#›
Storing a sequence of records
 Oftentimes,
we need to store a sequence of data
records. E.g.:
» The list of students: ID, names, grades, etc.
» All the emails, sorted by arrival time
» Finishing times of marathon runners
» Bank transactions
‹#›
Storing a sequence of records
 Need
to support these operations
» get the i-th record
» delete the i-th record,
» check if a record exists (get the index, if any)
» insert a record to the i-th place
 Using
an array to store such data records.
» Pros and cons?
» Fast random access (get the i-th record)
» Slow insert/delete
» Need to know the maximum size beforehand
• Overflow risk vs. waste of storage
‹#›
A new data structure:
Linked Lists
 List
of integers: [ 2 3 5 2 16 4 ]
2
 Key
3
5
2
16
4
differences to array?
» Array: random access (directly goes to i-th record)
» List: sequential access (follows the references)
‹#›
Inserting and Deleting
 Inserting
a record
2
3
5
2
16
4
2
16
4
7
 Removing
2
a record
3
5
‹#›
Linked Lists
 List
of integers: [ 2 3 5 2 16 4 ]
2

3
5
2
16
4
public class ListItem{
public int number;
public ListItem next;
public ListItem(int value, ListItem p){
number = value; next = p;
}
}
 Building
lists with the constructor
p
q
3
7
» ListItem p = new ListItem(3,null);
r
» ListItem q = new ListItem(7,p);
2
» ListItem r = new ListItem(2,new ListItem(5,null));
» What happens when q=p;? when ListItem d = r.next; ?
5
‹#›
toString for the ListItem class

public class ListItem{
public int number;
public ListItem next;
public ListItem(int value, ListItem p){
number = value; next = p;
}
public String toString() {
if (next == null)
return number + “ “;
else
return number + “ “ + next.toString();
}
}
‹#›
Traversing a List
Pass over list by advancing object reference
 Task: get the sum of all ListItem values reachable from a
ListItem p

p
2
3
5
2
16
// recursive traversal
int sum(ListItem p) {
if (p == null)
return 0;
else
return p.number + sum(p.next);
}
4
// iterative traversal
int sum(ListItem p) {
int result = 0;
while (p != null) {
result += p.number;
p = p.next;
}
return result;
}
‹#›
Implementing a LinkedIntList Class
 List
of integer values with the following operations
» addFirst(int x) – add the value x at the front of the list
• insert new ListItem before current head
» addLast(int x) – add the value x at the end of the list
• add new ListItem after last item
» indexOf(int x) – return the index of first item with value x
• scan the list for first match
» remove(int x) – delete the first item with value x
• scan to the first item and re-direct previous item’s next pointer
» size() – return the number of elements in the list
• uses a stored value
 Two
instance variables
» ListItem head – reference to first ListItem (or null)
» int size – number of items in the list
‹#›
Adding Elements
// Create new list item and insert before current first element
void addFirst(int x) {
head = new ListItem(x, head);
size++;
}
// Create new list item and insert it at the end of the list
void addLast(int x) {
if (head == null) {
addFirst(x);
} else {
// scan to end of list, then insert item
ListItem p;
for (p = head; p.next != null; p = p.next) {}
p.next = new ListItem(x,null);
size++;
}
‹#›
Finding and Removing Items in a List
// Return index of first item with value x (or -1 if none)
int indexOf(int x) {
int i = 0;
for (ListItem p = head; p != null; p = p.next) {
if (p.number == x) return i;
i++;
}
return -1;
}
// Remove first item with given value
void remove(int x) {
if (head == null) return;
if (head.number == x) { head = head.next; size--; return; }
for (ListItem p = head; p.next != null; p = p.next) {
if (p.next.number == x) {
p.next = p.next.next; size--; return;
}
} }
‹#›
Doubly Linked Lists
 Lists
with forward and reverse links
head
2
3
5
2
16
4
tail
size=6
» public class ListItem{
public int number;
public ListItem next;
public ListItem prev;
}
» makes it easy to traverse a list in reverse (by following p.prev)
» makes it easier to delete arbitrary elements (e.g. delete the
previous item)
‹#›
Doubly Linked Lists and Sentinels
 Lists
are sometimes implemented with sentinel nodes at
the beginning and end
» makes implementation more symmetric
» reduces number of special cases in methods
» list object includes references to both head and tail nodes
» explore it in your studio tomorrow 
head
-
2
3
5
2
16
4
-
tail
size=6
‹#›