Uploaded by ares thegod

Zybooks 7.1 Singly Linked Lists

advertisement
Zybooks 7.1 Singly Linked Lists
A Linked List is an alternative way to store large amounts of data,
in a sequential fashion, that is different from an array-based
structure
 Arrays store data in large, contiguous blocks of memory
allowing for fast, immediate, and random access… but,
knowing how big the Array needs to be is tricky to avoid
wasting memory
 Linked Lists store each object separately (called a Node)
along with the address of the next object in the list (called a
Link)… The objects can be stored anywhere in memory, but
are logically connected, in order, by the Link (the next address)
 A Linked List uses exactly the amount of memory it needs…
we can add a Node or delete a Node from the Linked List
 A Node contains 2 things… the address of the object it refers
to (its element), and the address of the next Node (its link)
 When you connect these Nodes together, you have a Linked
List
 It is customary to keep track of 3 things… the Head, the Tail,
and the number of elements in the Linked List (the Size)
 Head… without knowing the address of the 1st Node in the
Linked List, there would be no way to get to any Node in the
Linked List
 From the Head Node, we can Traverse the Linked List by
following the Next address pointers… we can access any Node
in the Linked List from the Head
 Tail… it is not necessary to keep track of the Tail Node… we
could always Traverse the Linked List until we got to the last
Node (the last Node would have a next pointer of None)
 But, to avoid a sequential Traversal of the Linked List every
time we needed to find the Tail, keeping track of the last Node
in the Linked List seems like a good idea
 Size… it is not necessary to keep track of the number of
elements in the Linked List (the Size)… we could always
Traverse the Linked List and count the number of Nodes
 Again, to avoid the sequential Traversal of the Linked List to
count the number of elements, keeping track of how many
elements are in the Linked List (the Size) is a good idea
We’ll use a Linked List to implement a Stack and a Queue later in
this section… but first, a few basic actions for a Linked List
 A basic definition of a Node class…
class Node:
def __init__(self, element, next):
self._element = element
self._next = next
 An empty List…
head = None
tail = None
size = 0
 Inserting an element at the head of a Linked List (works for an
empty list, also)…
newest = Node(element, head)
head = newest
if tail == None:
tail = head
size = size + 1
 Inserting an element at the tail of a Linked List (works for an
empty list, also…
newest = Node(element, None)
if head == None:
head = newest
else:
tail._next = newest
tail = newest
size = size + 1
 Inserting an element in the middle of a Linked List…
# previous is pointing to the Node before the insertion point
newest = Node(element, previous._next)
previous._next = newest
size = size + 1
 Removing an element from the head of a Linked List…
if head == None:
raise Empty(‘Linked List is Empty’)
else:
if head == tail:
head = tail = None
else:
head = head._next
size = size - 1
 Removing an element from the tail of a Linked List (this is a
sequential operation – not efficient at all)… a Doubly Linked
List will solve this later…
if tail == None:
raise Empty(‘Linked List is Empty’)
else:
if head == tail:
head = tail = None
else:
previous = head
while previous._next != tail
previous = previous._next
previous._next = None
tail = previous
size = size - 1
 Removing an element from the middle of a Linked List…
# previous is pointing to the Node before the deletion point
previous._next = previous._next._next
size = size - 1
 Zybooks (Code Fragment 7.1.5) Has an implementation of the
class LinkedStack in Python using the class _Node as
the basis for the Linked List structure
 The class _Node is implemented as a nested class,
discussed in section 2.5… this is for efficiency in a Linked
List
 Notice it is declared inside of class LinkedStack… and,
the constructor is called as an instance method…
 Zybooks (Code Fragment 7.1.6) Has an implementation of the
class LinkedQueue in Python… again, using the class
_Node as the basis for the Linked List structure
 Singly Linked List Exercises (7.8.1, 7.8.2, 7.8.3, 7.8.4 (singly
linked list only for 7.8.4))
Download