Stacks and Queues Flashcards

1
Q

Describe a Stack

A

Container of objects with a last in first out (LIFO) policy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What ordering do stacks follow?

A

LIFO (last in first out)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What methods are needed to implement a stack?

A
  • push
  • pop
  • top
  • size
  • isEmpty
  • clear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What exception will the push method in stack throw?

A

StackOverflowException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What exception will the pop and top methods in stack throw?

A

StackEmptyException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some test cases for stack?

A
  • Creating a new stack
  • Push on element
  • Push on element then pop
  • Pop an empty stack
  • Push two elements
  • Clear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the two ways you could implement a stack?

A

Linked list

Unsorted arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

State some applications of stacks

A
  • Reversing a list
  • Evaluating reverse Polish expression
  • Executing programs
  • Web browser history
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write the basis of code for a pop method

A
if(isEmpty()){
   throw new StackEmptyException();
ListNode node = list.getNode(0);
list.remove(node);
return node.element;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write the basis of code for a push method

A

list.insertAfter(element,null)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the two main methods in stack?

A

Push and pop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the complexities of all the stack methods apart from clear?

A

Constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe a queue

A

A container of objects with a first in first out (FIFO) policy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the only available element in a queue?

A

The one added earliest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the only available element in a stack?

A

The first element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the methods required for implementing a queue?

A
enqueue
dequeue
front
size
isEmpty
clear
17
Q

State some of the test cases for JUnit testing of a queue

A
  • Create a new queue
  • Queue an element
  • Queue an element then remove
  • Remove from an empty queue
  • Queue two elements
  • Clear a queue
18
Q

What are the two ways that a queue can be implemented?

A

Linked list

Unsorted array

19
Q

Write the basis for an enqueue method

A
if(current_size < elements.length){
   elements[(front+current.size++)%elements.length] = element;
}else{
   throw new QueueFullException();
}
20
Q

Write the basis for a dequeue method

A
if(!isEmpty()){
   currentSize--;
   return elements[(front++ % elements.length)];
}else{
   throw new QueueEmptyException();
}
21
Q

Give some applications for queues

A
  • Decouple threads passing data between them
  • Producer/Consumer problems
  • Operating systems (ready_queue)
22
Q

What is the complexity of all the queue methods apart from clear?

A

Constant