





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Example Application
Chris Smith - Art 250 Amy Grant - Bus 321 Amy Grant - Art 250 Chris Smith - CS 311 Randy Jones - CS 261 Randy Jones - Art 250
**1. List of courses for each student
**1. Add to each list as you read the data
Art 250 CS 311
Bus 321 Art 250
Bus 321 Art 250
Chris Smith Amy Grant Randy Jones
Linked Lists
public class Student { private String name; private int ssn; private Student link; // Link to next student in the list. }
data link
data link
name ssn (^) link
Generic Linked List
public class Link { public Object val; public Link next; }
val next
(e.g., Pizza, Student, Animal, etc.)
Link val next
Link
Object Object
Object-Oriented Linked Lists
val links
val links
list
links
sentinel
DoubleLink
public class DoubleLink implements Serializable { public Object val; public DoubleLink next, prev; // Two links (to next and previous link). public DoubleLink(Object obj, DoubleLink nxt, DoubleLink prv) { val = obj; next = nxt; prev = prv; } public synchronized void insertLink(DoubleLink link) { link.next = this; // Need to assign 4 links. link.prev = prev; if (prev != null) prev.next = link; // May be no previous link. prev = link; } ... (^) prev this
prev link this
DoubleLink (cont.)
public DoubleLink insert(Object obj) { DoubleLink link = new DoubleLink(obj, this, prev); insertLink(link); return link; } public synchronized void remove() { // Remove this link. val = null if (next != null) next.prev = prev; // Unlink from the right. if (prev != null) prev.next = next; // Unlink from the left. next = prev = null; } }
prev this next
DoubleLink (cont.)
Linked List: Embedded (Inner) Link Class
protected class Link extends DoubleLink { // Inner class. public Link(Object obj) { super(obj, null, null);} public void insert(Object obj){ // Shadow parent class. Link link = new Link(obj); count++; insertLink(link); if (link.prev == null) head = link; // Set head if first link. } public void remove() { // Shadow parent class. if (this == tail) // Cannot remove sentinel. throw new NoSuchElementException(); if (prev == null) head = next; super.remove(); count--; } }
Linked List: Deque Interface
// Implementation of the Deque interface. public void addFirst(Object obj) { head.insert(obj); } public void addLast (Object obj) { tail.insert(obj); } public Object getFirst () { if (isEmpty()) throw new NoSuchElementException(); return head.val; } public Object getLast() { if (isEmpty()) throw new NoSuchElementException; return tail.prev.val; }
public synchronized void removeFirst() { head.remove(); } public synchronized void removeLast() { if (isEmpty()) throw new NoSuchElementException; tail.prev.remove(); }
public class LinkedList implements Deque, Bag { public Enumeration elements() { return new ListEnumeration(); } ... protected class ListEnumeration implements Enumeration { private DoubleLink cur = head; // Current link. public boolean hasMoreElements() { return cur != tail; } public Object nextElement() { Object result = cur.val; cur = cur.next; return result; } } }