Skip to content

Conversation

@theomoondev
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 Abstract Data Type is a data structure described by the set of operations it can perform, the details of which are hidden.
Describe a Stack A Stack is an ADT that stores data in LIFO order (the last element in will always be the first element removed).
What are the 5 methods in Stack and what does each do? push(element): Adds an element to the top of the stack; pop: Removes and returns the element on top of the stack; is_empty: Returns true is the stack has no elements, and false otherwise; peek or top: Returns but does not remove the top element of the stack; size: Returns the number of elements on the stack
Describe a Queue A Queue is an ADT that stores data in FIFO order (the first element in will always be the first element removed).
What are the 5 methods in Queue and what does each do? enqueue(element): Puts an element at the back of the queue; dequeue: Removes and returns the element at the front of the queue; is_empty: Returns true if the queue is empty and false otherwise; front: Returns the element at the front, but leaves the queue unchanged; size: Returns the number of elements in the queue.
What is the difference between implementing something and using something? Implementing something refers to creating something but does not specify the means of creation, while using something refers to using something as a means of accomplishing a purpose (like using a LinkedList to create a Stack).

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 Lee. You hit all the learning goals here. Well done. Let me know if you have any questions.

Comment on lines +3 to 5
# Time Complexity: O(n), where n is the length of the string
# Space Complexity: O(n)
def balanced(string)

Choose a reason for hiding this comment

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

👍

@rear = -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 +17 to +23
elsif @rear == @size - 1 && @front != 0 # rear needs to wrap around
@rear = 0
@store[@rear] = element
else
@rear += 1
@store[@rear] = element
end

Choose a reason for hiding this comment

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

This could be simplified a bit.

Suggested change
elsif @rear == @size - 1 && @front != 0 # rear needs to wrap around
@rear = 0
@store[@rear] = element
else
@rear += 1
@store[@rear] = element
end
else
@rear = (@rear + 1) % @store.length
@store[@rear] = element
end

Comment on lines +36 to +40
elsif @front == @size - 1 # front needs to wrap around
@front = 0
else
@front += 1
end

Choose a reason for hiding this comment

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

See above for a way to simplify this.

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