Single-Linked Lists.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Double-Linked Lists and Circular Lists
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Lists and the Collections Framework
Lists and the Collections Framework
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
List Interface CS java.util.List Interface and its Implementers CS340 2.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CHAPTER 2 Lists and the Collections Framework. Chapter Objectives  The List interface  Writing an array-based implementation of List  Linked list data.
LinkedList Many slides from Horstmann modified by Dr V.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Chapter Objectives  The List interface  Implement lists based on arrays  Learn about List applications CS340 1.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
List Interface and Linked List Mrs. Furman March 25, 2010.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
CHAPTER 2 Lists and the Collections Framework. Chapter Objectives  The List interface  Writing an array-based implementation of List  Linked list data.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Using the Java Collection Libraries COMP 103 # T2
The List ADT.
Lists and the Collections Framework
The List ADT Reading: Textbook Sections 3.1 – 3.5
4. Linked Lists.
Tirgul 12 Data Structures (2) 1.
Building Java Programs
CSE 373: Data Structures and Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 8
Stack and Queue APURBO DATTA.
Lists and the Collections Framework
Top Ten Words that Almost Rhyme with “Peas”
MyList<T> It’s a generic type, <T> is a type parameter
The List ADT Reading: Textbook Sections 3.1 – 3.5
Building Java Programs
Lecture 26: Advanced List Implementation
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Lists and the Collection Interface
Grouped Data Arrays, and Array Lists.
Single-Linked Lists Section 2.5.
slides created by Marty Stepp
Recursive Objects Singly Linked Lists.
Building Java Programs
Programming II (CS300) Chapter 07: Linked Lists
Lecture 7: Linked List Basics reading: 16.2
slides created by Ethan Apter and Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Single-Linked Lists

Two Choices to Implement Implement a linked data structure manually inside an enclosing ADT – e.g. your queue class has an internal node class and your enqueue / dequeue methods take care of manipulating the internal linked list. Use a pre-made LinkedList class. This is what the following slides describe how to create. Instead of #1, you actually create a private linked list object as an instance variable. It may have more methods than you need to create your queue/stack/whatever. Your methods then delegate to the methods that come with your linked list instead of you implementing those linked list operations manually.

Single-Linked Lists A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and remove methods operate in linear time—requiring a loop to shift elements A linked list can add and remove elements at a known location in constant time In a linked list, instead of an index, each element is linked to the following element

A List Node A node can contain: A link is a reference to a list node a data item one or more links A link is a reference to a list node In our structure, the node contains a data field named data of type E and a reference to the next node, named next

List Nodes for Single-Linked Lists private static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next field @param dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another node @param nodeRef The node referenced by new node private Node(E dataItem, Node<E> nodeRef) { next = nodeRef;

List Nodes for Single-Linked Lists (cont.) private static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next field @param dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another node @param nodeRef The node referenced by new node private Node(E dataItem, Node<E> nodeRef) { next = nodeRef; The keyword static indicates that the Node<E> class will not reference its outer class Static inner classes are also called nested classes

List Nodes for Single-Linked Lists (cont.) private static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next field @param dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another node @param nodeRef The node referenced by new node private Node(E dataItem, Node<E> nodeRef) { next = nodeRef; Generally, all details of the Node class should be private. This applies also to the data fields and constructors. But nested class variables are visible to the enclosing class.

Connecting Nodes value= "Dave"

Connecting Nodes (cont.) Node<String> tom = new Node<String>("Tom"); Node<String> dave = new Node<String>("Dave"); Node<String> harry = new Node<String>("Harry"); Node<String> sam = new Node<String>("Sam"); tom.next = dave; dave.next = harry; harry.next = sam;

A Single-Linked List Class Generally, we do not have individual references to each node. A SingleLinkedList object has a data field head, the list head, which references the first list node (sometimes called first or front) public class SingleLinkedList<E> { private Node<E> head = null; private int size = 0; ... }

API for SingleLinkedList These public methods help us implement the List interface in Java. We will use private helper methods (carefully) behind the scenes. Note that we may not always want to implement the List interface. (Why?)

SLList: An Example List head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String>

Implementing addFirst(E item) head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> next = data = "Ann" Node<String> The element added to the list

Write the code: addFirst(E item) private void addFirst (E item) { }

Implementing addFirst(E item) (cont.) private void addFirst (E item) { Node<E> temp = new Node<E>(item, head); head = temp; size++; } or, more simply ... head = new Node<E>(item, head); This works even if head is null

Implementing addAfter(Node<E> node, E item) head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> next = data = "Ann" Node<String> The element added to the list

Write the code: addAfter(Node<E> node, E item) private void addAfter (Node<E> node, E item) { }

Implementing addAfter(Node<E> node, E item) (cont.) private void addAfter (Node<E> node, E item) { Node<E> temp = new Node<E>(item, node.next); node.next = temp; size++; } or, more simply ... node.next = new Node<E>(item, node.next); We declare this method private since it should not be called from outside the class. Later we will see how this method is used to implement the public add methods.

Implementing removeAfter(Node<E> node) temp head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> next = data = "Ann" Node<String> The Node parameter

Implementing removeAfter(Node<E> node) (cont.) private E removeAfter (Node<E> node) { }

Implementing removeAfter(Node<E> node) (cont.) private E removeAfter (Node<E> node) { Node<E> temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; }

Implementing removeFirst() head = SLList<String> next = data = "Dave" Node<String> next = data = "Tom" Node<String> temp

Implementing removeFirst() (cont.) private E removeFirst () { }

Implementing removeFirst() (cont.) private E removeFirst () { Node<E> temp = head; if (head != null) { head = head.next; } if (temp != null) { size--; return temp.data } else { return null;

Traversing a Single-Linked List head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> Do something with nodeRef Do something with nodeRef Do something with nodeRef next = data = "Ann" null Node<String> nodeRef

Traversing a Single-Linked List (cont.) toString()can be implemented with a traversal: public String toString() { }

Traversing a Single-Linked List (cont.) toString()can be implemented with a traversal: public String toString() { Node<String> nodeRef = head; StringBuilder result = new StringBuilder(); while (nodeRef != null) { result.append(nodeRef.data); if (nodeRef.next != null) { result.append(" ==> "); } nodeRef = nodeRef.next; return result.toString();

getNode(int) In order to implement methods required by the List interface, we need an additional helper method: private Node<E> getNode(int index) { }

getNode(int) In order to implement methods required by the List interface, we need an additional helper method: private Node<E> getNode(int index) { Node<E> node = head; for (int i=0; i<index && node != null; i++) { node = node.next; } return node;

Completing the SingleLinkedList Class

Implement These Methods public E get(int index) public E set(int index, E newValue) public void add(int index, E item) public boolean add(E item) USE getNode AND OTHER HELPER METHODS!

public E get(int index) }

public E get(int index) if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<E> node = getNode(index); return node.data;

public E set(int index, E newValue) public E set (int index, E anEntry) { }

public E set(int index, E newValue) public E set (int index, E anEntry) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<E> node = getNode(index); E result = node.data; node.data = anEntry; return result;

public void add(int index, E item)

public void add(int index, E item) public void add (int index, E item) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node<E> node = getNode(index-1); addAfter(node, item);

public boolean add(E item) To add an item to the end of the list public boolean add (E item) { }

public boolean add(E item) To add an item to the end of the list public boolean add (E item) { add(size, item); return true; }