Linked List: Traversal Insertion Deletion - PowerPoint PPT Presentation

About This Presentation
Title:

Linked List: Traversal Insertion Deletion

Description:

Linked List Traversal. Traversal means 'visiting' or examining each node. ... This can be done without any traversal/searching. Requires an in/out pointer ... – PowerPoint PPT presentation

Number of Views:1698
Avg rating:3.0/5.0
Slides: 90
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: Linked List: Traversal Insertion Deletion


1
Linked List Traversal InsertionDeletion
Lecture 9
2
Linked List Traversal
LB
3
Linked List Traversal
LB
  • 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?

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

5
The Big Picture
LB
42
98
12
6
heap
stack
head
6
The Little Picture
LB
12
7
The Classic Code
LB
  • 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 ltgt NIL) then
  • print(current.data)
  • Traverse(current.next)
  • endif
  • endprocedure // Traverse

8
The Big Picture
LB
42
98
12
6
heap
stack
head
9
Questions?
LB
10
Insertion Into Linked Lists
11
Node Definition
  • node definesa record
  • data isoftype Num
  • next isoftype ptr toa node
  • endrecord

12
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

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

14
Finding the Correct Location
  • Three possible positions
  • The front
  • The end
  • Somewhere in the middle

15
Inserting to the Front
48
17
142
head
  • There is no work to find the correct location
  • Empty or not, head will point to the right
    location

16
Inserting to the End
//
head
48
17
142
  • Find the end of the list(when at NIL)
  • Recursion or iteration

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

18
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 lt- new(Node)
  • temp.data lt- new_data
  • temp.next lt- current
  • current lt- temp

19
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

20
Inserting at the Front of a Linked List
21
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

22
Animated Insert to Front of Linked List
head
4
17
2
procedure Insert (current iot in/out ptr toa
Node, new_data isoftype in Num) temp
isoftype ptr toa Node temp lt- new(Node)
temp.data lt- new_data temp.next lt- current
current lt- temp endprocedure
(current iot in/out ptr toa Node,
temp lt- new(Node)
temp.data lt- new_data
temp.next lt- current
current lt- temp
23
Inserting at the End of a Linked List
24
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

25
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 lt- new( Node )
  • current.data lt- new_data
  • current.next lt- NIL
  • else
  • Add_To_End( current.next, new_data)
  • endif
  • endprocedure //Add_To_End

26
Inserting at the End of a Linked List
head
48
17
142
53
27
Inserting in Order into a Linked List
28
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

29
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 gt new_data)) then
  • temp lt- new( Node )
  • temp.data lt- new_data
  • temp.next lt- current
  • current lt- temp
  • else
  • Insert_In_Order(current.next,new_data)
  • endif
  • endprocedure //Insert_In_Order

30
Inserting In Order into a Linked List
Head
13
18
23
19
31
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)

32
Questions?
33
Deleting an Element from a Linked List
34
The Node Definition
  • Node definesa record
  • data isoftype num
  • next isoftype ptr toa Node
  • endrecord

35
The Scenario
head
4
17
  • Begin with an existing linked list
  • Could be empty or not
  • Could be ordered or not

36
The Scenario
head
4
17
  • Begin with an existing linked list
  • Could be empty or not
  • Could be ordered or not

37
The Scenario
head
4
17
  • Begin with an existing linked list
  • Could be empty or not
  • Could be ordered or not

38
The Scenario
head
4
17
  • Begin with an existing linked list
  • Could be empty or not
  • Could be ordered or not

39
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

40
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 ltgt nil) then
  • current lt- current.next
  • endif
  • endprocedure

41
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 lt- current.next
  • This removes the element, since nothing will
    point to the node.

42
head
4
17
. . Delete(head, 4) . .
43
head
4
17
. . Delete(head, 4) . .
procedure Delete(cur iot in/out ptr toa Node,
target isoftype in num) // Delete single
occurrence of a node. if(cur ltgt NIL) then
if(cur.data target) then cur lt- cur.next
else Delete(cur.next, target) endif
endif endprocedure
Target 4
44
head
4
17
. . Delete(head, 4) . .
procedure Delete(cur iot in/out ptr toa Node,
target isoftype in num) // Delete single
occurrence of a node. if(cur ltgt NIL) then
if(cur.data target) then cur lt- cur.next
else Delete(cur.next, target) endif
endif endprocedure
Target 4
45
head
4
17
. . Delete(head, 4) . .
procedure Delete(cur iot in/out ptr toa Node,
target isoftype in num) // Delete single
occurrence of a node. if(cur ltgt NIL) then
if(cur.data target) then cur lt- cur.next
else Delete(cur.next, target) endif
endif endprocedure
Target 4
46
head
4
17
. . Delete(head, 4) . .
procedure Delete(cur iot in/out ptr toa Node,
target isoftype in num) // Delete single
occurrence of a node. if(cur ltgt NIL) then
if(cur.data target) then cur lt- cur.next
else Delete(cur.next, target) endif
endif endprocedure
Target 4
47
head
4
17
. . Delete(head, 4) . .
48
head
4
17
. . Delete(head, 4) . .
49
head
4
17
. . Delete(head, 4) . .
50
head
4
17
. . Delete(head, 4) . .
51
head
4
17
. . Delete(head, 4) . .
52
head
4
17
. . Delete(head, 4) . .
53
head
17
. . Delete(head, 4) . .
54
head
17
. . Delete(head, 4) . .
55
head
17
. . Delete(head, 4) . .
56
Linked List Deletion(All Occurrences)
57
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

58
head
4
17
. . Delete(head, 4) . .
59
head
4
17
. . Delete(head, 4) . .
60
head
4
17
. . Delete(head, 4) . .
61
head
4
17
. . Delete(head, 4) . .
62
head
4
17
. . Delete(head, 4) . .
63
head
4
17
. . Delete(head, 4) . .
64
head
4
17
. . Delete(head, 4) . .
65
head
4
17
. . Delete(head, 4) . .
66
head
4
17
. . Delete(head, 4) . .
67
head
4
17
. . Delete(head, 4) . .
68
head
17
. . Delete(head, 4) . .
69
head
17
. . Delete(head, 4) . .
70
head
17
. . Delete(head, 4) . .
71
head
17
. . Delete(head, 4) . .
72
head
17
. . Delete(head, 4) . .
73
head
17
. . Delete(head, 4) . .
74
head
17
. . Delete(head, 4) . .
75
head
17
. . Delete(head, 4) . .
76
head
17
. . Delete(head, 4) . .
77
head
4
17
. . Delete(head, 4) . .
78
head
4
17
. . Delete(head, 4) . .
79
head
17
. . Delete(head, 4) . .
80
head
17
. . Delete(head, 4) . .
81
head
17
. . Delete(head, 4) . .
82
head
17
. . Delete(head, 4) . .
83
head
17
. . Delete(head, 4) . .
84
head
17
. . Delete(head, 4) . .
85
head
17
. . Delete(head, 4) . .
86
head
17
. . Delete(head, 4) . .
87
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

88
Questions?
89
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com