Docsity

Generic Linked List, Variations - Lecture Slides | CS 261, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS 261 – Data Structures
Linked Lists
Example Application
Data (Student - Course):
Chris Smith - Art 250
Amy Grant - Bus 321
Amy Grant - Art 250
Chris Smith - CS 311
Randy Jones - CS 261
Randy Jones - Art 250
We want two lists:
1. List of courses for each student
2. List of students in each course
Properties of the application:
1. Add to each list as you read the data
2. Order is not important
3. Want to print the lists
1. Students:
Chris Smith
Art 250
CS 311
Amy Grant
Bus 321
Art 250
Randy Jones
Bus 321
Art 250
2. Courses:
Art 250
Chris Smith
Amy Grant
Randy Jones
…
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Generic Linked List, Variations - Lecture Slides | CS 261 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 261 – Data Structures

Linked Lists

Example Application

Data (Student - Course):

Chris Smith - Art 250 Amy Grant - Bus 321 Amy Grant - Art 250 Chris Smith - CS 311 Randy Jones - CS 261 Randy Jones - Art 250

We want two lists:

**1. List of courses for each student

  1. List of students in each course**
Properties of the application:

**1. Add to each list as you read the data

  1. Order is not important
  2. Want to print the lists**

1. Students:

Chris Smith

Art 250 CS 311

Amy Grant

Bus 321 Art 250

Randy Jones

Bus 321 Art 250

2. Courses:

Art 250

Chris Smith Amy Grant Randy Jones

Linked Lists

What is it?

  • Like a chain, each link is tied to the next

Traditional implementation:

public class Student { private String name; private int ssn; private Student link; // Link to next student in the list. }

Problem: the data type is tied to the link

  • Mixes specific object data with list implementation
  • Not general

data link

data link

name ssn (^) link

Generic Linked List

public class Link { public Object val; public Link next; }

  • Advantages:
    • Object data (i.e., elements) are separated from list implementation (i.e., container)
    • General and reusable
  • Note: this definition is recursive – the last link must have some
way of indicating termination (e.g., a null reference)

val next

(e.g., Pizza, Student, Animal, etc.)

Link val next

Link

Object Object

Object-Oriented Linked Lists

  • A basic linked list (e.g., used in LISP) has:
    • Single links
    • No header
    • Reference to first (head) link only
    • No sentinel (or terminating) link.
  • We will look at a list that has:
    • Double links
    • A header
    • Reference to first (head) link and last (terminating) link
    • A sentinel (terminating or tail) link.

val links

val links

list

links

sentinel

Header

DoubleLink

(in src/jds/util)

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

Called on
this node

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.)

  • The DoubleLink class represents a single link
  • It will be reused in other linked list abstractions
  • So far, we have shown:
3 Double links
  • A header
  • Reference to first link only
  • A sentinel link

Linked List: Embedded (Inner) Link Class

Need to properly insert and delete prior to the fist link:

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--; } }

Use Link as the sentinel (tail) and when inserting or removing

Linked List: Deque Interface

Now we can implement the 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; }

Linked List: Deque Interface (cont.)

public synchronized void removeFirst() { head.remove(); } public synchronized void removeLast() { if (isEmpty()) throw new NoSuchElementException; tail.prev.remove(); }

  • Additions and removals are handled by the Link class

methods to ensure the head link is updated

  • Algorithmic complexity of the Deque interface methods:
addFirst, getFirst, removeFirst
addLast , getLast , removeLast

} O(1)

Linked List: Enumeration

  • The ListEnumeration class is an “inner” class
  • As such, it can access the protected fields head and tail

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; } } }