+ All Categories
Home > Documents > Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion...

Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion...

Date post: 20-Jan-2018
Category:
Upload: samantha-thompson
View: 217 times
Download: 0 times
Share this document with a friend
Description:
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? 3
30
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1
Transcript
Page 1: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

ReviewLinked ListInsertion DescriptionDeletion DescriptionBasic Node ImplementationConclusion

1

Page 2: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Linked List TraversalInserting into a linked list involves

two steps:Find the correct locationDo the work to insert the new value

We can insert into any positionFrontEndSomewhere in the middle(to preserve order)

2

Page 3: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Linked List TraversalTraversal means “visiting” or

examining each node.Simple linked list

Start at the beginningGo one node at a time until the end

Recursive procedure (or function)Given a head pointerLooks at just one nodeWhat procedure will look at the rest?

3

Page 4: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

The Node Code

Node definesa recorddata isoftype Numnext isoftype Ptr toa Node

endrecord

4

Page 5: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

The Big Picture

42

heap

stackhead

98 12 6

LB

5

Page 6: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

The Little Picture

12

6

Page 7: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

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 listif(current <> NIL) then

print(current^.data)Traverse(current^.next)

endifendprocedure // Traverse

7

Page 8: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

The Big Picture

42

heap

stackhead

98 12 6

8

Page 9: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

9

Insertion Into Linked Lists

Page 10: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Node Definition

node definesa record data isoftype Num next isoftype ptr toa nodeendrecord

10

Page 11: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

The ScenarioYou have a linked list

Perhaps empty, perhaps notPerhaps ordered, perhaps not

You want to add an element into the linked list

48 17 142head //

11

Page 12: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Adding an Element to a Linked ListInvolves two steps:

Finding the correct location

Doing the work to add the node

12

Page 13: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Finding the Correct LocationThree possible positions:

The frontThe endSomewhere in the middle

13

Page 14: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

head

Inserting to the Front

There is no work to find the correct location

Empty or not, head will point to the right location

48 17 142head 93

14

Page 15: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting to the End

Find the end of the list(when at NIL)Recursion or iteration

48 17 142head //93 //

Don’t Worry!15

Page 16: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting to the Middle

Used when order is importantGo to the node that should follow the one to addRecursion or iteration

17 48 142head //93 //142

16

Page 17: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

The Work to Add the Node

Create the new nodeFill in the data fieldDeal 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

17

Page 18: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Three Types of InsertionTo front

No recursion neededTo end

Get to the end, then add nodeIn order (in middle)

To maintain sorted property

18

Page 19: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

19

Inserting at the Front of a Linked List

Page 20: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting to the Front of a Linked ListNeed an in/out pointer parameter

Create new nodeFill in dataMake new node’s next pointer point

to currentUpdate current to point to new node

20

Page 21: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

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 <- tempendprocedure

4 17

head

42

Current new_data temp2R

2

Animated Insert to Front of Linked List

(current iot in/out ptr toa Node,

temp <- new(Node)temp^.data <- new_datatemp^.next <- currentcurrent <- temp

21

Page 22: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

22

Inserting at the End of a Linked List

Page 23: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting to End of a Linked ListRecursively traverse the list until at

endThen:

Create new node at currentFill in dataTerminate the new node’s next pointer to point to NIL

23

Page 24: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting to the End of a Linked Listprocedure 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) endifendprocedure //Add_To_End

24

Page 25: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

48 17 142head 53

current new_data 53R

current new_data 53R

current new_data 53R

current new_data 53R

Inserting at the End of a Linked List

25

Page 26: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting in Order into a Linked List

Page 27: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting In Order into a Linked ListRecursively traverse until you find the

correct place to insertCompare to see if you need to insert

before currentIf adding largest value, then insert at

the endPerform the commands to do the

insertionCreate new nodeAdd dataUpdate the next pointer of the new

nodeUpdate current to point to new node

27

Page 28: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

Inserting in Order into a Linked Listprocedure 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) endifendprocedure //Insert_In_Order

28

Page 29: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

13 18 23Head

Inserting In Order into a Linked List

19

current new_data tempR 19

current new_data tempR 19

current new_data tempR 19

29

Page 30: Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.

SummaryInserting into a linked list involves

two steps:Find the correct locationDo the work to insert the new value

We can insert into any positionFrontEndSomewhere in the middle

30


Recommended