Lecture10 11
Lecture10 11
CS062
DATA STRUCTURES AND ADVANCED PROGRAMMING
▸ Java Collections
Some slides adopted from Algorithms 4th Edition and Oracle tutorials
DOUBLY LINKED LISTS 3
item
Head/Beginning/Front/First Tail/End/Back/Last
Node
Item item;
Node next;
item
Node prev;
}
DOUBLY LINKED LISTS 5
Standard Operations
head = null
tail = null
What should happen?
n=0
dll.addFirst(“CS062”);
DOUBLY LINKED LISTS 7
addFirst(Item item):Inserts the specified item at the head of the doubly linked list
Head/Beginning/Front/First Tail/End/Back/Last
dll.addFirst(“CS062”)
CS062
n=1
dll.addFirst(“ROCKS”);
DOUBLY LINKED LISTS 8
addFirst(Item item):Inserts the specified item at the head of the doubly linked list
Head/Beginning/Front/First Tail/End/Back/Last
dll.addFirst(“ROCKS”)
ROCKS CS062
n=2
dll.addLast(“!”);
DOUBLY LINKED LISTS 9
addLast(Item item):Inserts the specified item at the tail of the doubly linked list
Head/Beginning/Front/First Tail/End/Back/Last
dll.addLast(“!”)
ROCKS CS062 !
n=3
dll.add(1,“?”);
DOUBLY LINKED LISTS 10
Head/Beginning/Front/First Tail/End/Back/Last
dll.add(1,“?”)
ROCKS ? CS062 !
n=4
dll.removeFirst();
DOUBLY LINKED LISTS 11
Head/Beginning/Front/First Tail/End/Back/Last
dll.removeFirst()
? CS062 ! n=3
dll.removeLast();
DOUBLY LINKED LISTS 12
Head/Beginning/Front/First Tail/End/Back/Last
dll.removeLast()
? CS062 n=2
dll.remove(1);
DOUBLY LINKED LISTS 13
Head/Beginning/Front/First
Tail/End/Back/Last
dll.remove(1)
?
n=1
DOUBLY LINKED LISTS 14
/**
* This nested class defines the nodes in the doubly linked list with a value
* and pointers to the previous and next node they are connected.
*/
private class Node {
Item item;
Node next;
Node prev;
}
DOUBLY LINKED LISTS 16
/**
* Returns true if the doubly linked list does not contain any item.
*
* @return true if the doubly linked list does not contain any item
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns the number of items in the doubly linked list.
*
* @return the number of items in the doubly linked list
*/
public int size() {
return n;
}
DOUBLY LINKED LISTS 17
/**
* Returns item at the specified index.
*
* @param index
* the index of the item to be returned
* @return the item at specified index
*/
public Item get(int index) {
rangeCheck(index);
if (index == 0)
return first.item;
/**
* Inserts the specified item at the head of the doubly linked list.
*
* @param item
* the item to be inserted
*/
public void addFirst(Item item) {
// Save the old node
Node oldfirst = first;
/**
* Inserts the specified item at the tail of the doubly linked list.
*
* @param item
* the item to be inserted
*/
public void addLast(Item item) {
// Save the old node
Node oldlast = last;
n++;
}
DOUBLY LINKED LISTS 20
/**
* A helper method to check if an index is in range 0<=index<n
*
* @param index
* the index to check
*/
private void rangeCheck(int index) {
if (index > n || index < 0)
throw new IndexOutOfBoundsException("Index " + index + " out of bounds");
}
DOUBLY LINKED LISTS 21
/**
* Inserts the specified item at the specified index.
*
* @param index
* the index to insert the item
* @param item
* the item to insert
*/
public void add(int index, Item item) {
rangeCheck(index);
if (index == 0) {
addFirst(item);
} else if (index == size()) {
addLast(item);
} else {
n++;
}
}
DOUBLY LINKED LISTS 22
/**
* Retrieves and removes the head of the doubly linked list.
*
* @return the head of the doubly linked list.
*/
public Item removeFirst() {
Node oldFirst = first;
// Fix pointers.
first = first.next;
// at least 1 nodes left.
if (first != null) {
first.prev = null;
} else {
last = null; // remove final node.
}
oldFirst.next = null;
n--;
return oldFirst.item;
}
DOUBLY LINKED LISTS 23
/**
* Retrieves and removes the tail of the doubly linked list.
*
* @return the tail of the doubly linked list.
*/
public Item removeLast() {
/**
* Retrieves and removes the item at the specified index.
*
* @param index
* the index of the item to be removed
* @return the item previously at the specified index
*/
public Item remove(int index) {
rangeCheck(index);
if (index == 0) {
return removeFirst();
} else if (index == size() - 1) {
return removeLast();
} else {
Node previous = null;
Node finger = first;
// search for value indexed, keep track of previous
while (index > 0) {
previous = finger;
finger = finger.next;
index--;
}
previous.next = finger.next;
finger.next.prev = previous;
n--;
// finger's value is old value, return it
return finger.item;
}
}
TODAY’S LECTURE IN A NUTSHELL 25
▸ Java Collections
JAVA COLLECTIONS 26
https://en.wikipedia.org/wiki/Java_collections_framework
JAVA COLLECTIONS 27
java.util.LinkedList;
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
TODAY’S LECTURE IN A NUTSHELL 28
▸ Java Collections
ASSIGNED READINGS AND PRACTICE PROBLEMS 29
Readings:
▸ Oracle’s guides:
▸ Collections: https://docs.oracle.com/javase/tutorial/collections/intro/index.html
▸ Textbook:
▸ Textbook Website:
Practice Problems: