M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.

Slides:



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

Linked Lists Geletaw S..
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.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
Double-Linked Lists and Circular Lists
Linked Lists.
Linked Lists.
Linked Lists CENG 213 Data Structures.
Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
1 Recitation 9. Practice with linked lists Introduction. This recitation will go over the linked list implementation given on the back of this sheet and.
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Chapter 17 Linked List.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
C++ Classes and Data Structures Jeffrey S. Childs
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
Data Structures Using Java1 Chapter 4 Linked Lists.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked Lists. 2 Anatomy of a linked list A linked list consists of: A sequence of nodes abcd  Each node contains a value  and a link (pointer or reference)
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Introduction to Data Structures and Algorithms
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Anatomy of a linked list
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Linked Lists.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
CMSC 341.
CMSC 341.
CMSC 341.
Header and Trailer Sentinels
Linked Lists.
CSC 143 Java Linked Lists.
CMSC 341 List 2.
TCSS 143, Autumn 2004 Lecture Notes
Linked Lists.
Linked Lists Chapter 5 (continued)
Presentation transcript:

M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1

Implementing a Linked List So what would a Linked List implementation look like? What happens if we want to –Delete the first item? –Insert an item before the first item? class MyLinkedList { // Field ListNode first; // Methods void insert(DataType x, ???); void delete(DataType x, ???); void append(DataType x);... }

Header Nodes Deletion of first item and insertion of new first item are special cases. Can be avoided by using header node; –contains no data, but serves to ensure that first "real" node in linked has a predecessor. –To go to the first element, set current to header.next; –List is empty if header.next == null; Searching routines will skip header. A1A2A3 header

Linked Lists: List Implementation (partial) public class LinkedList implements List { ListNode header; // Constructor public LinkedList() { header = new ListNode (null); } // Test if the list is logically empty. public boolean isEmpty( ) { return header.next == null; } // Make the list logically empty public void makeEmpty( ) { header.next = null; } }

Representing the “current” position How do we specify where an operation should occur? –Index position (int?) –ListNode // Methods void insert(DataType x, ???); void delete(DataType x, ???); void insert(DataType x, int current); void delete(DataType x, int current); void insert(DataType x, ListNode current); void delete(DataType x, ListNode current); a x current

List Iterator Class Maintains a notion of the current position The List class provides methods that do not depend on any position (such as isEmpty, and makeEmpty ). A List iterator ( ListItr ) provides other methods such which act on the current position stored in the iterator: –next() / advance() –hasNext() / isValid() –retrieve()

Linked List Iterator: Implementation public class ListItr { ListNode current; // Current position ListItr(ListNode node) { } public boolean hasNext() { } public void next() { } public DataType retrieve() { } }

Example Usage A method that computes the number of elements in any list: public static int listSize (List theList) { int size = 0; ListItr itr; for(itr=theList.first();itr.hasNext();itr.next()) size++; return size; }

Linked Lists: Implementation public class List { // Header node private ListNode header; // Check if list is empty boolean isEmpty() {???} // Make the list empty void makeEmpty () {???} // Cursor to header node public ListItr zeroth() {???} // Cursor to first node public ListItr first() {???} // Cursor to (first) node containing x public ListItr find(T x) {???} // Cursor to node before node containing x public ListItr findPrevious(T x) {???} // Insert x after current cursor position public void insert(T x, ListItr current) {???} // Remove (first) node containing x public void remove(T x) {???} }

Print all elements of Linked List Method 1: Without Iterator, Simple Looping public class LinkedList { public void print() { // step through list, outputting each item ListNode p = header.next; while (p != null) { System.out.println (p.data); p = p.next; }

Print all elements of Linked List Method 4: Using Iterator class LinkedList { public void print() { ListItr > itr = first(); while(itr.hasNext()) { itr.next(); System.out.println(itr.retrieve()); }

Doubly-linked lists: Each list node stores both the previous and next nodes in the list. Useful for traversing linked lists in both directions. Circular-linked lists: Last node's next references the first node. Works with or without headers. Other Linked Lists A headtail prev next A B C first prev next

Doubly-linked lists: Wrong InsertNext newNode = new DoublyLinkedListNode ( x ); 1newNode.prev = current; 2 newNode.prev.next = newNode; … x b a 1 2

Doubly-linked lists: insertNext 1 newNode = new DoublyLinkedListNode (x); 2 newNode.prev = current; 3 newNode.next = current.next; 4 newNode.prev.next = newNode; 5 newNode.next.prev = newNode; 6 current = newNode; A B current X prev next newNode

Doubly-linked lists: DeleteCurrent 1.current.prev.next = current.next; 2.current.next.prev = current.prev; 3.current = current.prev; x b a current 1 2 3

16 DLLs compared to SLLs Advantages: –Can be traversed in either direction (may be essential for some programs) –Some operations, such as deletion and inserting before a node, become easier Disadvantages: –Requires more space –List manipulations are slower (because more links must be changed) –Greater chance of having bugs (because more links must be manipulated)