Skip to content
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

Leaves-Yasmin #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Leaves-Yasmin #27

wants to merge 1 commit into from

Conversation

YasminM11
Copy link

Stacks and Queues

Thanks for doing some brain yoga. You are now submitting this assignment!

Comprehension Questions

Question Answer
What is an ADT? Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and a set of operations.
Describe a Stack A stack is an abstract data type, (Last in First out) that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed.
What are the 5 methods in Stack and what does each do? Push : Pushes an element on the top of the stack. Pop : Removes and returns the top element of the stack. Empty : It returns true if nothing is on the top of the stack ( if the stack is empty). Else, returns false. Peek : Returns the element on the top of the stack, but does not remove it.
Describe a Queue A Queue is an Abstract Data Structure which stores data and allows access in a First-In-First-Out order (FIFO)/last in last out.
What are the 5 methods in Queue and what does each do? enqueue: add an element to the rear of the queue, dequeue: Remove & return the element from the front, is_empty: returns true if the queue is empty
What is the difference between implementing something and using something? Implementing something: is to build it and know the how it works. Using something: using existing code

OPTIONAL JobSimulation

Question Answer
Did you include a sample run of your code as a comment?

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. One minor issue with the Queue, but otherwise excellent job. Take a look at my comments and let me know if you have questions.

@store = Array.new(100)
@front = @back = -1
# @front = -1
# @back = -1
end

def enqueue(element)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +32 to +34
if element = @store[@front]
@front = (@front + 1) % @store.length
return element

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is determining if the queue is empty or full because in either case front would be equal to back.

So

Suggested change
if element = @store[@front]
@front = (@front + 1) % @store.length
return element
if element = @store[@front]
@front = (@front + 1) % @store.length
@front = @back = -1 if @front == @back
return element

end

def size
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
return @store.length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only gives you the length of the internal storage, not the size of the queue.

Comment on lines +50 to +54
if @front == @back
return true
else
return false
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if @front == @back
return true
else
return false
end
return @front == @back

return true
else
return false
end
end

def to_s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants