Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 116 additions & 9 deletions doubly_linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,30 @@ def __init__(self, value, prev=None, next=None):
self.prev = prev
self.value = value
self.next = next


def get_value(self):
return self.value

def get_next(self):
if self.next:
return self.next
else:
return None

def get_previous(self):
if self.prev:
return self.prev
else:
return None

def delete(self):
if self.prev:
self.prev.next = self.next
if self.next:
self.next.prev = self.prev
self.value = None
self.next = None
self.prev = None
"""
Our doubly-linked list class. It holds references to
the list's head and tail nodes.
Expand All @@ -27,56 +50,140 @@ def __len__(self):
the old head node's previous pointer accordingly.
"""
def add_to_head(self, value):
pass
root = ListNode(value)
if self.head and self.tail:
root.next = self.head
self.head.prev = root
self.head = root
else:
self.head = root
self.tail = root
self.length += 1

"""
Removes the List's current head node, making the
current head's next node the new head of the List.
Returns the value of the removed Node.
"""
def remove_from_head(self):
pass
if self.head:
headValue = self.head.get_value()
if self.length > 1:
newHead = self.head.get_next()
self.head.delete()
self.head = newHead
self.length -= 1
return headValue
else:
self.head.delete()
self.head = None
self.tail = None
self.length -= 1
return headValue
else:
return None

"""
Wraps the given value in a ListNode and inserts it
as the new tail of the list. Don't forget to handle
the old tail node's next pointer accordingly.
"""
def add_to_tail(self, value):
pass
endNode = ListNode(value)
if self.head and self.tail:
endNode.prev = self.tail
self.tail.next = endNode
self.tail = endNode
else:
self.head = endNode
self.tail = endNode
self.length += 1

"""
Removes the List's current tail node, making the
current tail's previous node the new tail of the List.
Returns the value of the removed Node.
"""
def remove_from_tail(self):
pass
if self.tail:
tailValue = self.tail.get_value()
if self.length > 1:
newTail = self.tail.get_previous()
self.tail.delete()
self.tail = newTail
self.length -= 1
return tailValue
else:
self.tail.delete()
self.tail = None
self.head = None
self.length -= 1
return tailValue
else:
return None

"""
Removes the input node from its current spot in the
List and inserts it as the new head node of the List.
"""
def move_to_front(self, node):
pass
value = node.value
if node == self.head:
return
elif node == self.tail:
self.remove_from_tail()
self.add_to_head(value)
else:
self.delete(node)
self.add_to_head(value)

"""
Removes the input node from its current spot in the
List and inserts it as the new tail node of the List.
"""
def move_to_end(self, node):
pass
value = node.value
if node == self.tail:
return
elif node == self.head:
self.remove_from_head()
self.add_to_tail(value)
else:
self.delete(node)
self.add_to_tail(value)

"""
Deletes the input node from the List, preserving the
order of the other elements of the List.
"""
def delete(self, node):
pass
if self.length == 0:
return
elif self.length == 1:
self.head = None
self.tail = None
node.delete()
self.length -= 1
elif self.head == node:
self.remove_from_head()
elif self.tail == node:
self.remove_from_tail()
else:
node.delete()
self.length -= 1

"""
Finds and returns the maximum value of all the nodes
in the List.
"""
def get_max(self):
pass
if not self.head:
return None
max_value = self.head.value
current_node = self.head
while current_node:
if current_node.value > max_value:
max_value = current_node.value
current_node = current_node.get_next()

return max_value
19 changes: 14 additions & 5 deletions queue/queue.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
A queue is a data structure whose primary purpose is to store and
return elements in First In First Out order.

1. Implement the Queue class using an array as the underlying storage structure.
Make sure the Queue tests pass.
2. Re-implement the Queue class, this time using the linked list implementation
Expand All @@ -13,16 +12,26 @@
Stretch: What if you could only use instances of your Stack class to implement the Queue?
What would that look like? How many Stacks would you need? Try it!
"""


from singly_linked_list import LinkedList

class Queue:
def __init__(self):
self.size = 0
# self.storage = ?
self.storage = LinkedList()

def __len__(self):
pass
return self.size

def enqueue(self, value):
pass
self.storage.add_to_tail(value)
self.size += 1
return self.storage

def dequeue(self):
pass
if self.size >= 1:
value = self.storage.remove_head()
self.size -= 1
return value
return None
98 changes: 98 additions & 0 deletions queue/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
class Node:
def __init__(self, value, next_node=None):

self.value = value

self.next_node = next_node

def get_value(self):

return self.value

def get_next(self):

return self.next_node

def set_next(self, new_next):

self.next_node = new_next


class LinkedList:
def __init__(self):
self.head = None
self.tail = None

def add_to_head(self, value):
new_node = Node(value, self.head)
self.head = new_node
if self.head is None:
self.tail = new_node

def add_to_tail(self, value):

new_node = Node(value)

if self.head is None and self.tail is None:

self.head = new_node
self.tail = new_node

else:

self.tail.set_next(new_node)
self.tail = new_node

def remove_tail(self):

if self.head is None and self.tail is None:

return None

if self.head == self.tail:

value = self.head.get_value()

self.head = None
self.tail = None

return value

else:

value = self.tail.get_value()

current_node = self.head

while current_node.get_next() != self.tail:

current_node = current_node.get_next()

self.tail = current_node

self.tail.set_next(None)

return value

def remove_head(self):

if self.head is None and self.tail is None:

return None

if self.head == self.tail:

value = self.head.get_value()

self.head = None
self.tail = None

return value

else:

value = self.head.get_value()

self.head = self.head.get_next()

return value
Loading