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
106 changes: 102 additions & 4 deletions queue/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,114 @@
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!
"""

# class Node:
# def __init__(self, value=None, next_node=None):
# self.value=value
# self.next_node=next_node

# def get_value(self):
# return self.value

# def get_next_node(self):
# return self.next_node

# def set_next_node(self,next_new):
# self.next_node=next_new


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

# def add_to_head(self,value):
# #create a new Node
# new_node=Node(value)
# if self.head is None:
# #update the head and tail attributes
# self.head=new_node
# self.tail=new_node
# else:
# #set the next node of the new node to the head
# new_node.set_next_node(self.head)
# #update the head attribute
# self.head=new_node

# def add_to_tail(self,value):
# #create a new node
# new_node=Node(value)
# # if the linked list is empty (case 1)
# if self.head is None:
# #update the head and tail attributes
# self.head=new_node
# self.tail=new_node
# # the linked list is not empty (case 2)
# else:
# #update the next node of the tail
# self.tail.set_next_node(new_node)
# #update self.tail
# self.tail=new_node

# def remove_head(self):
# # if it is an empty list (case1)
# if self.head is None:
# return None
# # else return the old value of the head
# else:
# ret_value=self.head.get_value()
# #list with one element
# if self.head ==self.tail:
# self.head =None
# self.tail=None
# else:
# self.head=self.head.get_next_node()
# return ret_value

# def remove_tail(self):
# # if it is an empty list
# if self.head is None:
# return None
# else:
# ret_value=self.tail.get_value()
# #list with one element
# if self.head ==self.tail:
# self.head =None
# self.tail=None
# # if the list has more than 2 elements
# else:
# #if the next node is not the tail
# # assign the current_node to the next
# # set the current node to None by doing so the current node becomes the new tail
# current_node =self.head
# while current_node.next_node is not self.tail:
# current_node=current_node.next_node
# current_node.set_next_node(None)
# self.tail=current_node
# return ret_value


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

115 changes: 115 additions & 0 deletions queue/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# a class that represents the individual elements in our LL
class Node:
def __init__(self,value=None, next_node=None):
self.value=value
self.next_node=next_node

def get_value(self):
return self.value

def get_next_node(self):
return self.next_node

def set_next_node (self,new_next):
self.next_node=new_next

class LinkedList:
def __init__(self):
#what attributes do we need to create a LinkedList ?
self.head=None
self.tail=None

def add_to_head(self,value):
#create a new Node
new_node=Node(value)
if self.head is None:
#update head and tail attributes
self.head=new_node
self.tail=new_node
else:
#set next_node of my new Node to the head
new_node.set_next_node(self.head)
#update the head attribute
self.head=new_node




def add_to_tail(self,value):
#1. create a node from the value
new_node=Node(value)
# Whats the rule we want to set to indicate that the linked list is empty?
# wouldn't it be better to check the head ?

#case1: consider the LL being empty

if self.head is None:
#update the head and tail attributes
self.head=new_node
self.tail=new_node
#case2 : consider the LL not being empty
else:

self.tail.set_next_node(new_node)
#reassign self.tail to refer to the new node
self.tail=new_node

def remove_head(self):
# case1: empty list
if self.head is None:
return None
# return the value of the old head
else:
old_head=self.head.get_value()
if self.head==self.tail:
self.head==None
self.tail==None
else:
self.head=self.head.get_next_node()
return old_head

def remove_tail(self):
# if we have an emoty list we dont have to remove anything
if self.head is None :
return None
#else return value of the old head
else:
ret_value=self.tail.get_value()

#if we have a list with one element
if self.head==self.tail:
self.head=None
self.tail=None

#list with two elements
else:
# if current.next node is not the tail
# assign current to current.next.. then you set it to None making that the new tail
# return the current value

cur_node=self.head
while cur_node.get_next_node()is not self.tail:
cur_node=cur_node.get_next_node()
# keep going
#update pointer of temp node (prev_tail)None
cur_node.set_next_node(None)
self.tail=cur_node
#return value
return ret_value

def contains(self,value):
cur_node=self.head
while cur_node is not None:
# if value exists
if cur_node.get_value()==value:
return True

return False








116 changes: 116 additions & 0 deletions singly_linked_list/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# a class that represents the individual elements in our LL
class Node:
def __init__(self,value=None, next_node=None):
self.value=value
self.next_node=next_node

def get_value(self):
return self.value

def get_next_node(self):
return self.next_node

def set_next_node (self,new_next):
self.next_node=new_next

class LinkedList:
def __init__(self):
#what attributes do we need to create a LinkedList ?
self.head=None
self.tail=None

def add_to_head(self,value):
#create a new Node
new_node=Node(value)
if self.head is None:
#update head and tail attributes
self.head=new_node
self.tail=new_node
else:
#set next_node of my new Node to the head
new_node.set_next_node(self.head)
#update the head attribute
self.head=new_node




def add_to_tail(self,value):
#1. create a node from the value
new_node=Node(value)
# Whats the rule we want to set to indicate that the linked list is empty?
# wouldn't it be better to check the head ?

#case1: consider the LL being empty

if self.head is None:
#update the head and tail attributes
self.head=new_node
self.tail=new_node
#case2 : consider the LL not being empty
else:

self.tail.set_next_node(new_node)
#reassign self.tail to refer to the new node
self.tail=new_node

def remove_head(self):
# case1: empty list
if self.head is None:
return None
# return the value of the old head
else:
old_head=self.head.get_value()
if self.head==self.tail:
self.head=None
self.tail=None
return old_head
else:
self.head=self.head.get_next_node()
return old_head

def remove_tail(self):
# if we have an emoty list we dont have to remove anything
if self.head is None :
return None
#else return value of the old head
else:
ret_value=self.tail.get_value()

#if we have a list with one element
if self.head==self.tail:
self.head=None
self.tail=None

#list with two elements
else:
# if current.next node is not the tail
# assign current to current.next.. then you set it to None making that the new tail
# return the current value

cur_node=self.head
while cur_node.get_next_node()is not self.tail:
cur_node=cur_node.get_next_node()
# keep going
#update pointer of temp node (prev_tail)None
cur_node.set_next_node(None)
self.tail=cur_node
#return value
return ret_value

def contains(self,value):
cur_node=self.head
while cur_node is not None:
# if value exists
if cur_node.get_value()==value:
return True

return False








11 changes: 7 additions & 4 deletions stack/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
class Stack:
def __init__(self):
self.size = 0
# self.storage = ?
self.storage = []

def __len__(self):
pass
return len(self.storage)

def push(self, value):
pass
return self.storage.append(value)

def pop(self):
pass
if len(self.storage)==0:
return None
else:
return self.storage.pop()