Chapter 19

An Introduction to Data Structures


Chapter Goals

Linked List

Inserting an Element into a Linked List

Inserting in Linked List

Java's LinkedList class

ListIterator

A List Iterator

List Iterator

Conceptual View of the ListIterator

List Iterator

List Iterator

List Iterator

List Iterator

List Iterator

List Iterator

List Iterator

Adding and Removing from a LinkedList

Adding and Removing from a LinkedList

Adding and Removing from a LinkedList

File ListTest.java

File ListTest.java

Implementing Linked Lists

Implementing Linked Lists

Implementing Linked Lists

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

Adding a New First Element

Adding a New First Element

Adding a Link to the Head of a Linked List

Adding a Link to the Head of a Linked List

Removing the First Element

Removing the First Element

Removing the First Link from a Linked List

Removing the First Link from a Linked List

LinkedListIterator

LinkedListIterator

LinkListIterator's next Method

LinkListIterator's next Method

private class LinkedListIterator implements ListIterator
{  . . .
   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;
   }
   . . .
}

LinkListIterator's hasNext Method

LinkListIterator's hasNext Method

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

LinkListIterator's remove Method

LinkListIterator's remove Method

private class LinkedListIterator implements ListIterator
   {  . . .
      public void remove()
      {
         if (position == first)
         {
            removeFirst();
            position = first;
         }
         else
         {
            if (previous == null)
               throw new IllegalStateException();
            previous.next = position.next;
            position = previous;
         }
         previous = null;
      }
      . . .
   }

Removing a Link From the Middle of a Linked List

Removing a Link From the Middle of a Linked List

LinkListIterator's set Method

LinkListIterator's add Method

LinkListIterator's add Method

   private class LinkedListIterator implements ListIterator
   {  . . .
      public void add(Object obj)
      {
         if (position == null)
         {
            addFirst(obj);
            position = first;
         }
         else
         {
            Link newLink = new Link();
            newLink.data = obj;
            newLink.next = position.next;
            position.next = newLink;
            position = newLink;
         }
         previous = null;
      }
      . . .
   }

Adding a Link to the Middle of a Linked List

Adding a Link to the Middle of a Linked List

File LinkedList.java

File ListIterator.java

Abstract Data Types

Abstract Data Types

An Abstract View of a Linked List

An Abstract View of a Linked List

A Concrete View of a Linked List

A Concrete View of a Linked List

An Abstract View of an Array List

Abstract View of an Array List

A Concrete View of an Array List

Concrete View of an Array List

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 ListIteratior listIterator() {. . . }
   . . .
}

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

Efficiency of Linked List

Efficiency of Linked List

Efficiency of Array List

Efficiency of Operations for Arrays and List

Efficiency of Operations for Arrays and List

Abstract Data Type Stack

A Stack of Books

A Stack of Books

Stack of Books

Abstract Data Type Stack

Abstract Data Type Stack

Abstract Data Type Queue

A Queue

A Queue

Queue

Abstract Data Type Queue

A Queue Implementation

public class Queue
{
   /**
      Constructs an empty queue
   */
   public Queue()
   {
      list = new LinkedList();
   }

   /**
      Adds an item to the tail of the queue
      @param x the item to add
   */
   public void add(Object x)
   {
      list.addLast(x);
   }

   /**
      Removes an item from the head of the queue
      @return the removed item
   */
   public Object remove()
   {
      return list.removeFirst();
   }
   /**
      Gets the number of items in the queue
      @return the size
   */
   public int size()
   {
      return list.size()
   }
   private LinkedList list;
}