Skip to content
Open

MVP #745

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
152 changes: 144 additions & 8 deletions doubly_linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,192 @@ def __len__(self):
the old head node's previous pointer accordingly.
"""
def add_to_head(self, value):
pass
newnode = ListNode(value)
if not self.head and not self.tail:
self.head = newnode
self.tail = newnode
self.length += 1
else:
newnode.next = self.head
self.head.prev = newnode
self.head = newnode
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.length == 0:
return None
elif self.head == self.tail:
value = self.head.value
self.head = None
self.tail = None
return value
else:
value = self.head.value
self.head = self.head.next
return value


"""
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
newnode = ListNode(value)
if not self.head and not self.tail:
self.head = newnode
self.tail = newnode
else:
self.tail.next = newnode
newnode.prev = self.tail
self.tail = newnode



"""
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.length == 0:
return None
elif self.head == self.tail:
value = self.head.value
self.head = None
self.tail = None
return value
else:
value = self.tail.value
self.tail = self.tail.prev
return value

"""
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
currentnode = self.head
if self.head.value == node:
print('already at the head of the linked list!')
elif self.tail.value == node:
value = self.tail.value
self.tail = self.tail.prev
self.tail.next = None
newnode = ListNode(value)
self.head.prev = newnode
newnode.next = self.head
self.head = newnode

else:
while currentnode.value != node:
currentnode = currentnode.next
value = currentnode.value
nextnode = currentnode.next
prevnode = currentnode.prev
prevnode.next = nextnode
nextnode.prev = prevnode
newnode = ListNode(value)
newnode.next = self.head
self.head.prev = newnode
self.head = newnode



"""
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
currentnode = self.head
if self.tail.value == node:
print('already at the end of the linked list!')
elif self.head.value == node:
value = self.head.value
newnode = ListNode(value)
self.head = self.head.next
self.tail.next = newnode
newnode.prev = self.tail
self.tail = newnode
else:
while currentnode.value != node:
currentnode = currentnode.next
value = currentnode.value
nextnode = currentnode.next
prevnode = currentnode.prev
prevnode.next = nextnode
nextnode.prev = prevnode
newnode = ListNode(value)
self.tail.next = newnode
newnode.prev = self.tail
self.tail = newnode

"""
Deletes the input node from the List, preserving the
order of the other elements of the List.
"""
def delete(self, node):
pass
currentnode = self.head
while currentnode.value != node:
currentnode = currentnode.next
currentnode.value = None


"""
Finds and returns the maximum value of all the nodes
in the List.
"""
def get_max(self):
pass
values = []
currentnode = self.head
while currentnode.next != None:
values.append(int(currentnode.value))
currentnode = currentnode.next
values.append(int(currentnode.value))
return max(values)

def view(self):
list = []
currentnode = self.head
while currentnode != self.tail:
list.append(str(currentnode.value))
currentnode = currentnode.next
list.append(str(currentnode.value))
return '-->'.join(list)

LL = DoublyLinkedList()
LL.add_to_head(2)
LL.add_to_head(4)
LL.add_to_head(6)
print(LL.view())
LL.move_to_end(2)
# LL.remove_from_tail()
print(LL.view())
LL.move_to_end(6)
print(LL.view())
LL.move_to_end(6)
print(LL.view())
LL.move_to_end(6)
LL.move_to_end(4)
print(LL.view())
LL.move_to_front(4)
print(LL.view())
LL.move_to_front(4)
print(LL.view())
LL.move_to_front(2)
print(LL.view())
print(LL.get_max())
LL.delete(4)
print(LL.view())
LL.remove_from_head()
LL.remove_from_tail()
print(LL.view())
LL.add_to_tail(2)
print(LL.view())
# LL.remove_from_tail()
# print(LL.view())
50 changes: 46 additions & 4 deletions queue/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,62 @@
Make sure the Queue tests pass.
3. What is the difference between using an array vs. a linked list when
implementing a Queue?


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 = []

def __len__(self):
pass
return self.size

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

def dequeue(self):
pass
self.storage.pop(0)
self.size -= 1

def view(self):
return self.storage

class Queues:
def __init__(self):
self.size = 0
self.storage = LinkedList(None,None)

def __len__(self):
return self.size

def enqueue(self, value):
self.storage.add_to_head(value)

def dequeue(self):
self.storage.remove_tail()

def view(self):
return self.storage.view()

que = Queue()
que.enqueue(1)
que.enqueue(2)
que.enqueue(3)
print(que.view())
que.dequeue()
print(que.view())

# Linked list implementation
que = Queues()
que.enqueue(1)
que.enqueue(2)
que.enqueue(3)
print(que.view())
que.dequeue()
print(que.view())
77 changes: 77 additions & 0 deletions queue/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Node:
def __init__(self,data):
self.data = data
self.next = None

class LinkedList:
def __init__(self,head,tail):
self.head = head
self.tail = tail
self.length = 0

def add_to_head(self,data):
newnode = Node(data)
if not self.head and not self.tail:
self.head = newnode
self.tail = newnode
self.length += 1
else:
newnode.next = self.head
self.head = newnode

def add_to_tail(self,data):
newnode = Node(data)
if not self.head and not self.tail:
self.head = newnode
self.tail = newnode
self.length += 1
else:
self.tail.next = newnode
self.tail = newnode
self.length += 1

def remove_from_head(self):
if self.length == 0:
return None
elif self.head == self.tail:
value = self.head.data
self.head = None
self.tail = None
self.length -= 1
return value
else:
value = self.head.data
self.head = self.head.next
self.length -= 1
return value

def remove_tail(self):
if self.length == 0:
return None
elif self.head == self.tail:
value = self.head.data
self.head = None
self.tail = None
self.length -= 1
return value
else:
value = self.tail.data
currentnode = self.head
while currentnode.next != self.tail:
currentnode = currentnode.next
self.tail = currentnode
self.length -= 1
return value

def len(self):
return self.length


def view(self):
list = []
currentnode = self.head
while currentnode != self.tail:
list.append(str(currentnode.data))
currentnode = currentnode.next
list.append(str(currentnode.data))
return '-->'.join(list)
Loading