forked from AdaGold/list-implementations
-
Notifications
You must be signed in to change notification settings - Fork 22
linked list methods working. #9
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
jlikesplants
wants to merge
1
commit into
Ada-C5:master
Choose a base branch
from
jlikesplants: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 |
|---|---|---|
| @@ -1,100 +1,129 @@ | ||
| # Quick Example of a Self Referential Data Structure in Ruby | ||
| # NODE -> contains a value and a pointer to (next_node) | ||
| # LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list | ||
|
|
||
| class 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 | ||
| 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) | ||
| # Quick Example of a Self Referential Data Structure in Ruby | ||
| # NODE -> contains a value and a pointer to (next_node) | ||
| # LinkedList -> This class holds the linked list functions - adding a node, traversing and displaying the linked list | ||
|
|
||
| class Node | ||
| attr_accessor :value, :next_node | ||
|
|
||
| def initialize(val,next_in_line = nil) | ||
| @value = val | ||
| @next_node = next_in_line | ||
| puts "Initialized a Node with value: " + value.to_s | ||
| end | ||
| end | ||
|
|
||
| class LinkedList | ||
| attr_accessor :head | ||
| 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 | ||
| until current == nil | ||
| if current.value == key | ||
| return true | ||
| else | ||
| current = current.next_node | ||
| end | ||
| end | ||
| false | ||
| end | ||
|
|
||
| def size | ||
| 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 | ||
| acc = 0 | ||
| until current == nil | ||
| current = current.next_node | ||
| acc += 1 | ||
| end | ||
| end | ||
| acc | ||
|
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. ditto here |
||
| end | ||
|
|
||
| def display | ||
| # Traverse through the list till you hit the "nil" at the end | ||
| def max | ||
| current = @head | ||
| full_list = [] | ||
| while current.next_node != nil | ||
| full_list += [current.value.to_s] | ||
| max = 0 | ||
| until current == nil | ||
| if current.value > max | ||
| max = current.value | ||
| current = current.next_node | ||
| else | ||
| current = current.next_node | ||
| end | ||
| end | ||
| full_list += [current.value.to_s] | ||
| puts full_list.join("->") | ||
| end | ||
|
|
||
| def include?(key) | ||
| end | ||
|
|
||
| def size | ||
| end | ||
|
|
||
| def max | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Initializing a Linked List with a node containing value (5) | ||
| ll = LinkedList.new(5) | ||
|
|
||
| # Adding Elements to Linked List | ||
| ll.add(10) | ||
| ll.add(20) | ||
|
|
||
| # Display the Linked List | ||
| puts "Displaying Linked List:" | ||
| ll.display | ||
|
|
||
| puts "Delete 10 and then display the linked list:" | ||
| ll.delete(10) | ||
| ll.display | ||
|
|
||
| =begin | ||
| Output: | ||
| Initialized a Node with value: 5 | ||
| Initialized a Node with value: 10 | ||
| Initialized a Node with value: 20 | ||
| Displaying Linked List: | ||
| 5->10->20 | ||
| Delete 10 and then display the linked list: | ||
| 5->20 | ||
| =end | ||
| max | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Initializing a Linked List with a node containing value (5) | ||
| # ll = LinkedList.new(5) | ||
| # | ||
| # # Adding Elements to Linked List | ||
| # ll = LinkedList.new(5) | ||
| # ll.add(10) | ||
| # ll.add(20) | ||
| # | ||
| # # Display the Linked List | ||
| # puts "Displaying Linked List:" | ||
| # ll.display | ||
| # | ||
| # puts "Delete 10 and then display the linked list:" | ||
| # ll.delete(10) | ||
| # ll.display | ||
|
|
||
| # =begin | ||
| # Output: | ||
| # Initialized a Node with value: 5 | ||
| # Initialized a Node with value: 10 | ||
| # Initialized a Node with value: 20 | ||
| # Displaying Linked List: | ||
| # 5->10->20 | ||
| # Delete 10 and then display the linked list: | ||
| # 5->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.
I know ruby says this is ok, but i think it's weird to leave off the
return