forked from AdaGold/list-implementations
-
Notifications
You must be signed in to change notification settings - Fork 22
Jillian - Finished methods for linked lists. #6
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
Open
JBoshart
wants to merge
1
commit into
Ada-C5:master
Choose a base branch
from
JBoshart:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,74 +3,102 @@ | |
| # LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list | ||
|
|
||
| class Node | ||
| attr_accessor :value, :next_node | ||
| attr_accessor :value, :next_node | ||
|
|
||
| def initialize(val,next_in_line=null) | ||
| @value = val | ||
| @next_nodex = next_in_line | ||
| puts "Initialized a Node with value: " + value.to_s | ||
| end | ||
| def initialize(val,next_in_line=nil) | ||
| @value = val | ||
| @next_node = next_in_line | ||
| puts "Initialized a Node with value: " + val.to_s | ||
| end | ||
| end | ||
|
|
||
| class LinkedList | ||
| def initialize(val) | ||
| # Initialize a new node at the head | ||
| @head = Node.new(val,nil) | ||
| end | ||
|
|
||
| def add(value) | ||
| # Traverse to the end of the list | ||
| # And insert a new node over there with the specified value | ||
| current = @head | ||
| while current.next_node != nil | ||
| current = current.next_node | ||
| end | ||
| current.next_node = Node.new(value,nil) | ||
| self | ||
| end | ||
|
|
||
| def delete(val) | ||
| current = @head | ||
| if current.value == val | ||
| # If the head is the element to be delete, the head needs to be updated | ||
| @head = @head.next_node | ||
| else | ||
| # ... x -> y -> z | ||
| # Suppose y is the value to be deleted, you need to reshape the above list to : | ||
| # ... x->z | ||
| # ( and z is basically y.next_node ) | ||
| current = @head | ||
| while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val) | ||
| current = current.next_node | ||
| end | ||
|
|
||
| if (current != nil) && (current.next_node != nil) | ||
| current.next_node = (current.next_node).next_node | ||
| end | ||
| end | ||
| #In a LinkedList each node only knows what the node in front of it is, it does not know what the node behind it was, or what the last node is. It only knows about the next node. | ||
| def initialize(val) | ||
| # Initialize a new node at the head | ||
| @head = Node.new(val,nil) | ||
| end | ||
|
|
||
| def add(value) | ||
| # Traverse to the end of the list | ||
| # And insert a new node over there with the specified value | ||
| current = @head | ||
| while current.next_node != nil | ||
| current = current.next_node | ||
| end | ||
| current.next_node = Node.new(value,nil) | ||
| self | ||
| end | ||
|
|
||
| def delete(val) | ||
| current = @head | ||
| if current.value == val | ||
| # If the head is the element to be delete, the head needs to be updated | ||
| @head = @head.next_node | ||
| else | ||
| # ... x -> y -> z | ||
| # Suppose y is the value to be deleted, you need to reshape the above list to : | ||
| # ... x->z | ||
| # ( and z is basically y.next_node ) | ||
| current = @head | ||
| while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val) | ||
| current = current.next_node | ||
| end | ||
|
|
||
| if (current != nil) && (current.next_node != nil) | ||
| current.next_node = (current.next_node).next_node | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def display | ||
| # Traverse through the list till you hit the "nil" at the end | ||
| current = @head | ||
| full_list = [] | ||
| while current.next_node != nil | ||
| full_list += [current.value.to_s] | ||
| current = current.next_node | ||
| end | ||
| full_list += [current.value.to_s] | ||
| puts full_list.join("->") | ||
| end | ||
|
|
||
| def include?(key) | ||
| current = @head | ||
| while current != nil | ||
| if current.value != key | ||
| current = current.next_node | ||
| elsif current.value == key | ||
| puts "true" | ||
| return true | ||
| else | ||
| puts "false" | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| def display | ||
| # Traverse through the list till you hit the "nil" at the end | ||
| current = @head | ||
| full_list = [] | ||
| while current.next_node != nil | ||
| full_list += [current.value.to_s] | ||
| current = current.next_node | ||
| end | ||
| full_list += [current.value.to_s] | ||
| puts full_list.join("->") | ||
| end | ||
|
|
||
| def size | ||
| count = 1 | ||
| current = @head | ||
| while current.next_node != nil | ||
| count += 1 | ||
| current = current.next_node | ||
| end | ||
|
|
||
| def include?(key) | ||
| return count | ||
| end | ||
|
|
||
| def max | ||
| max = 0 | ||
| current = @head | ||
| while current != nil | ||
| if current.value > max | ||
| max = current.value | ||
| end | ||
| current = current.next_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. What will happen here if my list contains -5 --> -10 --> -1 --> nil |
||
| end | ||
|
|
||
| def size | ||
| end | ||
|
|
||
| def max | ||
| end | ||
|
|
||
| return max | ||
| end | ||
| end | ||
|
|
||
| # Initializing a Linked List with a node containing value (5) | ||
|
|
@@ -88,6 +116,16 @@ def max | |
| ll.delete(10) | ||
| ll.display | ||
|
|
||
| # Find element in list: | ||
| puts "Is 20 in the list?" | ||
| "#{ll.include?(20)}" | ||
|
|
||
| # Give the size of the list: | ||
| puts "The size of the list is #{ll.size}." | ||
|
|
||
| #Find the highest value in the list: | ||
| puts "The highest value in the list is #{ll.max}." | ||
|
|
||
| =begin | ||
| Output: | ||
| Initialized a Node with value: 5 | ||
|
|
@@ -97,4 +135,8 @@ def max | |
| 5->10->20 | ||
| Delete 10 and then display the linked list: | ||
| 5->20 | ||
| Is 20 in the list? | ||
| true | ||
| The size of the list is 2. | ||
| The highest value in the list is 20. | ||
| =end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What will get returned here if the LinkedList has 0 nodes?