
Data structure and algorithm in Python Linked Lists Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Singly Linked Lists 2. Circularly Linked Lists 3. Doubly Linked Lists 4. The Positional List ADT 5. Sorting a Positional List 6. Favorite List 1 Python’s list class is highly optimized, and often a great choice for storage. With that said, there are some notable disadvantages: 1. The length of a dynamic array might be longer than the actual number of elements that it stores. 2. Amortized bounds for operations may be unacceptable in real-time systems. 3. Insertions and deletions at interior positions of an array are expensive. In this lecture, we introduce a data structure known as a linked list, which provides an alternative to an array-based sequence (such as a Python list). 2 Both array-based sequences and linked lists keep elements in a certain order, but using a very different style. • An array provides the more centralized representation, with one large chunk of memory capable of accommodating references to many elements. • A linked list, in contrast, relies on a more distributed representation in which a lightweight object, known as a node, is allocated for each element. Each node maintains a reference to its element and one or more references to neighboring nodes in order to collectively represent the linear order of the sequence. 3 Singly Linked Lists Singly Linked Lists Definition A singly linked list, in its simplest form, is a collection of nodes that collectively form a linear sequence. Each node stores a reference to an object that is an element of the sequence, as well as a reference to the next node of the list 4 Singly Linked Lists Definition : head and tail The first and last node of a linked list are known as the head and tail of the list, respectively. Definition : traverse By starting at the head, and moving from one node to another by following each node’s next reference, we can reach the tail of the list. We can identify the tail as the node having None as its next reference. This process is commonly known as traversing the linked list. 5 Singly Linked Lists 6 Singly Linked Lists 7 Singly Linked Lists Inserting an Element at the Head of a Singly Linked List Inserting an Element at the Head of a Singly Linked List An important property of a linked list is that it does not have a predetermined fixed size; it uses space proportionally to its current number of elements. 8 Inserting an Element at the Head of a Singly Linked List How to insert an element at the head of the list? 9 Inserting an Element at the Head of a Singly Linked List How to insert an element at the head of the list? 9 Inserting an Element at the Head of a Singly Linked List 1. create a new node, set its element to the new element, set its next link to the current head; 2. set the list’s head to point to the new node. 10 Inserting an Element at the Head of a Singly Linked List 11 Singly Linked Lists Inserting an Element at the tail of a Singly Linked List Inserting an Element at the tail of a Singly Linked List We can also easily insert an element at the tail of the list, provided we keep a reference to the tail node. 12 Inserting an Element at the tail of a Singly Linked List 13 Singly Linked Lists Removing an Element at the head of a Singly Linked List Removing an Element at the head of a Singly Linked List Removing an element from the head of a singly linked list is essentially the reverse operation of inserting a new element at the head. 14 Removing an Element at the head of a Singly Linked List Removing an element from the head of a singly linked list is essentially the reverse operation of inserting a new element at the head. 14 If we want to support such an operation efficiently, we will need to make our list doubly linked. Removing an Element at the head of a Singly Linked List Unfortunately, we cannot easily delete the last node of a singly linked list. • Even if we maintain a tail reference directly to the last node of the list, we must be able to access the node before the last node in order to remove the last node. • But we cannot reach the node before the tail by following next links from the tail. • The only way to access this node is to start from the head of the list and search all the way through the list. But such a sequence of link-hopping operations could take a long time. 15 Removing an Element at the head of a Singly Linked List Unfortunately, we cannot easily delete the last node of a singly linked list. • Even if we maintain a tail reference directly to the last node of the list, we must be able to access the node before the last node in order to remove the last node. • But we cannot reach the node before the tail by following next links from the tail. • The only way to access this node is to start from the head of the list and search all the way through the list. But such a sequence of link-hopping operations could take a long time. If we want to support such an operation efficiently, we will need to make our list doubly linked. 15 Singly Linked Lists Implementing a Stack with a Singly Linked List We can orient the top of the stack at the head because we can efficiently insert and delete elements in constant time only at the head as well as all stack operations affect the top. Implementing a Stack with a Singly Linked List To implement a stack with singly linked list, we need to decide whether to model the top of stack at the head or at the tail of the list. 16 Implementing a Stack with a Singly Linked List To implement a stack with singly linked list, we need to decide whether to model the top of stack at the head or at the tail of the list. We can orient the top of the stack at the head because we can efficiently insert and delete elements in constant time only at the head as well as all stack operations affect the top. 16 Implementing a Stack with a Singly Linked List from exceptions import Empty class LinkedStack: class _Node : __slots__ = ’_element’, ’_next’ def __init__(self, element, next ): self._element = element self._next = next def __init__(self): self._head = None self._size = 0 17 Implementing a Stack with a Singly Linked List def __len__(self): return self . _size def is_empty(self): return self._size == 0 def push(self, e): self._head = self._Node(e, self._head) self._size += 1 def top ( self ): if self.is_empty(): raise Empty(’Stack is empty’) return self._head._element 18 Implementing a Stack with a Singly Linked List def pop ( self ): if self.is_empty(): raise Empty(’Stack is empty’) answer = self._head._element self._head = self._head._next self._size -= 1 return answer 19 Implementing a Stack with a Singly Linked List 20 Singly Linked Lists Implementing a Queue with a Singly Linked List Implementing a Queue with a Singly Linked List Because we need to perform operations on both ends of the queue, we will explicitly maintain both a head reference and a tail reference as instance variables for each queue. The natural orientation for a queue is to align the front of the queue with the head of the list, and the back of the queue with the tail of the list, because we must be able to enqueue elements at the back, and dequeue them from the front. 21 Implementing a Queue with a Singly Linked List from exceptions import Empty class LinkedQueue: class _Node : __slots__ = ’_element’, ’_next’ def __init__(self, element, next ): self._element = element self._next = next def __init__(self): self._head = None self._tail = None self._size = 0 22 Implementing a Queue with a Singly Linked List def __len__(self): return self . _size def is_empty(self): return self._size == 0 def first(self): if self.is_empty(): raise Empty(’Queue is empty’) return self._head._element 23 Implementing a Queue with a Singly Linked List def dequeue(self): if self.is_empty(): raise Empty(’Queue is empty’) answer = self._head._element self._head = self._head._next self._size -= 1 if self.is_empty(): self._tail = None return answer 24 Implementing a Queue with a Singly Linked List def enqueue(self, e): newest = self._Node(e, None) if self.is_empty(): self._head = newest else : self._tail._next = newest self._tail = newest self._size += 1 25 Implementing a Queue with a Singly Linked List In terms of performance, the LinkedQueue is similar to the LinkedStack in that all operations run in worst-case constant time, and the space usage is linear in the current number of elements. 26 Circularly Linked Lists Circularly Linked Lists Definition : Circularly Linked Lists A circularly linked list is a linked list that its tail use its next reference to point back to the head of the list. 27 Circularly Linked Lists A circularly linked list provides a more general model than a standard linked list for data sets that are cyclic, that is, which do not have any particular notion of a beginning and end. 28 Circularly Linked Lists Even though a circularly linked list has no beginning or end, per se, we must maintain a reference to a particular node in order to make use of the list.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages114 Page
-
File Size-