List Representation - Chain Fall 2010 CSE, POSTECH.

Slides:



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

Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
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.
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.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
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.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
(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.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
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.
slides adapted from Marty Stepp and Hélène Martin
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.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Reminder: Course Policy
Stacks 황승원 Fall 2010 CSE, POSTECH.
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.
slides created by Marty Stepp and Hélène Martin
Queues CSC212.
Stacks public interface Stack { public boolean empty();
The Class chain.
The Class chain.
CSC 143 Java Linked Lists.
Programming II (CS300) Chapter 07: Linked Lists
Lecture 7: Linked List Basics reading: 16.2
TCSS 143, Autumn 2004 Lecture Notes
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:

List Representation - Chain Fall 2010 CSE, POSTECH

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

3 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 }

4 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);}

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

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

7 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); }

8 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

9 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++; }

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

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

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

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

14 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; }

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

16 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);

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

18 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++; } abcde firstNode f new p c

19 Discussion add(0) add(0) remove(size-1) remove(size-1)? What happens to “removedNode”?

20 Doubly Linked List abcde null firstNode null lastNode

21 Doubly Linked Circular List abcde firstNode

22 java.util.LinkedList  Linked implementation of a linear list.  Doubly linked circular list with header node.

23 Coming Up Next READING: Ch 6 NEXT: Linked List – Simulated Pointers (Ch 7)