4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown.

Slides:



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

Linked List. p The every usage of the term list refers to a linear collection of data item. e.g. Shopping List. p A Shopping List contains a first element,
Linked Lists Geletaw S..
Linked Lists.
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
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.
Linked Lists.
Linked Lists.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
Review Learn about linked lists
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
Linked Lists [AJ 15] 1. 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.
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Kazalci in dinamične podatkovne strukture: seznami.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays.
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)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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)
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Data Structures and Algorithms Lecture 7,8 and 9 (Linked List) Instructor: Quratulain Date: 25, 29 September and 2 October, 2009 Faculty of Computer Science,
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
7.2 Priority Queue ADTs Priority queue concepts
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Data Structures Using Java1 Chapter 4 Linked Lists.
Linked lists Singly linked lists Doubly linked lists Circular lists Self-organizing lists LinkedList and ArrayList Data Structures and Algorithms in Java,
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)
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Introduction to Data Structures and Algorithms
CS2006- Data Structures I Chapter 5 Linked Lists III.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Linked Lists Revision Based on: v/
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
LINKED LISTS.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
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.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Linked Lists, Queues, Stacks
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
C++ Programming:. Program Design Including
Linked Lists.
Anatomy of a linked list
The Tree Data Structure
Linked Lists.
Data Structures ADT List
Linked Lists.
8 List ADTs List concepts. List applications.
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown

4-2 Linked lists (1) A linked list consists of a sequence of nodes connected by links, plus a header. Each node (except the last) has a successor, and each node (except the first) has a predecessor. Each node contains a single element (object or value), plus links to its successor and/or predecessor. antbatcatantbatcat header null link node element link

4-3 Linked lists (2) The length of a linked list is the number of nodes. An empty linked list has no nodes. In a linked list:  We can manipulate the individual elements.  We can manipulate the links, thus changing the linked list’s very structure! (This is impossible in an array.)

4-4 Singly-linked lists (1) A singly-linked list (SLL) consists of a sequence of nodes, connected by links in one direction only. Each SLL node contains a single element, plus a link to the node’s successor (or a null link if the node has no successor). An SLL header contains a link to the SLL’s first node (or a null link if the SLL is empty). pigdogratcatdog

4-5 Singly-linked lists (2) Java class implementing SLL nodes: public class SLLNode { protected Object element; protected SLLNode succ; public SLLNode (Object elem, SLLNode succ) { this.element = elem; this.succ = succ; } }

4-6 Singly-linked lists (3) Java class implementing SLL headers: public class SLL { private SLLNode first; public SLL () { // Construct an empty SLL. this.first = null; } … } SLL methods (to follow)

4-7 antbatcat first Example 1: SLL traversal Instance method (in class SLL ) to traverse an SLL: public void printFirstToLast () { // Print all elements in this SLL, in first-to-last order. for (SLLNode curr = this.first; curr != null; curr = curr.succ) System.out.println(curr.element); } Animation: antbatcat first curr antbatcat first curr antbatcat first curr antbatcat first curr

4-8 Example 2: SLL manipulation (1) Instance method (in class SLL ) to delete an SLL’s first node: public void deleteFirst () { // Delete this SLL’s first node (assuming length > 0). this.first = this.first.succ; } Animation: antbatcat first antbatcat first

4-9 Example 2 (2) Instance method (in class SLL ) to delete an SLL’s second node: public void deleteSecond () { // Delete this SLL’s second node (assuming length > 1). SLLNode second = this.first.succ; this.first.succ = second.succ; } Animation: antbatcat first antbatcat first second antbatcat first second

4-10 Example 2 (3) Instance method (in class SLL ) to swap an SLL’s first and second nodes: public void swapFirstTwo () { // Swap this SLL’s 1st and 2nd nodes (assuming length > 1). SLLNode second = this.first.succ; this.first.succ = second.succ; second.succ = this.first; this.first = second; } Animation: antbatcat first antbatcat first second antbatcat first second antbatcat first second antbatcat first second

4-11 Doubly-linked lists (1) A doubly-linked list (DLL) consists of a sequence of nodes, connected by links in both directions. Each DLL node contains a single element, plus links to the node’s successor and predecessor (or null link(s)). The DLL header contains links to the DLL’s first and last nodes (or null links if the DLL is empty). pigdogratcatdog

4-12 Doubly-linked lists (2) Java class implementing DLL nodes: public class DLLNode { protected Object element; protected DLLNode pred, succ; public DLLNode (Object elem, DLLNode pred, DLLNode succ) { this.element = elem; this.pred = pred; this.succ = succ; } }

4-13 Doubly-linked lists (3) Java class implementing DLL headers: public class DLL { private DLLNode first, last; public DLL () { // Construct an empty DLL. this.first = null; this.last = null; } … } DLL methods (to follow)

4-14 antbatcat first last Example 3: DLL traversal Instance method (in class DLL ) to traverse a DLL, from last node to first: public void printLastToFirst () { // Print all elements in this DLL, in last-to-first order. for (DLLNode curr = this.last; curr != null; curr = curr.pred) System.out.println(curr.element); } Animation: curr antbatcat first last curr antbatcat first last curr antbatcat first last curr antbatcat first last

4-15 Example 4: DLL manipulation (1) Instance method (in class DLL ) to delete a DLL’s first node: public void deleteFirst () { // Delete this DLL’s first node (assuming length > 0). DLLNode second = this.first.succ; second.pred = null; this.first = second; } Animation: antbatcat first last second antbatcat first last second antbatcat first last second antbatcat first last

4-16 Example 4 (2) Instance method (in class DLL ) to delete a DLL’s last node: public void deleteLast () { // Delete this DLL’s last node (assuming length > 0). DLLNode penult = this.last.pred; penult.succ = null; this.last = penult; } Animation: antbatcat first last penult antbatcat first last penult antbatcat first last penult antbatcat first last

4-17 DLL = forward SLL + backward SLL View a DLL as a backward SLL superimposed on a forward SLL: antbatcat DLL: antbatcat Forward SLL: antbatcat Backward SLL:

4-18 Insertion Problem: Insert a new element at a given point in a linked list. Four cases to consider: 1)insertion in an empty linked list; 2)insertion before the first node of a nonempty linked list; 3)insertion after the last node of a nonempty linked list; 4)insertion between nodes of a nonempty linked list. The insertion algorithm needs links to the new node’s successor and predecessor.

4-19 SLL insertion (1) SLL insertion algorithm: To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate.

4-20 SLL insertion (2) Animation (insertion before first node): batcat first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first ins To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first ins To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first ins To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate.

4-21 SLL insertion (3) Animation (insertion after intermediate node): dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. pred dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel predins dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel predins dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel predins dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel

4-22 SLL insertion (4) Implementation as a Java method (in class SLL ): public void insert (Object elem SLLNode pred) { // Insert elem at a given point in this SLL, either after the node // pred, or before the first node if pred is null. SLLNode ins = new SLLNode(elem, null); if (pred == null) { ins.succ = this.first; this.first = ins; } else { ins.succ = pred.succ; pred.succ = ins; } }

4-23 DLL insertion (1) DLL insertion algorithm: To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate.

4-24 DLL insertion (2) Auxiliary forward SLL insertion algorithm: To insert node ins at a given point in the forward SLL headed by first: 1.If the insertion point is before the first node: 1.1.Set node ins’s successor to first. 1.2.Set first to ins. 2.If the insertion point is after the node pred: 2.1.Set node ins’s successor to node pred’s successor. 2.2.Set node pred’s successor to ins. 3.Terminate.

4-25 DLL insertion (3) Auxiliary backward SLL insertion algorithm: To insert node ins after node succ in the backward SLL headed by last: 1.If succ is null: 1.1.Set node ins’s predecessor to first. 1.2.Set last to ins. 2.If succ is not null: 2.1.Set node ins’s predecessor to node succ’s predecessor. 2.2.Set node succ’s predecessor to ins. 3.Terminate.

4-26 DLL insertion (4) Animation (insertion before the first node): To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ant batcat first last

4-27 DLL insertion (5) Animation (insertion after the last node): To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. dog batcat first last

4-28 To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. fox dog first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last DLL insertion (6) Animation (insertion between nodes): To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. fox dog eel first last

4-29 Deletion Problem: Delete a given node from a linked list. Four cases to consider: 1)deletion of a singleton node; 2)deletion of the first (but not last) node; 3)deletion of the last (but not first) node; 4)deletion of an intermediate node. The deletion algorithm needs links to the deleted node’s successor and predecessor.

4-30 SLL deletion (1) SLL deletion algorithm: To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. But there is no link from node del to its predecessor, so step 3.1 can access del’s predecessor only by following links from first!

4-31 SLL deletion (2) Animation (deleting the first node): To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first del To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first delsucc To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first delsucc To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first garbage

4-32 SLL deletion (3) Animation (deleting an intermediate (or last) node): To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first del To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first delsucc To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first delsuccpred To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first delsuccpred To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first garbage

4-33 SLL deletion (4) Analysis: Let n be the SLL’s length. Step 3.1 must visit all nodes from the first node to the deleted node’s predecessor. There are between 0 and n–1 such nodes. Average no. of nodes visited = (n – 1)/2 Time complexity is O(n).

4-34 SLL deletion (5) Implementation as a Java method (in class SLL ): public void delete (SLLNode del) { // Delete node del from this SLL. SLLNode succ = del.succ; if (del == this.first) { this.first = succ; } else { SLLNode pred = this.first; while (pred.succ != del) pred = pred.succ; pred.succ = succ; } }

4-35 DLL deletion (1) DLL deletion algorithm: To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate.

4-36 To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. antbatcat first last del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. pred antbatcat first last succ del DLL deletion (2) Animation (deleting the first (but not last) node): To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. pred antbatcat first last succ del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. pred antbatcat first last succ del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. antbatcat first last

4-37 DLL deletion (3) Animation (deleting an intermediate node): To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last predsucc To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last predsucc To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last predsucc To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. fox dogeel first last

4-38 Comparison of SLL and DLL insertion and deletion algorithms AlgorithmSLLDLL InsertionO(1) DeletionO(n)O(n)O(1)

4-39 Searching (1) Problem: Search for a given target value in a linked list. Unsorted SLL linear search algorithm: To find which (if any) node of the SLL headed by first contains an element equal to target: 1.For each node curr in the SLL headed by first, repeat: 1.1.If target is equal to node curr’s element, terminate with answer curr. 2.Terminate with answer none. DLL linear search is similar, except that we can search from last to first if preferred.

4-40 Searching (2) Analysis (counting comparisons): Let n be the SLL’s length. If the search is successful: Average no. of comparisons = (n + 1)/2 If the search is unsuccessful: No. of comparisons = n In either case, time complexity is O(n).

4-41 Searching (3) Java implementation: public SLLNode search (Object target) { // Find which (if any) node of this SLL contains an element equal to // target. Return a link to the matching node (or null if there is // none). for (SLLNode curr = this.first; curr != null; curr = curr.succ) { if (target.equals(curr.element)) return curr; } return null; }