Stacks and Queues - Chapter 3 Flashcards

1
Q

A blank is an ADT in which items are only inserted on or removed from the top

A

stack

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

The stack blank operation inserts an item on the top of the stack.

A

push

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

The stack blank operation removes and returns the item at the top of the stack.

A

pop

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

A stack is referred to as a blank ADT.

A

last-in first-out

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

A stack can be implemented using a blank, blank, or blank.

A

linked list, an array, or a vector

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

Inserts x on top of stack

A

Push(stack, x)

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

Returns and removes item at top of stack

A

Pop(stack)

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

Returns but does not remove item at top of stack

A

Peek(stack)

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

Returns true if stack has no items

A

IsEmpty(stack)

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

Returns the number of items in the stack

A

GetLength(stack)

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

blank and blank operations should not be applied to an empty stack; the resulting behavior may be undefined.

A

Pop and Peek

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

A stack is often implemented using a blank, with the list’s head node being the stack’s top.

A

linked list

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

A stack is often implemented using a linked list, with the list’s head node being the stack’s top. A blank is performed by creating a new list node, assigning the node’s data with the item, and prepending the node to the list. A blank is performed by assigning a local variable with the head node’s data, removing the head node from the list, and then returning the local variable.

A

push
pop

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

A stack can be implemented with an array. Two variables are needed in addition to the array:

A

allocationSize: an integer for the array’s allocated size.
length: an integer for the stack’s length.

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

An blank is a stack with no upper limit on length

A

unbounded stack

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

An unbounded stack’s blank can increase indefinitely, so the stack’s array allocation size must also be able to increase indefinitely.

A

length

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

A blank is a stack with a length that does not exceed a maximum value. The maximum is commonly the initial allocation size

A

bounded stack

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

A bounded stack with a length equal to the maximum length is said to be blank.

A

full

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

The Stack class implementation that follows uses a Python list. A Python list does not have a blank, so implementing reallocation is not needed. Therefore, implementation is simpler than an implementation with a fixed size array.

A

fixed size

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

A blank is an ADT in which items are inserted at the end of the queue and removed from the front of the queue

A

queue

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

The queue blank operation inserts an item at the end of the queue.

A

enqueue

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

The queue blank operation removes and returns the item at the front of the queue.

A

dequeue

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

A queue is referred to as a blank ADT.

A

first-in first-out

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

A queue can be implemented using a blank or a blank.

A

linked list or an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
The blank module implements multi-producer, multi-consumer queues.
queue
26
The queue module is especially useful in blank when information must be exchanged safely between multiple threads.
threaded programming
27
In a FIFO queue, the blank tasks added are the first retrieved.
first
28
In a LIFO queue, the blank added entry is the first retrieved (operating like a stack).
most recently
29
With a blank queue, the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first.
priority
30
Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
class queue.Queue(maxsize=0)
31
Constructor for a LIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
class queue.LifoQueue(maxsize=0)
32
Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.
class queue.PriorityQueue(maxsize=0)
33
Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking.
Class queue.SimpleQueue
34
Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.
exception queue.Empty
35
Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.
exception queue.Full
36
Return the approximate size of the queue. Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.
Queue.qsize()
37
Return True if the queue is empty, False otherwise. If empty() returns True it doesn’t guarantee that a subsequent call to put() will not block. Similarly, if empty() returns False it doesn’t guarantee that a subsequent call to get() will not block.
Queue.empty()
38
Return True if the queue is full, False otherwise. If full() returns True it doesn’t guarantee that a subsequent call to get() will not block. Similarly, if full() returns False it doesn’t guarantee that a subsequent call to put() will not block.
Queue.full()
39
Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).
Queue.put(item, block=True, timeout=None)
40
Equivalent to put(item, block=False).
Queue.put_nowait(item)
41
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).
Queue.get(block=True, timeout=None)
42
Equivalent to get(False)
Queue.get_nowait()
43
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.
Queue.task_done()
44
Blocks until all items in the queue have been gotten and processed.
Queue.join()
45
Inserts x at end of the queue
Enqueue(queue, x)
46
Returns and removes item at front of queue
Dequeue(queue)
47
Returns but does not remove item at the front of the queue
Peek(queue)
48
Returns true if queue has no items
IsEmpty(queue)
49
Returns the number of items in the queue
GetLength(queue)
50
Blank and blank operations should not be applied to an empty queue; the resulting behavior may be undefined.
Dequeue and Peek
51
A queue is often implemented using a blank, with the list's head node representing the queue's front, and the list's tail node representing the queue's end.
linked list
52
A queue can be implemented with an blank. Two variables are needed: length: an integer for the queue's length. front_index: an integer for the queue's front item index.
array
53
A blank is a queue with a length that does not exceed a specified maximum value.
bounded queue
54
An additional variable, max_length, is needed. max_length is commonly assigned at construction time and blank for the queue's lifetime.
does not change
55
A bounded queue with a length equal to the maximum length is said to be blank.
full
56
An blank is a queue with a length that can grow indefinitely.
unbounded queue
57
If max_length is blank, the queue is unbounded.
negative
58
If max_length is blank, the queue is bounded.
nonnegative
59
Compares self.length and self.max_length. If equal, the queue is full and so no change occurs and False is returned. Compares self.length and len(self.queue_list). If equal, a resize operation occurs. Computes the enqueued item's index as (self.front_index + self.length) % len(self.queue_list) and assigns to queue_list at that index. Ex: Enqueueing 42 into a queue with front index 2, length 3, and queue_list length 8 assigns queue_list at index (2 + 3) % 8 = 5 with 42. Increments the length attribute and then returns True.
An enqueue operation
60
Makes a copy of the list item at front_index. Decrements length. Increments front_index, resetting to 0 if the incremented value equals the allocation size. Returns the list item from step 1.
A dequeue operation
61
Using the implementation from above, worst-case time complexities are the same whether the queue is bounded or unbounded: blank for enqueue and blank for dequeue.
O(N) O(1)
62
A blank can be implemented in Python using a class with a single LinkedList data member. The class has two methods, push() and pop().
stack
63
Blank adds a node to the top of the stack's list by calling LinkedList's prepend() method. blank removes the head of the stack's list by calling the LinkedList's remove_after() method and then returns the removed node's data.
push() pop()
64
A blank can also be implemented in Python using a class with a single LinkedList data member and class methods enqueue() and dequeue().
queue
65
blank adds a node to the end of the queue's list by calling LinkedList's append() method.
enqueue()
66
The blank removes the queue's head node and returns the removed node's data.
dequeue() method
67
A blank is an ADT in which items can be inserted and removed at both the front and back.
deque (pronounced "deck" and short for double-ended queue)
68
Yhe deque blank operation inserts an item at the front of the deque, and the blank operation inserts at the back of the deque.
push-front push-back
69
The blank operation removes and returns the item at the front of the deque, and the blank operation removes and returns the item at the back of the deque.
pop-front pop-back
70
A blank operation returns an item in the deque without removing the item.
peek
71
Inserts x at the front of the deque
PushFront(deque, x)
72
Inserts x at the back of the deque
PushBack(deque, x)
73
Returns and removes item at front of deque
PopFront(deque)
74
Returns and removes item at back of deque
PopBack(deque)
75
Returns but does not remove the item at the front of deque
PeekFront(deque)
76
Returns but does not remove the item at the back of deque
PeekBack(deque)
77
Returns true if the deque is empty
IsEmpty(deque)
78
Returns the number of items in the deque
GetLength(deque)