Jason and Michael Data Structures#1
Jason and Michael Data Structures#1MichaelWhalonCS wants to merge 1 commit intolimaplatoon:masterfrom
Conversation
GusPetito
left a comment
There was a problem hiding this comment.
Hello! I looked over your code and left a few suggestions. You don't have to implement these and this doesn't affect your grades. They're just here for you to read over if you want to
| def __init__(self, head=None): | ||
| self.head = head | ||
| self.length = 1 | ||
| print(self.head) |
There was a problem hiding this comment.
It's probably best practice to remove unnecessary print statements before requesting to pull
There was a problem hiding this comment.
Hey, I just saw these! Thank you for the feedback, these all help a lot.
| class LinkList(): | ||
| def __init__(self, head=None): | ||
| self.head = head | ||
| self.length = 1 |
There was a problem hiding this comment.
Since head defaults to none, it's possible to start with a length of 0, so something with the ternary operator like:
| self.length = 1 | |
| self.length = 0 if head == None else 1 |
may work
| pass | ||
| self.length += 1 | ||
| #for node in range(self.length-1) | ||
| node = self.head |
There was a problem hiding this comment.
Don't forget to check if self.head is None or not! Currently, making a LinkList with the default head value of None and adding to it would break the program, since you'd be calling None.next
| def remove(self, data): | ||
| # write your code to REMOVE an element from the Linked List | ||
| pass | ||
| self.length -=1 |
There was a problem hiding this comment.
Don't forget to check if the length is equal to 0! You don't want to be having a length of -1
| a = Node('a') | ||
| b = Node('b') | ||
| c = Node('c') | ||
| d = Node('d') | ||
|
|
||
| test = LinkList(a) | ||
| test.add(b) | ||
| test.add(c) | ||
| test.add(d) | ||
|
|
||
|
|
||
| print(test.head.value) | ||
| print(test.head.next.value) | ||
| print(test.head.next.next.value) | ||
| print(test.head.next.next.next.value) | ||
|
|
||
| print(test.get(c)) | ||
| print('-'*25) |
There was a problem hiding this comment.
I like that you're testing out your LinkList, but it's best form to delete these before pull requesting, or at least putting them inside a if(name == 'main') statement so it doesn't run if someone wants to import your LinkList
There was a problem hiding this comment.
Also, this type of testing is perfect for a separate unittest file, and ideally that's where it should be. But I understand if you were running out of time and just wanted a quick and dirty way to test.
| self.queue.pop(0) | ||
| self.length -= 1 |
There was a problem hiding this comment.
Don't forget to check if the length is already 0!
| self.stack.pop(0) | ||
| self.length -= 1 |
There was a problem hiding this comment.
Don't forget to check if self.length = 0!
No description provided.