Skip to content

Conversation

@seaweeddol
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 - a data structure that follows a set of rules/has a set of operations it can perform, but can be implemented with any underlying structure which can be hidden in implementation.
Describe a Stack A stack can be any data type as long as it follows Last In First Out rules. The first item in will be the last out, and the last item in will be the first out.
What are the 5 methods in Stack and what does each do? Push - add an item to the top of the stack. Pop - remove an item from the top of a stack. is_empty: returns true if stack is empty or false if not. Peek: returns the top item but does not remove it. Size: returns the number of items in the stack
Describe a Queue A queue can be any data type and follows First In First Out rules. So the first item in will always be the first item to be removed, then the second, and so on.
What are the 5 methods in Queue and what does each do? Enqueue: add an item to the back of the queue. Dequeue: remove an item from the front of a queue. is_empty: return true/false depending on if the queue is empty. Front: returns the first item in the queue
What is the difference between implementing something and using something? Implementing means you create the methods and know how they work in the background. Using something can be done without knowing anything about how the thing works.

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 Jessica. You hit all the learning goals here. Take a look at my comments and let me know what questions you have.

Comment on lines +16 to +28
case remove
when '['
return false if array[i] != ']'
when ']'
return false if array[i] != '['
when '{'
return false if array[i] != '}'
when '}'
return false if array[i] != '{'
when '('
return false if array[i] != ')'
when ')'
return false if array[i] != '('

Choose a reason for hiding this comment

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

Just a note that you can dry this up with a hash where they keys are the close braces and the values are the corresponding open braces.

return false if brace_hash[remove] != array.pop

Comment on lines +11 to +12
if (@front == 0 && @rear == (@max_size - 1)) ||
(@rear == ((@front - 1) % (@max_size - 1)))

Choose a reason for hiding this comment

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

Suggested change
if (@front == 0 && @rear == (@max_size - 1)) ||
(@rear == ((@front - 1) % (@max_size - 1)))
if (@front != -1 && @front == (@rear + 1) % @max_size

Comment on lines +14 to +22
elsif @front == -1 # Queue is empty
# initialize front and rear
@front += 1
@rear += 1
elsif (@rear == (@max_size - 1) && @front != 0) # rear needs to wrap around
@rear = 0
else
@rear += 1
end

Choose a reason for hiding this comment

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

This can be dried up with something like

Suggested change
elsif @front == -1 # Queue is empty
# initialize front and rear
@front += 1
@rear += 1
elsif (@rear == (@max_size - 1) && @front != 0) # rear needs to wrap around
@rear = 0
else
@rear += 1
end
else
@rear = (@rear + 1) % @max_size
end

@store[@rear] = element
end

def dequeue

Choose a reason for hiding this comment

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

👍

return dequeue
end

def front

Choose a reason for hiding this comment

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

👍

end

# get how many elements are in the array
def size

Choose a reason for hiding this comment

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

👍 , Nicely done

end
end

def empty?

Choose a reason for hiding this comment

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

👍

@@ -1,19 +1,18 @@
class Stack

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