Stacks and Queues Flashcards

1
Q

Stacks

For stacks

What order is the data inserted in?

Where are insertions and deletions made?

A

Are last in, first out (LIFO) linear data structures
* In other words, reverse order of insertion!

Can only be accessed at the “top” using push and pop operations

A structure where insertions and deletions are made at the same location in the data structure (on one side).

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

Stacks

When pushing, what happens if the stack is full?

When popping what happens when the stack is empty?

A

You cannot push when the stack is full.

You cannot pop an empty stack.

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

Stacks

What are the other operations (other than pushing and popping) for a stack?

A

peek: Return the top element without popping.

search: returns the position where ab object is on the stack. (returns -1 if not found)

clear: clears the entire stack.

full: Check is the stack is full

empty: checks if empty

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

What are the possible stack applications.

A

Function calls and recursion

Undo and Redo (text editor)

Expressions for parsing

Backtracking (Go back and forth in browsers)

Game (a game that follows stack behaviour)

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

Stacks

When implementing stacks with arrays, what must the variable point to?

What else do we need to consider?

A

Must use a variable to point to the top
* Will initially be set to -1, to indicate an empty stack e.g. top = -1

Will have a maximum size
* Unless a resizable array (list) is used

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

Stacks

For a stack, how will push and pop be implemented with an array

A

push:
* Increment top and store the element at that position in the array
e.g.
top += 1
array[top] = elementValue

pop:
* Copy the top element in the array to a temporary variable
* Decrement top
e.g.
temp = array[top]
top -= 1

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

Stacks

When implementing a stack with a linked list, what are some of the advantages over using an array.

How does push and pop work?

A

Unlike arrays there is no max size.

push: inserts the element at the head of the list.

pop: copy the element at the head of the list, delete the node, then return the element.

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

Stacks

When using stacks in general, what is the complexity for push and pop?

A

It is O(1) regardless of whether it is in an array or a linked list.

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

Queues

What are queues. How does data enter and leave a queue?

How are elements accessed?

A

They are FIFO. First in, First out.

Elements are accessed at the head and the tail of the list.

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

Queues

What are the basic operations of a queue?

What happens when you try to enqueue a full queue?

What happens when you try and dequeue from an empty queue?

A

Enqueue: add an element to the end of the list. You cannot add to a full queue.

Dequeue: delete and return the element at the beginning of the list. You cannot dequeue from an empty queue.

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

Queue

How are queues implemented using an array?

What indicates an empty queue?

A

We use two variables to point to the beginning and the end.
* The head index is incremented after dequeuing
* The tail index is incremented before enqueuing

Heda and tail are set to -1 to indicate an empty queue.

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

Queue

In an queue array implementation how might we enqueue and dequeue.

A

Enqueue:
If the queue is empty, head and tail = 0.
Otherwise increment tail mod n
tail += 1
tail % n
set array[tail] to element value.

Dequeue:
Store array[head] to a temporary variable.
If only one element in the queue (head == tail), then set head and tail = -1.
Else, increment head mod n
head += 1
head % n
return the value in the temporary value.

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

Queue

In a queue linked list implementation, how do we enqueue and dequeue?

A

Insert the element at the tail of the list.

Copy the element at the head of the list, delete and the node, then return the element.

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

Queues

In general, what is the complexity of enqueue and dequeue.

A

They are O(1).

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

Priority Queues

What is a priority queue?

A

Each element has an associated priority. The smallest value means the highest priority.

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

When should we use arrays and linked lists.

A

You will use arrays when
* Random access
* Predefined data size
* Very little insertion and deletion is expected.
* Need to use less space per element

Linked List
* Dyanmic data size
* Multiple insertions and deletions are expected
* You do not need random access to data.

17
Q

When should we use stacks vs queues vs linked lists.

A

Stack
* Data needs to be accessed in reverse order.

Queue
* Data needs to be accessed in the same order

Use them over linked list when
* No insertions in the middle take place.
* No sorting is required