Chapter 15

An Introduction to Data Structures


Chapter Goals

Using Linked Lists

Inserting an Element into a Linked List

Java's LinkedList class

List Iterator

A List Iterator

A Conceptual View of the List Iterator

List Iterator

List Iterator

List Iterator

List Iterator

Adding and Removing from a LinkedList

Adding and Removing from a LinkedList

Sample Program

File ListTester.java

Output

Dick
Harry
Juliet
Nina
Tom

Self Check

  1. Do linked lists take more storage space than arrays of the same size?
  2. Why don't we need iterators with arrays?

Answers

  1. Yes, for two reasons. You need to store the node references, and each node is a separate object. (There is a fixed overhead to store each object in the virtual machine.)
  2. An integer index can be used to access any array location.

Implementing Linked Lists

Implementing Linked Lists

Implementing Linked Lists

public class LinkedList
{
. . .
private class Node
{
public Object data;
public Node next;
}

}

Implementing Linked Lists

Implementing Linked Lists

public class LinkedList
{
public LinkedList()
{
first = null;
}

public Object getFirst()
{
if (first == null)
throw new NoSuchElementException();
return first.data;
}

. . .
private Node first;
}

Adding a New First Element

Adding a New First Element

Adding a Node to the Head of a Linked List

Removing the First Element

Removing the First Element

Removing the First Node from a Linked List

Linked List Iterator

LinkedListIterator

The Linked List Iterator's next Method

The Linked List Iterator's next Method

public Object next()
{
if (!hasNext())
throw new NoSuchElementException();
previous = position; // Remember for remove

if (position == null)
position = first;
else
position = position.next;

return position.data;
}

The Linked List Iterator's hasNext Method

The Linked List Iterator's hasNext Method

private class LinkedListIterator implements ListIterator
{
. . .
public boolean hasNext()
{
if (position == null)
return first != null;
else
return position.next != null;
}
. . .
}

The Linked List Iterator's remove Method

The Linked List Iterator's remove Method

public void remove()
{
if (previous == position)
throw new IllegalStateException();
if (position == first)
{
removeFirst();
}
else
{
previous.next = position.next;
}
position = previous;
}

Removing a Node From the Middle of a Linked List

The Linked List Iterator's set Method

The Linked List Iterator's add Method

The Linked List Iterator's add Method

public void add(Object obj)
{
if (position == null)
{
addFirst(obj);
position = first;
}
else
{
Node newNode = new Node();
newNode.data = obj;
newNode.next = position.next;
position.next = newNode;
position = newNode;
}
previous = position;
}

Adding a Node to the Middle of a Linked List

File LinkedList.java

File ListIterator.java

Self Check

  1. Trace through the addFirst method when adding an element to an empty list.
  2. Conceptually, an iterator points between elements (see Figure 3). Does the position reference point to the element to the left or to the element to the right?
  3. Why does the add method have two separate cases?

Answers

  1. When the list is empty, first is null. A new Node is allocated. It's data field is set to the newly inserted object. It's next field is set to null because first is null. The first field is set to the new node. The result is a linked list of length 1.
  2. It points to the element to the left. You can see that by tracing out the first call to next. It leaves position to point to the first node.
  3. If position is null, we must be at the head of the list, and inserting an element requires updating the first reference. If we are in the middle of the list, the first reference should not be changed.

Abstract and Concrete Data Types

Abstract and Concrete Data Types


Abstract Data Types

Abstract and Concrete Array Type

Abstract and Concrete Data Types


Abstract and Concrete Data Types

Fundamental Operations on Array List

public class ArrayList
{
public Object get(int index) { . . . }
public void set(int index, Object value) { . . . }
. . .
}

Fundamental Operations on Linked List

public class LinkedList
{
public ListIterator listIterator() { . . . }
. . .
}

public interface ListIterator
{
Object next();
boolean hasNext();
void add(Object value);
void remove();
void set(Object value);
. . .
}

Abstract and Concrete Data Types

Efficiency of Operations for Arrays and Lists

Efficiency of Operations for Arrays and Lists

Operation Array List
Random access O(1) O(n)
Linear traversal step O(1) O(1)
Add/remove an element O(n) O(1)

Abstract Data Types

Self Check

  1. What is the advantage of viewing a type abstractly?
  2. How would you sketch an abstract view of a doubly linked list? A concrete view?
  3. How much slower is the binary search algorithm for a linked list compared to the linear search algorithm?

Answers

  1. You can focus on the essential characteristics of the data type without being distracted by implementation details.
  2. The abstract view would be like Figure 9, but with arrows in both directions. The concrete view would be like Figure 8, but with references to the previous node added to each node.
  3. To locate the middle element takes n / 2 steps. To locate the middle of the subinterval to the left or right takes another n / 4 steps. The next lookup takes n / 8 steps. Thus, we expect almost n steps to locate an element. At this point, you are better off just making a linear search that, on average, takes n / 2 steps.

Stacks and Queues

Stack

A Stack of Books

Queue

A Queue

Stacks and Queues: Uses in Computer Science

Abstract Data Type Stack

Abstract Data Type Queue

A Queue Implementation

Self Check

  1. Draw a sketch of the abstract queue type, similar to Figures 9 and 11.
  2. Why wouldn't you want to use a stack to manage print jobs?

Answers

  1. Stacks use a "last in, first out" discipline. If you are the first one to submit a print job and lots of people add print jobs before the printer has a chance to deal with your job, they get their printouts first, and you have to wait until all other jobs are completed.