-
Notifications
You must be signed in to change notification settings - Fork 48
Time - Jocelyn #23
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?
Time - Jocelyn #23
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 |
|---|---|---|
|
|
@@ -2,87 +2,169 @@ | |
| # Defines a node in the singly linked list | ||
| class Node | ||
| attr_reader :data # allow external entities to read value but not write | ||
| attr_accessor :next # allow external entities to read or write next node | ||
| attr_accessor :next, :previous # allow external entities to read or write next node | ||
|
|
||
| def initialize(value, next_node = nil) | ||
| def initialize(value, next_node = nil, previous_node = nil) | ||
| @data = value | ||
| @previous = previous_node | ||
| @next = next_node | ||
| end | ||
| end | ||
|
|
||
| # Defines the singly linked list | ||
| class LinkedList | ||
| def initialize | ||
| @head = nil # keep the head private. Not accessible outside this class | ||
| @head = nil | ||
| @tail = nil | ||
| end | ||
|
|
||
| # 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(value) | ||
| raise NotImplementedError | ||
| if @head.nil? | ||
| @head = @tail = Node.new(value) | ||
| else | ||
| new_node = Node.new(value, @head) | ||
| @head.previous = new_node | ||
| @head = new_node | ||
| end | ||
| end | ||
|
|
||
| # 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(value) | ||
|
Comment on lines
+37
to
39
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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
|
|
||
| while current != nil | ||
| return true if current.data == value | ||
| current = current.next | ||
| end | ||
|
|
||
| return false | ||
| end | ||
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max | ||
|
Comment on lines
50
to
52
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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
| return nil if current == nil | ||
|
|
||
| max = current.data | ||
|
|
||
| while current != nil | ||
| max = current.data if current.data > max | ||
| current = current.next | ||
| end | ||
|
|
||
| return max | ||
| end | ||
|
|
||
| # method to return the min value in the linked list | ||
| # returns the data value and not the node | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def find_min | ||
|
Comment on lines
+68
to
70
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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
| return nil if current == nil | ||
|
|
||
| min = current.data | ||
| while current != nil | ||
| min = current.data if current.data < min | ||
| current = current.next | ||
| end | ||
|
|
||
| return min | ||
| end | ||
|
|
||
|
|
||
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def length | ||
|
Comment on lines
+85
to
87
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. 👍 |
||
| raise NotImplementedError | ||
| current, length = @head, 0 | ||
|
|
||
| while current != nil | ||
| length += 1 | ||
| current = current.next | ||
| end | ||
|
|
||
| return length | ||
| end | ||
|
|
||
| # method that returns the value at a given index in the linked list | ||
| # index count starts at 0 | ||
| # returns nil 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(index) | ||
|
Comment on lines
+101
to
103
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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
| current_index = 0 | ||
|
|
||
| return nil if current == nil | ||
|
|
||
| while current != nil | ||
| return current.data if current_index == index | ||
| current_index += 1 | ||
| current = current.next | ||
| end | ||
|
|
||
| return nil | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def visit | ||
|
Comment on lines
+119
to
121
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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
|
|
||
| while current != nil | ||
| puts current.data | ||
| current = current.next | ||
| end | ||
| end | ||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(1) | ||
| # Space Complexity: O(1) | ||
| def delete(value) | ||
|
Comment on lines
+131
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. 👍 Well done, but what if |
||
| raise NotImplementedError | ||
| current = @head | ||
|
|
||
| while current != nil | ||
| if current.data == value | ||
| if current.previous != nil | ||
| current.previous.next = current.next | ||
| else | ||
| @head = current.next | ||
| @head.previous = nil | ||
| end | ||
| end | ||
|
|
||
| current = current.next | ||
| end | ||
| end | ||
|
|
||
| # method to reverse the singly linked list | ||
| # note: the nodes should be moved and not just the values in the nodes | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def reverse | ||
|
Comment on lines
+152
to
154
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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
|
|
||
| while current != nil | ||
| temp = current.previous | ||
| current.previous = current.next | ||
| current.next = temp | ||
|
|
||
| current = current.previous | ||
| end | ||
|
|
||
| temp = @head | ||
| @head = @tail | ||
| @tail = temp | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -118,7 +200,7 @@ def has_cycle | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_first | ||
| raise NotImplementedError | ||
| @head ? @head.data : nil | ||
| end | ||
|
|
||
| # method that inserts a given value as a new last node in the 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.
👍