Skip to content

Conversation

@mvlofthus
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? An ADT is an Abstract Data Type. The actual details of how the data is stored and implemented are hidden, but the operations are public.
Describe a Stack A stack is an abstract data type that operates on the last in first out principle, like a stack of paper, the element you can remove first is the element that is on top and was added last.
What are the 5 methods in Stack and what does each do? 1. push - adds an element to the top of the stack, 2. pop - removes the top element from the stack, 3. is_empty - tells whether there are any elements in the stack, 4. peek - similar to pop, but returns the value of the top element without removing it, 5. size - returns the number of elements in the stack
Describe a Queue A queue is an abstract data type that operates on the first in first out principle, like standing in line for a service, the first element you can remove is the element that was added first
What are the 5 methods in Queue and what does each do? 1. enqueue - adds an element to the back of the queue, 2. dequeue - removes an element from the front of the queue, 3. is_empty - tells whether there are any elements in the queue, 4. front - similar to dequeue, but returns the value of the first/front element without removing it, 5. size - returns the number of elements in the queue
What is the difference between implementing something and using something? Implementation is creating the data structure and behind the scenes methods users cannot see, while using it is calling those methods which is something users can see.

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 Mackenzie, you hit the learning goals here. Well done.

@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.

👍


end

def dequeue

Choose a reason for hiding this comment

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

👍


end

def front

Choose a reason for hiding this comment

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

👍

return @store.compact.length
end

def empty?

Choose a reason for hiding this comment

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

👍

return @store.compact.length == 0 ? true : false
end

def to_s

Choose a reason for hiding this comment

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

Just note that this will return the elements in the order they are in in the internal array, not in the queue order (i.e. the 1st element in the queue could be at index 3 and the last element of the queue at index 0.

@@ -1,19 +1,23 @@
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