Created
March 24, 2020 02:13
-
-
Save wyy1234567/271c053ee5f01bd3b317bca668c81fec to your computer and use it in GitHub Desktop.
create LinkedList class
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
#linked list is composed nodes, each node has a data, than it points to the next node | |
class LinkedList | |
#is_empty?: return true if the linked list is empty | |
def is_empty? | |
if @head == nil | |
return true | |
else | |
return false | |
end | |
end | |
#push: given a data, add a new node in the end | |
def push(data) | |
if self.is_empty? | |
@head = Node.new(data) | |
else | |
current_node = @head | |
new_node = Node.new(data) | |
while current_node.next != nil | |
current_node = current_node.next | |
end | |
current_node.next = new_node | |
end | |
end | |
#unshift: add a new node in the front | |
def unshift(data) | |
if self.is_empty? | |
@head = Node.new(data) | |
else | |
curr_head = Node.new(data) | |
curr_head.next = @head | |
@head = curr_head | |
end | |
end | |
#pop: remove the last node and return it | |
def pop | |
if self.is_empty? | |
return "Can't pop the element, this list is currently empty" | |
else | |
current_node = @head | |
while current_node.next.next != nil | |
current_node = current_node.next | |
end | |
last_node = current_node.next | |
current_node.next = nil | |
end | |
last_node | |
end | |
#shift: remove the first node and return it | |
def shift | |
if self.is_empty? | |
return "This list is currently empty" | |
else | |
curr_head = @head | |
new_head = @head.next | |
@head.next = nil | |
@head = new_head | |
end | |
curr_head | |
end | |
#size: return the length of linked list | |
def size | |
if self.is_empty? | |
count = 0 | |
else | |
count = 1 | |
current_node = @head | |
while current_node.next != nil | |
current_node = current_node.next | |
count += 1 | |
end | |
end | |
count | |
end | |
#pretty_print: print the current linked list as an array | |
def pretty_print | |
array = [] | |
current_node = @head | |
if self.is_empty? | |
return array | |
else | |
while current_node.next != nil | |
array << current_node.data | |
current_node = current_node.next | |
end | |
array << current_node.data | |
end | |
array | |
end | |
#clear: clear the whole linked list | |
def clear | |
@head = nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment