-
Notifications
You must be signed in to change notification settings - Fork 49
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
Branches - Diana Han #26
base: master
Are you sure you want to change the base?
Changes from all commits
bb7934d
f7e95ca
b28e968
19b32be
1f686f0
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,31 +1,61 @@ | ||||||||||
class Queue | ||||||||||
|
||||||||||
def initialize | ||||||||||
# @store = ... | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
@store = Array.new(50) | ||||||||||
@front = @back = -1 | ||||||||||
end | ||||||||||
|
||||||||||
def enqueue(element) | ||||||||||
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, "Not yet implemented" | ||||||||||
if @front == -1 | ||||||||||
@front = 0 | ||||||||||
@back = 1 | ||||||||||
@store[@front] = element | ||||||||||
elsif @front == @back | ||||||||||
raise ArgumentError | ||||||||||
else | ||||||||||
@store[@back] = element | ||||||||||
@back = (@back + 1 ) % @store.length | ||||||||||
end | ||||||||||
end | ||||||||||
|
||||||||||
def dequeue | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
def dequeue #remove and return from front | ||||||||||
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. 👍 |
||||||||||
if @front == -1 | ||||||||||
raise ArgumentError | ||||||||||
else | ||||||||||
element = @store[@front] | ||||||||||
@front = (@front + 1) % @store.length | ||||||||||
end | ||||||||||
|
||||||||||
if @front == @back | ||||||||||
@front = @back = -1 | ||||||||||
end | ||||||||||
|
||||||||||
return element | ||||||||||
end | ||||||||||
|
||||||||||
def front | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
@store[@front] | ||||||||||
end | ||||||||||
|
||||||||||
def size | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
@back - @front | ||||||||||
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. This works when front < back. What about when back has wrapped around the buffer. |
||||||||||
end | ||||||||||
|
||||||||||
def empty? | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
if @front == @back | ||||||||||
return true | ||||||||||
end | ||||||||||
Comment on lines
+45
to
+47
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.
Suggested change
|
||||||||||
end | ||||||||||
|
||||||||||
def to_s | ||||||||||
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 |
||||||||||
return @store.to_s | ||||||||||
# @store.delete(nil) | ||||||||||
# return @store.to_s | ||||||||||
list = [] | ||||||||||
current = @front | ||||||||||
while current != @back | ||||||||||
list << @store[current] | ||||||||||
current = (current + 1) % @store.length | ||||||||||
end | ||||||||||
return list.to_s | ||||||||||
end | ||||||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,57 @@ | ||
class Stack | ||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new | ||
end | ||
|
||
def push(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
@store << element | ||
end | ||
|
||
def pop | ||
raise NotImplementedError, "Not yet implemented" | ||
top_element = @store[-1] | ||
@store.delete_at(-1) | ||
|
||
return top_element | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
if @store.length == 0 | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
end | ||
def length | ||
return @store.length | ||
end | ||
end | ||
|
||
# USING LINKED LIST | ||
# class Stack | ||
# def initialize | ||
# @store = LinkedList.new | ||
# end | ||
|
||
# def push(element) | ||
# @store.add_front(element) | ||
# end | ||
|
||
# def pop | ||
# return nil if self.empty? | ||
|
||
# element = store.remove_front | ||
# return element | ||
# end | ||
|
||
# def empty? | ||
# return @store.empty? | ||
# end | ||
|
||
# def to_s | ||
# return @store.to_s | ||
# end | ||
# end |
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.
👍