Skip to content

Instantly share code, notes, and snippets.

@Mintri1199
Last active November 8, 2021 08:34
Show Gist options
  • Save Mintri1199/07692b57d726a83338ba0c33c01697c3 to your computer and use it in GitHub Desktop.
Save Mintri1199/07692b57d726a83338ba0c33c01697c3 to your computer and use it in GitHub Desktop.
class ListDeque:
def __init__(self, iterable=None):
# Initialize a new list to store the items
self.list = list()
if iterable is not None:
self.list.extend(iterable)
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 len(self.list) == 0
def length(self):
"""Return the number of items in this stack."""
return len(self.list)
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.insert(0, 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[0]
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[-1]
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:
return self.list.pop(0)
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:
return self.list.pop(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment