L8,9 List Stack Queue2
L8,9 List Stack Queue2
U Kang
Seoul National University
U Kang 1
In This Lecture
Learn the motivation and main idea of doubly
linked list
U Kang 2
Limitation of Linked List
U Kang 3
Limitation of Linked List
U Kang 4
Doubly Linked Lists
class DLink<E> {
private E element;
private DLink<E> next;
private DLink<E> prev;
DLink(E it, DLink<E> p, DLink<E> n)
{ element = it; prev = p; next = n; }
DLink(DLink<E> p, DLink<E> n)
{ prev = p; next = n; }
DLink<E> next() { return next; }
DLink<E> setNext(DLink<E> nextval)
{ return next = nextval; }
DLink<E> prev() { return prev; }
DLink<E> setPrev(DLink<E> prevval)
{ return prev = prevval; }
E element() { return element; }
E setElement(E it) { return element = it; }
} U Kang 5
Doubly Linked Lists
U Kang 6
Doubly Linked Insert
U Kang 7
Doubly Linked Insert
public void 4 insert(E it) {
curr.setNext( 3 2
1 new DLink<E>(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++; 5
}
U Kang 8
Doubly Linked Remove
U Kang 9
Doubly Linked Remove
public E remove() {
if (curr.next() == tail) return null;
E it = curr.next().element();
curr.next().next().setPrev(curr); 1
curr.setNext(curr.next().next()); 2
cnt--;
return it;
}
U Kang 10
Effect of Fixed Head & Tail
Issues:
Which end is the top?
Where does “top” point to?
What are the costs of the operations?
PUSH, POP, and TOP
U Kang 15
Linked Stack
class LStack<E> implements Stack<E> {
private Link<E> top;
private int size;
}
U Kang 16
Queues
FIFO: First in, First Out
U Kang 17
Queue Implementation (1)
U Kang 18
Queue Implementation (2)
“Circular” Queue
U Kang 19
Dictionary
Required concepts:
Search key: describe what we are looking for
E.g., for records consisting of (country name, population, # of
visitors), a search key is ‘country name’
Key comparison
Equality: while doing sequential search
U Kang 20
Records and Keys
U Kang 21
Dictionary ADT
public interface Dictionary<Key, E> {
public void clear();
public void insert(Key k, E e);
public E remove(Key k); // Null if none
public E removeAny(); // Null if none
public E find(Key k); // Null if none
public int size();
};
U Kang 22
Payroll Class
// Simple payroll entry: ID, name, address
class Payroll {
private Integer ID;
private String name;
private String address;
Payroll(int inID, String inname, String inaddr)
{
ID = inID;
name = inname;
address = inaddr;
}
public Integer getID() { return ID; }
public String getname() { return name; }
public String getaddr() { return address; }
}
U Kang 23
Using Dictionary
// IDdict organizes Payroll records by ID
Dictionary<Integer, Payroll> IDdict =
new UALdictionary<Integer, Payroll>();
// namedict organizes Payroll records by name
Dictionary<String, Payroll> namedict =
new UALdictionary<String, Payroll>();
Payroll foo1 = new Payroll(5, "Joe", "Anytown");
Payroll foo2 = new Payroll(10, "John", "Mytown");
IDdict.insert(foo1.getID(), foo1);
IDdict.insert(foo2.getID(), foo2);
namedict.insert(foo1.getname(), foo1);
namedict.insert(foo2.getname(), foo2);
Payroll findfoo1 = IDdict.find(5);
Payroll findfoo2 = namedict.find("John");
U Kang 24
Unsorted List Dictionary
class UALdictionary<Key, E>
implements Dictionary<Key, E> {
private static final int defaultSize = 10;
private AList<KVpair<Key, E>> list;
// Constructors
UALdictionary() { this(defaultSize); }
UALdictionary(int sz)
{ list = new AList<KVpair<Key, E>>(sz); }
public void clear() { list.clear(); }
/** Insert an element: append to list */
public void insert(Key k, E e) {
KVpair<Key,E> temp = new KVpair<Key,E>(k, e);
list.append(temp);
} U Kang 25
Sorted vs. Unsorted Array List
Dictionaries
If the list is sorted
Could use binary search to speed up search
Would need to insert in order, slowing insert
Which is better?
If lots of searches, sorted list is good
If insertions are much more frequent, then sorting has
no benefit.
U Kang 26
What You Need to Know
Motivation and main idea of doubly linked list
Doubly linked list vs. singly linked list
U Kang 27
Questions?
U Kang 28