singly-linked listslinked structure constructed using a collection of objects called nodes. each...

20
© 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise. Singly-Linked Lists Singly-Linked Lists Modifications by Nathan Sprague

Upload: others

Post on 26-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

© 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Singly-Linked ListsSingly-Linked Lists

Modifications by Nathan Sprague

Page 2: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

2Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Linked StructureLinked Structure

Constructed using a collection of objects called nodes.

Each node contains data and at least one reference or link to another node.

Linked list – a linked structure in which the nodes are linked together in linear order.

Page 3: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

3Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Linked ListLinked List

Terms: head – first node in the list. tail – last node in the list; link field has a null

reference.

self._

Page 4: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

4Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Linked ListLinked List

Most nodes have no name; they are referenced via the link of the preceding node.

head reference – the first node must be named or referenced by an external variable. Provides an entry point into the linked list. An empty list is indicated by a null head reference.

self._

Page 5: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

5Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Singly Linked ListSingly Linked List

A linked list in which each node contains a single link field and allows for a complete linear order traversal from

front to back.

self._

Page 6: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

6Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Node DefinitionNode Definition

The nodes are constructed from a simple storage class:

class ListNode: def __init__(self, data, next_node): self.data = data self.next = next_node

Page 7: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

7Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Prepending NodesPrepending Nodes

Example: add value 96 to the sample list.

self._

Page 8: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

8Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Prepending NodesPrepending Nodes

Create a new node for the new item.

Connect the new node to the list.

new_node

new_node self._

self._

Page 9: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

9Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Prepending NodesPrepending Nodes

The resulting list.

Python code

# Given the head reference and the new item. new_node = ListNode( new_item, self._head ) self._head = new_node

self._

Page 10: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

10Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Removing The First NodeRemoving The First Node

The head reference must be reposition to reference the next node in the list.

self._

Page 11: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

11Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Linked Stack...Linked Stack...

Page 12: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

12Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Using a Tail ReferenceUsing a Tail Reference

Some applications require items be appended to the end of the linked list. tail reference – a second external reference

indicating the tail or last node in the list.

self._ self._

Page 13: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

13Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Appending NodesAppending Nodes

Example: append 21 to the list.

new_nodeself._

self._

self._

self._

Page 14: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

14Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Appending NodesAppending Nodes

new_node = ListNode(item, None)if self._head is None: # list is empty self._head = new_nodeelse : self._tail.next = new_nodeself._tail = new_node

Given the head and tail reference, we can add an item to a linked list.

Page 15: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

15Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Linked Queue...Linked Queue...

Page 16: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

16Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Traversing the NodesTraversing the Nodes

We can traverse the nodes using a temporary external reference variable.

Initialize a temporary reference to the head node.

Visit the node.

cur_node

self._

self._

Page 17: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

17Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Traversing the NodesTraversing the Nodes

Advance the temporary reference to the next node using the link field and visit that node.

cur_nodeself._

Page 18: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

18Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Traversing the NodesTraversing the Nodes

Repeat the process until the reference falls off the end of the list.

cur_node

cur_node

self._

self._

Page 19: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

19Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Traversing the NodesTraversing the Nodes

Repeat the process until the reference falls off the end of the list.

cur_node

cur_node

self._

self._

Page 20: Singly-Linked ListsLinked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list

20Linked Structures – © 2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Traversal CodeTraversal Code

def print_list(self): cur_node = self._head while cur_node is not None : print(cur_node.data) cur_node = cur_node.next

Given the head reference, we can traverse the nodes.