Skip to content

Instantly share code, notes, and snippets.

@wambu-i
Last active December 10, 2018 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wambu-i/0b7a1f33d702c41c9da4d2a106a30fd3 to your computer and use it in GitHub Desktop.
Save wambu-i/0b7a1f33d702c41c9da4d2a106a30fd3 to your computer and use it in GitHub Desktop.
Implementation of a Doubly Linked List in Python
# Implementation of a dequeue using a doubly linked list
# Supports insertion and deletion of elements at both sides using FIFO
from doubly_linked import DoublyLinked
class LinkedDequeue(DoublyLinked):
'''A dequeue class that inherits from the DoublyLinked class.
It uses the inherited functions and attributes to perform dequeue functions.'''
def __init__(self):
super().__init__()
def dequeue_size(self):
'''Get the current size of the dequeue.'''
size = self.get_size()
return size
def enqueue_first(self, element):
'''Insert an element at the front of the dequeue.'''
self.insert_first(element)
def enqueue_last(self, element):
'''Insert an element at the back of the dequeue.'''
self.insert_last(element)
def dequeue_first(self):
'''Return and remove the element at the front of the dequeue.'''
element = self.delete_first()
return element
def dequeue_last(self):
'''Return and remove the element at the back of the dequeue.'''
element = self.delete_last()
return element
def top(self):
'''Return but not remove the element at the front of the dequeue.'''
element = self._head.element
return element
if __name__ == '__main__':
deck = LinkedDequeue()
deck.insert_first(7)
deck.insert_last(3)
deck.insert_last(4)
deck.insert_first(1)
print(deck.dequeue_size()) # 4
print(deck.dequeue_first()) # 1
print(deck.delete_last()) # 4
print(deck.top()) #7
# A doubly linked list is a linked list in which each node keeps an explicit reference to the node before it and a reference to the node after it.
from errors import UnderflowError, OverflowError
class DoublyLinked:
'''A class implementation of a doubly linked list.
Uses sentinels for border demarcation.'''
class Node:
'''A nested class that creates the element and references for each node.'''
__slots__ = 'element', 'next', 'previous'
def __init__(self, _element = None, _next = None, _previous = None):
self.element = _element
self.next = _next
self.previous = _previous
def __init__(self):
self._head = self.Node()
self._tail = self.Node()
self.size = 0
def insert_first(self, element):
'''Insert a new _head.'''
if self.size == 0:
new = self.Node(element, None, None)
self._tail = self._head
self.size += 1
new = self.Node(element, self._head, None)
self._head.previous = new
self._head = new
return new
def insert_last(self, element):
'''Insert a new tail.'''
if self.size == 0:
new = self.Node(element, None, None)
self.size += 1
new = self.Node(element, None, self._tail)
self._tail.next = new
self._tail = new
def delete_first(self):
'''Remove and return the element at the head.'''
if self.size == 0:
raise UnderflowError('List is empty!')
value = self._head.element
if self.size == 1:
self._head = None
self.tail = None
else:
self._head = self._head.next
self._head.previous = None
self.size -= 1
return value
def delete_last(self):
'''Remove and return the element at the tail.'''
if self.size == 0:
raise UnderflowError('List is empty!')
value = self._tail.element
if self.size == 1:
self._head = None
self.tail = None
else:
self._tail= self._tail.previous
self._tail.next = None
self.size -= 1
return value
def get_size(self):
return self.size
class Error(Exception):
pass
class UnderflowError(Error):
'''Error thrown when the stack is empty.'''
def __init__(self, message):
self.message = message
class OverflowError(Error):
'''Error thrown when the data structure is full.'''
def __init__(self, message):
self.message = message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment