Last active
August 11, 2016 05:47
-
-
Save astronomy88/2f33f0a976d091f51d7f4e8abe390e09 to your computer and use it in GitHub Desktop.
Queue implementation with linked lists
This file contains 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
require './node' | |
class QueueLinkedList | |
attr_reader :length | |
def initialize(item) | |
@q = Node.new item | |
@q.next = nil | |
@current = @q | |
#-- @current holds the item last queued, to keep track of the end of the queue | |
#-- assign @current to the initial node | |
@length = 1 | |
end | |
def enqueue item | |
if @q.nil? | |
initialize item | |
else | |
#-- Grab the last node from previous time | |
@last = @current | |
@length += 1 | |
#-- Create the new node | |
temp = Node.new(item) | |
#-- Point the old node to the new node | |
@last.next = temp | |
#-- Save the current node for next time - reassigning @current now | |
@current = temp | |
end | |
end | |
def dequeue | |
if @length == 0 | |
return nil | |
end | |
@length -= 1 | |
#-- We are attempting to peel off from the first | |
first = @q.item | |
@q = @q.next | |
first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment