Review Deleting an Element from a Linked List

  • Slides: 28
Download presentation
Review Deleting an Element from a Linked List Deletion involves: Getting to the correct

Review Deleting an Element from a Linked List 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 1

Singly link list All the nodes in a singly linked list are arranged sequentially

Singly link list All the nodes in a singly linked list are arranged sequentially by linking with a pointer. A singly linked list can grow or shrink, because it is a dynamic data structure. 2

figure explains the different operations on a singly linked list. 3

figure explains the different operations on a singly linked list. 3

Insertion and Deletion 4

Insertion and Deletion 4

Singly Link list • Learn about linked lists • Become aware of the basic

Singly Link list • Learn about linked lists • Become aware of the basic properties of linked lists • Explore the insertion and deletion operations on linked lists • Discover how to build and manipulate a linked list 5

Introduction Data can be organized and processed sequentially using an array, called a sequential

Introduction Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is slow Sorted array: insertion and deletion is slow 6

Linked Lists Linked list: a list of items (nodes), in which the order of

Linked Lists 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 Link field in last node is NULL 7

Linked Lists (cont'd. ) Because each node of a linked list has two components,

Linked Lists (cont'd. ) Because each node of a linked list has two components, we need to declare each node as a class or struct Data type of a node depends on the specific application The link component of each node is a pointer 8

Linked Lists: Some Properties 9

Linked Lists: Some Properties 9

Linked Lists: Some Properties (cont'd. ) current = head; Copies value of head into

Linked Lists: Some Properties (cont'd. ) current = head; Copies value of head into current 10

Linked Lists: Some Properties (cont'd. ) current = current->link; 11

Linked Lists: Some Properties (cont'd. ) current = current->link; 11

Traversing a Linked List The basic operations of a linked list are: Search to

Traversing a Linked List The basic operations of a linked list are: Search to determine if an item is in the list Insert an item in the list Delete an item from the list Traversal: given a pointer to the first node of the list, step through the nodes of the list 12

Traversing a Linked List (cont'd. ) To traverse a linked list: Example: 13

Traversing a Linked List (cont'd. ) To traverse a linked list: Example: 13

Item Insertion and Deletion Consider the following definition of a node: We will use

Item Insertion and Deletion Consider the following definition of a node: We will use the following variable declaration: 14

Insertion Consider the following linked list: A new node with info 50 is to

Insertion Consider the following linked list: A new node with info 50 is to be created and inserted after p 15

Insertion (cont'd. ) 16

Insertion (cont'd. ) 16

Insertion (cont'd. ) Using two pointers, we can simplify the insertion code somewhat To

Insertion (cont'd. ) Using two pointers, we can simplify the insertion code somewhat To insert new. Node between p and q: The order in which these statements execute does not matter 17

Insertion (cont'd. ) 18

Insertion (cont'd. ) 18

Deletion Node with info 34 is removed from the list, but memory is still

Deletion Node with info 34 is removed from the list, but memory is still occupied; node is dangling 19

Deletion (cont’d. ) 20

Deletion (cont’d. ) 20

Building a Linked List If data is unsorted The list will be unsorted Can

Building a Linked List If data is unsorted The list will be unsorted Can build a linked list forward or backward Forward: a new node is always inserted at the end of the linked list Backward: a new node is always inserted at the beginning of the list 21

Building a Linked List Forward You need three pointers to build the list: One

Building a Linked List Forward You need three pointers to build the list: One to point to the first node in the list, which cannot be moved One to point to the last node in the list One to create the new node 22

Building a Linked List Forward (cont’d. ) 23

Building a Linked List Forward (cont’d. ) 23

Building a Linked List Forward (cont’d. ) 24

Building a Linked List Forward (cont’d. ) 24

Building a Linked List Forward (cont'd. ) We now repeat statements 1 through 6

Building a Linked List Forward (cont'd. ) We now repeat statements 1 through 6 b three more times: 25

Building a Linked List Forward (cont’d. ) 26

Building a Linked List Forward (cont’d. ) 26

Building a Linked List Backward The algorithm is: Initialize first to NULL For each

Building a Linked List Backward The algorithm is: Initialize first to NULL For each item in the list Create the new node, new. Node Store the item in new. Node Insert new. Node before first Update the value of the pointer first 27

Building a Linked List Backward (cont’d. ) 28

Building a Linked List Backward (cont’d. ) 28