Skip to content

Instantly share code, notes, and snippets.

@Mintri1199
Last active April 23, 2019 05:17
Show Gist options
  • Save Mintri1199/3813fbb5f45db033ab20357c2b73426a to your computer and use it in GitHub Desktop.
Save Mintri1199/3813fbb5f45db033ab20357c2b73426a to your computer and use it in GitHub Desktop.
from linkedlist import LinkedList
class LinkedListDeque:
def __init__(self, iterable=None):
# Initialize a new Linked list to store the items
self.list = LinkedList()
if iterable is not None:
for item in iterable:
self.append(item)
def __repr__(self):
"""Return a string representation of this stack."""
return 'Stack({} items, top={}, last={}'.format(self.length(), self.peek_first(), self.peek_last())
def is_empty(self):
"""Return True if this stack is empty, or False otherwise."""
return self.list.is_empty()
def length(self):
"""Return the number of items in this stack."""
return self.list.size
def append(self, item):
""" Insert the given item to the end of the double ended queue"""
self.list.append(item)
def prepend(self, item):
""" Insert the given item to the end of the double ended queue"""
self.list.prepend(item)
def peek_first(self):
"""Return the item on the top of this queue without removing it,
or None if this queue is empty."""
if self.is_empty():
return None
else:
return self.list.head.data
def peek_last(self):
"""Return the item on the end of this queue without removing it,
or None if this queue is empty."""
if self.is_empty():
return None
else:
return self.list.tail.data
def pop_first(self):
"""Remove and return the item on the top of this queue,
or raise ValueError if this queue is empty."""
if self.is_empty():
raise ValueError('Stack is empty')
else:
top_value = self.list.head.data
self.list.delete(top_value)
return top_value
def pop_last(self):
"""Remove and return the item on the end of this queue,
or raise ValueError if this queue is empty."""
if self.is_empty():
raise ValueError('Stack is empty')
else:
top_value = self.list.tail.data
self.list.delete(top_value)
return top_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment