Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ofer baharav module 2 sprint 2 #735

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
88 changes: 84 additions & 4 deletions doubly_linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ def __len__(self):
"""
def add_to_head(self, value):
pass
self.length +=1
node = ListNode(value)
if self.head == None:
self.head = node
self.tail = node
else:
node.next = self.head
self.head.prev = node
self.head = node


"""
Removes the List's current head node, making the
Expand All @@ -36,14 +46,32 @@ def add_to_head(self, value):
"""
def remove_from_head(self):
pass

if self.head == None:
return None
node = self.head
self.length -=1
self.head = node.next
if self.head == None:
self.tail = None
else:
self.head.prev = None
return node.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
self.length +=1
node = ListNode(value)
if self.tail == None:
self.head = node
self.tail = node
else:
node.prev = self.tail
self.tail.next = node
self.tail = node

"""
Removes the List's current tail node, making the
Expand All @@ -52,31 +80,83 @@ def add_to_tail(self, value):
"""
def remove_from_tail(self):
pass

if self.tail == None:
return None
node = self.tail
self.length -=1
self.tail = node.prev
print("Quote Tail", self.tail)
if self.tail == None:
self.head = None
else:
self.tail.next = None
return node.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

#disloging code:
if node.prev != None:
node.prev.next = node.next
if node.next != None:
node.next.prev = node.prev
if self.tail == node:
self.tail = node.prev

node.next = self.head
self.head.prev = node
self.head = node


"""
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
#disloging code:
if node.prev != None:
node.prev.next = node.next
if node.next != None:
node.next.prev = node.prev
if self.head == node:
self.head = node.next
node.next = None
node.prev = self.tail
self.tail.next = node
self.tail = node


"""
Deletes the input node from the List, preserving the
order of the other elements of the List.
"""
def delete(self, node):
pass
self.length -= 1

if node.prev != None:
node.prev.next = node.next
if node.next != None:
node.next.prev = node.prev
if self.head == node:
self.head = node.next
if self.tail == node:
self.tail = node.prev
"""
Finds and returns the maximum value of all the nodes
in the List.
"""
def get_max(self):
pass
pass
max_value = self.head.value
node = self.head
while node != None:
if node.value > max_value:
max_value = node.value
node = node.next
return max_value
15 changes: 15 additions & 0 deletions queue/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@
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


def dequeue(self):
pass
if self.size == 0:
return None
value = self.storage.remove_head()
self.size -=1
return value

#3. instead of using append and pop(0) on python lists
# I'm using add_to_tail and remove_head on linked LinkedListTests
61 changes: 61 additions & 0 deletions queue/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Node:
def __init__(self, value):
self.value = value
self.next_node = None

def __str__(self):
return f"value = {self.value} next_node = {self.next_node}"

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

def __str__(self):
return f"head={self.head} tail={self.tail}"

def add_to_tail(self, value):
node = Node(value)
if self.tail == None:
self.head = node
else:
self.tail.next_node = node

self.tail = node


def remove_head(self):
if self.head == None:
return None

node = self.head
self.head = self.head.next_node
if self.head == None:
self.tail = None

return node.value

def remove_tail(self):
if self.tail == None:
return None

value = self.tail.value
if self.head == self.tail:
self.head = None
self.tail = None
return value

node = self.head
while node.next_node != self.tail:
node = node.next_node

self.tail = node
self.tail.next_node = None

return value






61 changes: 61 additions & 0 deletions singly_linked_list/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Node:
def __init__(self, value):
self.value = value
self.next_node = None

def __str__(self):
return f"value = {self.value} next_node = {self.next_node}"

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

def __str__(self):
return f"head={self.head} tail={self.tail}"

def add_to_tail(self, value):
node = Node(value)
if self.tail == None:
self.head = node
else:
self.tail.next_node = node

self.tail = node


def remove_head(self):
if self.head == None:
return None

node = self.head
self.head = self.head.next_node
if self.head == None:
self.tail = None

return node.value

def remove_tail(self):
if self.tail == None:
return None

value = self.tail.value
if self.head == self.tail:
self.head = None
self.tail = None
return value

node = self.head
while node.next_node != self.tail:
node = node.next_node

self.tail = node
self.tail.next_node = None

return value






61 changes: 61 additions & 0 deletions stack/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Node:
def __init__(self, value):
self.value = value
self.next_node = None

def __str__(self):
return f"value = {self.value} next_node = {self.next_node}"

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

def __str__(self):
return f"head={self.head} tail={self.tail}"

def add_to_tail(self, value):
node = Node(value)
if self.tail == None:
self.head = node
else:
self.tail.next_node = node

self.tail = node


def remove_head(self):
if self.head == None:
return None

node = self.head
self.head = self.head.next_node
if self.head == None:
self.tail = None

return node.value

def remove_tail(self):
if self.tail == None:
return None

value = self.tail.value
if self.head == self.tail:
self.head = None
self.tail = None
return value

node = self.head
while node.next_node != self.tail:
node = node.next_node

self.tail = node
self.tail.next_node = None

return value






19 changes: 15 additions & 4 deletions stack/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@
3. What is the difference between using an array vs. a linked list when
implementing a Stack?
"""
from singly_linked_list import LinkedList

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

def __len__(self):
pass
return self.size

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


def pop(self):
pass
if self.size == 0:
return None
self.size-=1
return self.storage.remove_tail()

#3. The difference between using an array and using a stack is:
#in array you append and pop
#in linked lists you use add_to_tail and remove_tail
Loading