-
Notifications
You must be signed in to change notification settings - Fork 86
C16 Cedar LinkedLists #60
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Node: | ||
def __init__(self, value): | ||
self.data = value | ||
self.next = None |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,75 +1,136 @@ | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Defines a node in the singly linked list | ||||||||||||||||||||||
class Node: | ||||||||||||||||||||||
from platform import node | ||||||||||||||||||||||
#from sys import last_value | ||||||||||||||||||||||
|
||||||||||||||||||||||
class Node: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||
def __init__(self, value, next_node = None): | ||||||||||||||||||||||
self.value = value | ||||||||||||||||||||||
self.next = next_node | ||||||||||||||||||||||
|
||||||||||||||||||||||
# Defines the singly linked list | ||||||||||||||||||||||
class LinkedList: | ||||||||||||||||||||||
def __init__(self): | ||||||||||||||||||||||
self.head = None # keep the head private. Not accessible outside this class | ||||||||||||||||||||||
|
||||||||||||||||||||||
self.head = None # keep the head private. Not accessible outside this class | ||||||||||||||||||||||
# returns the value in the first node | ||||||||||||||||||||||
# returns None if the list is empty | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(1) | ||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def get_first(self): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||
pass | ||||||||||||||||||||||
|
||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
return self.head.value | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method to add a new node with the specific data value in the linked list | ||||||||||||||||||||||
# insert the new node at the beginning of the linked list | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(1) | ||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def add_first(self, value): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||
pass | ||||||||||||||||||||||
self.head = Node(value, self.head) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method to find if the linked list contains a node with specified value | ||||||||||||||||||||||
# returns true if found, false otherwise | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(n) | ||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def search(self, value): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||
pass | ||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||
while current != None: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can also shorten this to:
Suggested change
|
||||||||||||||||||||||
if current.value == value: | ||||||||||||||||||||||
return True | ||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method that returns the length of the singly linked list | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(n) | ||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def length(self): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||
pass | ||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||
count = 0 | ||||||||||||||||||||||
while current: | ||||||||||||||||||||||
count += 1 | ||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||
return count | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method that returns the value at a given index in the linked list | ||||||||||||||||||||||
# index count starts at 0 | ||||||||||||||||||||||
# returns None if there are fewer nodes in the linked list than the index value | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(n) | ||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def get_at_index(self, index): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||||||||||||||||||
pass | ||||||||||||||||||||||
current = self.head #initialise temp | ||||||||||||||||||||||
count = 0 #index of current node | ||||||||||||||||||||||
while (current): | ||||||||||||||||||||||
if (count == index): | ||||||||||||||||||||||
Comment on lines
+64
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops! Looks like JavaScript crept in here
Suggested change
|
||||||||||||||||||||||
return current.value | ||||||||||||||||||||||
count +=1 | ||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method that returns the value of the last node in the linked list | ||||||||||||||||||||||
# returns None if the linked list is empty | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(1) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be O(n). The while loop is dependent upon the length of the linked list (ie, the input) to find the last node's value |
||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def get_last(self): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
while current.next != None: | ||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||
return current.value | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method that inserts a given value as a new last node in the linked list | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(1) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be O(n). The while loop is dependent upon the length of the linked list (ie, the input) to find the last node, and then add the new node to it |
||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def add_last(self, value): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
|
||||||||||||||||||||||
new_node = Node(value) | ||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||
self.head = new_node | ||||||||||||||||||||||
return | ||||||||||||||||||||||
last = self.head | ||||||||||||||||||||||
while (last.next): | ||||||||||||||||||||||
last = last.next | ||||||||||||||||||||||
last.next = new_node | ||||||||||||||||||||||
|
||||||||||||||||||||||
# method to return the max value in the linked list | ||||||||||||||||||||||
# returns the data value and not the node | ||||||||||||||||||||||
|
||||||||||||||||||||||
def find_max(self): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
head = self.head | ||||||||||||||||||||||
if (self.head == None): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
return None | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
max = head.value | ||||||||||||||||||||||
|
||||||||||||||||||||||
while (head != None): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
if head.value > max: | ||||||||||||||||||||||
max = head.value | ||||||||||||||||||||||
head = head.next | ||||||||||||||||||||||
return max | ||||||||||||||||||||||
#print("Maximum value node in the list: " + str(max)) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
# method to delete the first node found with specified value | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
# Time Complexity: O(n) | ||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||
def delete(self, value): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
if self.head is None: | ||||||||||||||||||||||
return | ||||||||||||||||||||||
|
||||||||||||||||||||||
if self.head.value == value: | ||||||||||||||||||||||
self.head = self.head.next | ||||||||||||||||||||||
return | ||||||||||||||||||||||
|
||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||
Comment on lines
+124
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have this twice
Suggested change
|
||||||||||||||||||||||
while current.next is not None: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
if value == current.next.value: | ||||||||||||||||||||||
break | ||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||
if current.next is None: | ||||||||||||||||||||||
print("Node not found") | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
current.next = current.next.next | ||||||||||||||||||||||
Comment on lines
+127
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a couple if statements here that aren't really doing anything but stop the loop or print. It makes it hard to read what is important here. I think we can shorten it:
Suggested change
Since there could be more than one node with the same value, we don't want to |
||||||||||||||||||||||
|
||||||||||||||||||||||
# method to print all the values in the linked list | ||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
|
@@ -89,7 +150,14 @@ def visit(self): | |||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||
Comment on lines
150
to
151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||
def reverse(self): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
prev = None | ||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||
while current is not None: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
next = current.next | ||||||||||||||||||||||
current.next = prev | ||||||||||||||||||||||
prev = current | ||||||||||||||||||||||
current = next | ||||||||||||||||||||||
self.head = prev | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Advanced/ Exercises | ||||||||||||||||||||||
# returns the value at the middle element in the singly linked list | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this coming from? You've created your own
Node
class on line 6, so you don't need to import a third-party class