{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP3502_7_LinkedLists3 - Linked Lists Deleting Nodes...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Linked Lists: Deleting Nodes COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Linked Lists: Deleting Nodes page 2 Linked Lists: Basic Operations Operations Performed on Linked Lists Several operations can be performed on linked lists Add a new node Delete a node Search for a node Counting nodes Modifying nodes and more We will build functions to perform these operations
Background image of page 2
Linked Lists: Deleting Nodes page 3 Linked Lists: Deleting Nodes General Approach: You must search for the node that you want to delete (remember, we are using sorted lists) If found, you must delete the node from the list This means that you change the various link pointers The predecessor of the deleted node must point to the deleted nodes successor Finally, the node must be physically deleted from the heap You must free the node
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Linked Lists: Deleting Nodes page 4 Linked Lists: Deleting Nodes General Approach: There are 4 deletion scenarios: 1) Delete the first node of a list 2) Delete any middle node of the list Not the first node or the last node 3) Delete the last node of the list 4) A special case when we delete the only node in the list Causes the resulting list to become empty
Background image of page 4
Linked Lists: Deleting Nodes page 5 Linked Lists: Deleting Nodes 4 Cases of Deletion: 1) Delete the first node of a list 6 myList NULL The list after deleting the first node 4 myList NULL The initial list 6 node to be deleted
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Linked Lists: Deleting Nodes page 6 Linked Lists: Deleting Nodes 4 Cases of Deletion: 1) Delete the first node of a list Think about how you make this happen: myList needs to point to the 2 nd node in the list So we save the address of the 2 nd node into myList Where do we get that address: It is saved in the “ next ” of the first node So we take that address and save it into myList 4 myList NULL The initial list 6 node to be deleted
Background image of page 6
Linked Lists: Deleting Nodes page 7 Linked Lists: Deleting Nodes 4 Cases of Deletion: 1) Delete the first node of a list Think about how you make this happen: myList needs to point to the 2 nd node in the list So we save the address of the 2 nd node into myList Where do we get that address: It is saved in the “ next ” of the first node So we take that address and save it into myList Finally, we free the 1 st node 4 myList NULL The initial list 6 node to be deleted
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Linked Lists: Deleting Nodes page 8 Linked Lists: Deleting Nodes 4 Cases of Deletion: 1) Delete any middle node of the list 4 myList NULL The initial list 8 6 node to be deleted 4 myList NULL The list after deletion has occurred 8
Background image of page 8
Linked Lists: Deleting Nodes page 9 Linked Lists: Deleting Nodes 4 Cases of Deletion: 1) Delete any middle node of the list Think about how you make this happen: Node # 4 (with 4 as data) needs to point to Node # 8 So we save the address of Node #8 into “ next ” of Node # 4 Where do we get the address of Node #8?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}