-
Notifications
You must be signed in to change notification settings - Fork 42
Erica #18
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
base: master
Are you sure you want to change the base?
Erica #18
Changes from all commits
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,55 @@ | ||
| QUEUE_SIZE = 20 | ||
|
|
||
| class Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(QUEUE_SIZE) | ||
| @front = @rear = -1 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 #Q is empty | ||
| @rear = 1 | ||
| @front = 0 | ||
| @store[@front] = element | ||
| elsif @front == @rear | ||
| raise ArgumentError, "Queue is full" | ||
| else | ||
| new_rear = (@rear + 1) % QUEUE_SIZE | ||
| @store[@rear] = element | ||
| @rear = new_rear | ||
| end | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 | ||
| raise ArgumentError, "Queue is empty" | ||
| else | ||
| dequeued = @store[@front] | ||
| @store[@front] = nil | ||
| @front = (@front + 1) % QUEUE_SIZE | ||
| return dequeued | ||
| end | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store[@front] | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.compact.length | ||
|
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 is a bit expensive. You could also calculate the size based on the locations of front and rear. |
||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| queue = @store.compact | ||
|
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 is a pretty expensive way to see if the queue is empty. Why not just |
||
| if queue.length == 0 | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return @store.compact.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,46 @@ | ||
| class Stack | ||
| STACK_SIZE = 20 | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(QUEUE_SIZE) | ||
| @front = @rear = -1 | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 | ||
|
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. You don't really need a circular buffer for a Stack as you only add or remove on one end (the top). Instead I'd asked you to use a Linked List. So this is a little weird. |
||
| @rear = 1 | ||
| @front = 0 | ||
| @store[@front] = element | ||
| elsif @front == @rear | ||
| raise ArgumentError, "Stack is full" | ||
| else | ||
| new_rear = (@rear + 1) % STACK_SIZE | ||
| @store[@rear] = element | ||
| @rear = new_rear | ||
| end | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 | ||
| raise ArgumentError, "Stack is empty" | ||
| else | ||
| popped = @store[@rear - 1] | ||
| @store[@rear - 1] = nil | ||
| @rear = (@rear - 1) % STACK_SIZE | ||
| return popped | ||
| end | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| stack = @store.compact | ||
| if stack.length == 0 | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return @store.compact.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.
You should also check to see if the queue is now empty.