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

Sample program inserts elements into a list, then iterates through the list, adding and removing elements,then prints the list

Implementing Linked Lists

Implementing Linked Lists

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

LinkListIterator's hasNext Method

LinkListIterator's hasNext Method

LinkListIterator's remove Method

LinkListIterator's remove Method

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

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

Abstract Data Type Stack

Abstract Data Type Stack

A Stack of Books

A stack can be visualized as a stack of books. You place books on top and remove from the top.

Stack of Books 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;
}

A Queue

A Queue can be visualized as a queue of people. People join the tail of the queue and wait until they reach the head.

Queue