The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
List Representation - Chain Fall 2010 CSE, POSTECH.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Data Structures data object set or collection of instances integer = {0, +1, -1, +2, -2, +3, -3, …} daysOfWeek = {S,M,T,W,Th,F,Sa}
Linear Lists – Linked List Representation CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
SNU IDB Lab. Ch7. Linear Lists – Simulated Pointers © copyright 2006 SNU IDB Lab.
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.
Information and Computer Sciences University of Hawaii, Manoa
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
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.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSCS-200 Data Structure and Algorithms Lecture
Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2, …, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
Reminder: Course Policy
The Class ArrayLinearList
List Representation - Array
LinkedList Class.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
The Class arrayList General purpose implementation of linear lists.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Programming in Java Lecture 11: ArrayList
Recitation 5 CS0445 Data Structures
Lecture 7: Linked List Basics reading: 16.2
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Linked Representation
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Queues CSC212.
Stacks public interface Stack { public boolean empty();
The Class chain.
Header and Trailer Sentinels
The Class chain.
CSC 143 Java Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists
slides created by Marty Stepp
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Presentation transcript:

The Class Chain

next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements

The Class Chain /** linked implementation of LinearList */ package dataStructures; import java.util.*; // has Iterator public class Chain implements LinearList { // data members protected ChainNode firstNode; protected int size; // methods of Chain come here }

Constructors /** create a list that is empty */ public Chain(int initialCapacity) { // the default initial values of firstNode and size // are null and 0, respectively } public Chain() {this(0);}

The Method isEmpty true iff list is empty */ public boolean isEmpty() {return size == 0;}

The Method size() current number of elements in list */ public int size() {return size;}

The Method checkIndex IndexOutOfBoundsException when * index is not between 0 and size - 1 */ void checkIndex(int index) { if (index = size) throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); }

The Method get public Object get(int index) { checkIndex(index); // move to desired node ChainNode currentNode = firstNode; for (int i = 0; i < index; i++) currentNode = currentNode.next; return currentNode.element; } abcde null firstNode

The Method indexOf public int indexOf(Object theElement) { // search the chain for theElement ChainNode currentNode = firstNode; int index = 0; // index of currentNode while (currentNode != null && !currentNode.element.equals(theElement)) { // move to next node currentNode = currentNode.next; index++; }

The Method indexOf // make sure we found matching element if (currentNode == null) return -1; else return index; }

Removing An Element remove(0) firstNode = firstNode.next; abcde null firstNode

Remove An Element public Object remove(int index) { checkIndex(index); Object removedElement; if (index == 0) // remove first node { removedElement = firstNode.element; firstNode = firstNode.next; }

remove(2) Find beforeNode and change its pointer. beforeNode.next = beforeNode.next.next; beforeNode abcde null firstNode

Remove An Element else { // use q to get to predecessor of desired node ChainNode q = firstNode; for (int i = 0; i < index - 1; i++) q = q.next; removedElement = q.next.element; q.next = q.next.next; // remove desired node } size--; return removedElement; }

One-Step add(0,’f’) abcde null firstNode f newNode firstNode = new ChainNode(‘f’, firstNode);

Add An Element public void add(int index, Object theElement) { if (index size) // invalid list position throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); if (index == 0) // insert at front firstNode = new ChainNode(theElement, firstNode);

Two-Step add(3,’f’) beforeNode = firstNode.next.next; beforeNode.next = new ChainNode(‘f’, beforeNode.next); abcde null firstNode f newNode beforeNode c

Adding An Element else { // find predecessor of new element ChainNode p = firstNode; for (int i = 0; i < index - 1; i++) p = p.next; // insert after p p.next = new ChainNode(theElement, p.next); } size++; }

Performance 40,000 operations of each type

Performance 40,000 operations of each type Operation FastArrayLinearList Chain get 5.6ms 157sec best-case adds 31.2ms 304ms average adds 5.8sec 115sec worst-case adds 11.8sec 157sec best-case removes 8.6ms 13.2ms average removes 5.8sec 149sec worst-case removes 11.7sec 157sec

Performance Indexed AVL Tree (IAVL)

Performance Indexed AVL Tree (IAVL) Operation FastArrayLinearList Chain IAVL get 5.6ms 157sec 63ms best-case adds 31.2ms 304ms 253ms average adds 5.8sec 115sec 392ms worst-case adds 11.8sec 157sec 544ms best-case removes 8.6ms 13.2ms 1.3sec average removes 5.8sec 149sec 1.5sec worst-case removes 11.7sec 157sec 1.6sec

Chain With Header Node abcde null headerNode

Empty Chain With Header Node headerNode null

Circular List abcde firstNode

Doubly Linked List abcde null firstNode null lastNode

Doubly Linked Circular List abcde firstNode

Doubly Linked Circular List With Header Node abce headerNode d

Empty Doubly Linked Circular List With Header Node headerNode

java.util.LinkedList  Linked implementation of a linear list.  Doubly linked circular list with header node.  Has all methods of LinearList plus many more.