Skip to content

Conversation

@indiakato
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 an object that is defined by it's methods, the actual implementation is hidden.
Describe a Stack A stack stores data in a last in first out order.
What are the 5 methods in Stack and what does each do? 1. initialize - initializes the stack class and creates new LinkedList as the stack. 2. push - adds an element to the top of the stack. 3. pop - removes the first element from the top. 4. empty - checks to see if the stack is empty. 5. to_s - converts the stack into a string.
Describe a Queue A queue is a data structure that stores data in a first in first out order.
What are the 5 methods in Queue and what does each do? 1. initialize - initializes queue class, creates new array, sets front and back pointers to -1. 2. enqueue - adds an element to the front of the queue 3. dequeue - removes the first element in the queue. 4. front - returns the element at the front pointer of the queue. 5. empty - checks to see if the queue is empty 6. to_s - converts queue to a string
What is the difference between implementing something and using something? Implementing something creates a class that you can use elsewhere, using something is using that class to solve a problem.

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.

I had a few comments on the circular buffer-Queue, and let me know what questions you have. Otherwise this is well done. Nice work.

Comment on lines +12 to +22
if @front == 0 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1)
raise ArgumentError
elsif @front == -1
@front = 0
@back = 0
elsif @back == MAX_SIZE - 1 && @front != 0
@back = 0
else
@back += 1
end
@store[@back] = element

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 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1)
raise ArgumentError
elsif @front == -1
@front = 0
@back = 0
elsif @back == MAX_SIZE - 1 && @front != 0
@back = 0
else
@back += 1
end
@store[@back] = element
if @front == (@back + 1) % MAX_SIZE
raise ArgumentError, "Queue is full"
elsif @front == -1
@front = 0
end
@back = (@back + 1) % MAX_SIZE
@store[@back] = element


def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == 0 && @back == MAX_SIZE - 1 || @back == (@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.

This mostly works but look at the 2nd OR condition here.

def dequeue
raise NotImplementedError, "Not yet implemented"
return nil if @store.empty?
raise ArgumentError if @front == -1

Choose a reason for hiding this comment

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

@front is only going to be -1 if the queue is empty.

return @store[@front]
end

def size

Choose a reason for hiding this comment

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

👍

return @front < @back ? @back - @front + 1 : MAX_SIZE - @front + @back
end

def empty?

Choose a reason for hiding this comment

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

👍

def to_s
return @store.to_s
result_array = []
20.times do |i|

Choose a reason for hiding this comment

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

Do you need the magic number 20.times loop? Could you instead start i at @front and keep incrementing until you hit the back?

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