Linked List: Traversal Insertion Deletion. Linked List Traversal LB.

Slides:



Advertisements
Similar presentations
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.
Advertisements

Linked Lists Mohammed Almashat CS /03/2006.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Linked Lists.
Data Structures: A Pseudocode Approach with C
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
O() Analysis of Methods and Data Structures Reasonable vs. Unreasonable Algorithms Using O() Analysis in Design.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Reference Details (copying, comparing). References You were introduced to the concept of pointers earlier this term. The pointer capabilities in pseudocode.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
Summary of lectures (1 to 11)
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.
Review Records Dynamic Memory and Pointers Introduction to Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
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,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Review Problems. What is the Big O? i
Binary Search Trees (BST)
Graphs Arrays Iteration Combining Data Structures.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Data Structure & Algorithms
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,
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Linked List :: Basic Concepts
Review Deleting an Element from a Linked List Deletion involves:
Chapter 15 Lists Objectives
Data Structure Dr. Mohamed Khafagy.
Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.
Linked List Variations
Simple Dynamic Data (Linked Lists)
Linked List Sudeshna Sarkar.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 18: Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Random inserting into a B+ Tree
Linked List and Selection Sort
Linked List Functions.
Linked Lists.
Linked List Intro CSCE 121.
Presentation transcript:

Linked List: Traversal Insertion Deletion

Linked List Traversal LB

Linked List Traversal Traversal means visiting or examining each node. Simple linked list –Start at the beginning –Go one node at a time until the end Recursive procedure (or function) –Given a head pointer –Looks at just one node –What procedure will look at the rest? LB

The Node Code Node definesa record data isoftype Num next isoftype Ptr toa Node endrecord LB

The Big Picture 42 heap stack head LB

The Little Picture 12 LB

The Classic Code Procedure Traverse (current isoftype in Ptr toa Node) // Precon: Pass in pointer to a list // Purpose: Print each data item // Postcon: No changes to list if(current <> NIL) then print(current^.data) Traverse(current^.next) endif endprocedure // Traverse LB

The Big Picture 42 heap stack head LB

Questions? LB

Insertion Into Linked Lists

Node Definition node definesa record data isoftype Num next isoftype ptr toa node endrecord

The Scenario You have a linked list –Perhaps empty, perhaps not –Perhaps ordered, perhaps not You want to add an element into the linked list head //

Adding an Element to a Linked List Involves two steps: Finding the correct location Doing the work to add the node

Finding the Correct Location Three possible positions: –The front –The end –Somewhere in the middle

head Inserting to the Front There is no work to find the correct location Empty or not, head will point to the right location head 93

Inserting to the End Find the end of the list (when at NIL) –Recursion or iteration head // 93 // Dont Worry!

Inserting to the Middle Used when order is important Go to the node that should follow the one to add –Recursion or iteration head // 93 // 142

The Work to Add the Node Create the new node Fill in the data field Deal with the next field –Point to nil (if inserting to end) –Point to current (front or middle) temp <- new(Node) temp^.data <- new_data temp^.next <- current current <- temp

Three Types of Insertion To front –No recursion needed To end –Get to the end, then add node In order (in middle) –To maintain sorted property

Inserting at the Front of a Linked List

Inserting to the Front of a Linked List Need an in/out pointer parameter Create new node Fill in data Make new nodes next pointer point to current Update current to point to new node

procedure Insert (current iot in/out ptr toa Node, new_data isoftype in Num) temp isoftype ptr toa Node temp <- new(Node) temp^.data <- new_data temp^.next <- current current <- temp endprocedure 4 17 head 42 Current new_data temp 2 R 2 Animated Insert to Front of Linked List (current iot in/out ptr toa Node, temp <- new(Node) temp^.data <- new_data temp^.next <- current current <- temp

Inserting at the End of a Linked List

Inserting to End of a Linked List Recursively traverse the list until at end Then: –Create new node at current –Fill in data –Terminate the new nodes next pointer to point to NIL

Inserting to the End of a Linked List procedure Add_To_End( current isoftype in/out Ptr toa Node, new_data isoftype in Num ) // Purpose: Add new node to end of list // Pre: current points to NIL-terminated list // Post: new list has added element at end if( current = NIL ) then current <- new( Node ) current^.data <- new_data current^.next <- NIL else Add_To_End( current^.next, new_data) endif endprocedure //Add_To_End

head 53 current new_data 53 R R R R Inserting at the End of a Linked List

Inserting in Order into a Linked List

Inserting In Order into a Linked List Recursively traverse until you find the correct place to insert –Compare to see if you need to insert before current –If adding largest value, then insert at the end Perform the commands to do the insertion –Create new node –Add data –Update the next pointer of the new node –Update current to point to new node

Inserting in Order into a Linked List procedure Insert_In_Order(current isoftype in/out Ptr toa Node, new_data isoftype in Num ) // comments here temp isoftype Ptr toa Node if ((current = NIL) OR (current^.data > new_data)) then temp <- new( Node ) temp^.data <- new_data temp^.next <- current current <- temp else Insert_In_Order(current^.next,new_data) endif endprocedure //Insert_In_Order

Head Inserting In Order into a Linked List 19 current new_data temp R 19 current new_data temp R 19 current new_data temp R 19

Summary Inserting into a linked list involves two steps: –Find the correct location –Do the work to insert the new value We can insert into any position –Front –End –Somewhere in the middle (to preserve order)

Questions?

Deleting an Element from a Linked List

The Node Definition Node definesa record data isoftype num next isoftype ptr toa Node endrecord

The Scenario Begin with an existing linked list –Could be empty or not –Could be ordered or not 417 head 42 6

The Scenario Begin with an existing linked list –Could be empty or not –Could be ordered or not 417 head 42 6

The Scenario Begin with an existing linked list –Could be empty or not –Could be ordered or not 417 head 42

The Scenario Begin with an existing linked list –Could be empty or not –Could be ordered or not 417 head 42

Finding the Match Well examine three situations: –Delete the first element –Delete the first occurrence of an element –Delete all occurrences of a particular element

Deleting the First Element This can be done without any traversal/searching Requires an in/out pointer procedure DeleteFront (current iot in/out ptr toa Node) // deletes the first node in the list if (current <> nil) then current <- current^.next endif endprocedure

Deleting from a Linked List Deletion from a linked list involves two steps: –Find a match to the element to be deleted (traverse until nil or found) –Perform the action to delete Performing the deletion is trivial: current <- current^.next –This removes the element, since nothing will point to the node.

417 head Delete(head, 4).

Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure 417 head 42 6 Target = 4

. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure 417 head 42 6 Target = 4

. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure 417 head 42 6 Target = 4

. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure 417 head 42 6 Target = 4

. Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 417 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 17 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 17 head 42 6 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete single occurrence of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next else Delete(cur^.next, target) endif endprocedure Target = 4

. Delete(head, 4). 17 head 42 6

Linked List Deletion (All Occurrences)

Deleting All Occurrences Deleting all occurrences is a little more difficult. Traverse the entire list and dont stop until you reach nil. If you delete, recurse on current If you dont delete, recurse on current^.next

417 head 4 6. Delete(head, 4).

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

417 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 4 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4 procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4). procedure Delete(cur iot in/out ptr toa Node, target isoftype in num) // Delete all occurrences of a node. if(cur <> NIL) then if(cur^.data = target) then cur <- cur^.next Delete(cur, target) else Delete(cur^.next, target) endif endprocedure Target = 4

17 head 6. Delete(head, 4).

Summary Deletion involves: –Getting to the correct position –Moving a pointer so nothing points to the element to be deleted Can delete from any location –Front –First occurrence –All occurrences

Questions?