List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
List Implementations That Use Arrays
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Linked Lists Dr. Tim Margush University of Akron © 2009.
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.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 14.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Chapter 5 Linked Lists II
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
List Interface and Linked List Mrs. Furman March 25, 2010.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
CSCS-200 Data Structure and Algorithms Lecture
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Linked Lists and Generics Written by J.J. Shepherd.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
1 CS162: Introduction to Computer Science II Abstract Data Types.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
An Introduction to Sorting
Priority Queue.
A Bag Implementation that Links Data
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations Chapter 9.
A List Implementation That Links Data
Queues CSC212.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Lecture 6: Linked Data Structures
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queues.
A List Implementation that Links Data
Presentation transcript:

List Implementations That Link Data Chapter 6

2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of List Adding at Given Position Method remove Method replace Method getEntry Method contains Remaining methods Using a Class Node with Set and Get Methods Tail References Revision of List Pros and Cons of Using Chain Java Class Library: LinkedList

3 Linked Data Consider the analogy of desks in a classroom Placed in classroom as needed Each desk has a unique id, the “address” The desks are linked by keeping the address of another chair We have a chain of chairs

4 Linked Data A chain of 5 desks.

5 Forming a Chain First desk placed in room Blank desk top, no links Address of the first chair given to teacher One desk in the room.

6 Forming a Chain Second student arrives, takes a desk Address of first desk placed on new desk Instructor “remembers” address of new desk Two linked desks

7 Forming a Chain Third desk arrives New desk gets address of second desk Instructor remembers address of new desk Three linked desks, newest desk first.

8 Forming Another Chain This time the first student is always at the beginning of the chain Instructor only remembers first address The address of a new desk is placed on the previous desk (at end of chain) End of chain found by following links Newest desk does not have a pointer address to any other desk

9 Forming Another Chain Two linked desks, newest desk last.

10 Forming Another Chain Three linked desks, newest desk last.

11 Forming Another Chain Five linked desks, newest desk last.

12 Adding New Desk Consider the requirement to organize the chain alphabetically New arrivals are placed somewhere in the chain, not necessarily at the end Possibilities for placement of a new desk Case 1: Before all current desks Case 2: Between two existing desks Case 3: After all current desks

13 Adding New Desk: Case 1 Chain of desks prior to adding a new desk to beginning of the chain

14 Adding New Desk: Case 1 Addition of a new desk to beginning of a chain of desks

15 Adding New Desk: Case 2 Two consecutive desks within a chain prior to adding new desk between

16 Adding New Desk: Case 2 Addition of a new desk between two other desks.

17 Removing Desk Removing an item from a chain Possible cases Case 1: Desk to be removed is first in the chain Case 2: Desk to be removed is between two current desks Case 3: Desk to be removed is last in the chain

18 Removing Desk: Case 1 A chain of desks just prior to removing first desk.

19 Removing Desk: Case 1 A chain of desks just after removing first desk.

20 Removing Desk: Case 2 A chain of desks just prior to removing a desk between two other desks.

21 Removing Desk: Case 2 A chain of desks just after removing a desk between two other desks.

22 Removing Desk: Case 3 Before and after removing last desk from a chain.

23 The Class Node Nodes are objects that are linked together to form a data structure We will use nodes with two data fields A reference to an entry in the list (the person sitting at the desk) A reference to another node (the address on the paper on the desk)

24 The Class Node Two linked nodes with (a) primitive data; (b) class/object data private class Node { private T data; //data value in that node entry private Node next; //link to next node … }

25 Inner Class Node private class Node { private T data; //data value in that node entry private Node next; //link to next node private Node (T dataPortion) { data = dataPortion; next = null; } //end constructor private Node( T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } // end constructor }

26 A Linked Implementation of the ADT List Use a chain of nodes Remember the address of the first node in the chain Record a reference to the first node The “head reference” The implementation contains the class Node as an inner class

27 A Linked Implementation of the ADT List public class LList implements ListInterface { private Node firstNode; //head reference to fist node private Node lastNode; // tail reference to last node private int length; //number of entries in list public LList() { clear(); } //end constructor public final void clear() { firstNode = null; lastNode = null; length = 0; } // end clear // implementation of the public methods add, remove, replace, getEntry, contains, getLength, isEmpty, isFull, and display… }

28 Adding to the End of An empty List (a) An empty list and a new node; (b) after adding a new node to a list that was empty

29 Adding to the End of An Non-empty List A chain of nodes (a) just prior to adding a node at the end; (b) just after adding a node at the end.

30 Adding to the End of the List public boolean add(T newEntry) { Node newNode = new Node(newEntry); if (isEmpty()) firstNode = newNode; else lastNode.next = newNode; lastNode = newNode; length++; return true; }

31 Adding to the Beginning of the List A chain of nodes (a) prior to adding a node at the beginning; (b) after adding a node at the beginning.

32 Adding at a Given Position Within the List A chain of nodes (a) prior to adding node between adjacent nodes; (b) after adding node between adjacent nodes

33 Adding at a Given Position Within the List The following Java Statements implement these steps: Node newNode = new Node(newEntry); Node nodeBefore = getNodeAt(newPosition -1); Node nodeAfter = nodeBefore.next; newNode.next = nodeAfter; nodeBefore.next = newNode;

34 Removing the first node A chain of nodes (a) prior to removing first node; (b) after removing the first node Need to see if there is only one node, if yes, update the lastnode.

35 Removing other than the beginning of the list A chain of nodes (a) prior to removing interior node; (b) after removing interior node Need to see if nodeToRemove is at the end, if yes, update the lastnode.

36 The Method remove: first node firstNode = firstNode.next; The Method remove: other than the beginning Node nodeBefore = getNodeAt(givenPosition -1); Node nodeToRemove = nodeBefore.next; Node nodeAfter = nodeToRemove.next; nodeBefore.next = nodeAfter; nodeToRemove = null;

37 Using Class Node that Has Set and Get Methods Class Node is an inner class The class LList can access private data fields directly Stylistically better to use the Set and Get methods of the class Node Thus better to use statements such as: currentNode.getData(); or desiredNode.setData(newEntry);

38 Using Class Node that Has Set and Get Methods private T getData() { return data; } private void setData(T newData) { data = newData; } private Node getNextNode() { return next; }

39 Tail References Consider a set of data where we repeatedly add data to the end of the list Each time the getNodeAt method must traverse the whole list This is inefficient Solution: maintain a pointer that always keeps track of the end of the chain The tail reference

40 Tail References A linked chain with a head and tail reference.

41 Tail References When adding to an empty list Both head and tail references must point to the new solitary node When adding to a non empty list No more need to traverse to the end of the list lastNode points to it Adjust lastNode.next to new node and lastNode to new node

42 Tail References Adding a node to the end of a nonempty chain that has a tail reference

43 Pros and Cons of a Chain for an ADT List The chain (list) can grow as large as necessary Can add and remove nodes without shifting existing entries But … Must traverse a chain to determine where to make addition/deletion Retrieving an entry requires traversal As opposed to direct access in an array

44 Java Class Library: The Class LinkedList The standard java package java.util contains the class LinkedList This class implements the interface List Contains additional methods addFirst() addLast() removeFirst() removeLast() getFirst() getLast()