Week 6 Flashcards

1
Q

True or false, Stacks are last in and first out.

A

True. They are LIFO linear data structures. (In other words, reverse order of insertion).

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

True or false, stacks can only be accessed at the top using push and pop operations.

A

This is true.

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

What is this data structure?

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

A

This is a stack.

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

What are the basic operations of a stack?

A

Push: Stores an element at the top of the stack (insert)

Pop: remove and return the element at the top of the stack (delete)

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

Can you pop an empty stack.

A

No

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

If the stack has a maximum size, can you push when the stack is full?

A

You cannot push when the stack is full.

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

What does the stack operation peek do?

A

Returns the top element without popping.

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

What does the stack operation search do?

A

Returns the position where an object is on the stack.

Returns -1 if not found.

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

What does the stack operation full do? How about empty?

A

Checks if the stack is full and checks if the stack is empty.

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

What does the stack operation clear do?

A

Clears the entire stack.

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

What are some applications of stacks?

A

Function calls and recursion.
Undo and Redo
Expression evaluation
Backtracking
Environment specific modeling

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

How does a stack look like in an array?

A

Push: increment the top and store the element at that position in the array.
top +=1
array[top] = value

Pop: Copy the top element in the array to a temporary value and decrement top.
temp = array[top]
top -= 1

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

What is the difference between a stack in a linked list and in an array?

A

Unlike arrays, linked lists have no max size.

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

What is the complexity of time operations for pop and push (regardless of implementation?)

A

They are O(1)

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

What are queues analogous to?

A

It is analogous to lineups at store checkouts. They are FIFO

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

What are the basic operations of queues?

A

enqueue: add an element to the end of the list. (add insert).

dequeue: delete and return the element at the beginning of the list. Remove, poll, delete.

If the capacity is max you cannot enqueue. If the queque is empty you cannot dequeue.

16
Q

What additional operations may queues use?

A

peek
search
clear
full
empty

17
Q

How is a queue implemented in an array?

A

Two variables to point the beginning and the end.

The head index is incremented after dequeuing.

The tail index is incremented before enqueuing.

(Why not the reverse?)

18
Q

In a queue, where do you add and remove elements from.

A

You remove from the head and add to the tail.

19
Q

How do we know that a queue is empty?

A

The head and tail will be equal. We will set both of them to -1 to indicate an empty queue.

20
Q

How will the enqueue instruction work in a queue?

A

If the queue is empty we set the head and tail to 0.

Else, increment tail mod n
* tail += 1
* tail % n
* set array[tail] to element value.

21
Q

How do we dequeue in an array?

A

Store array[head] in a temporary variable.

If only one element in the queue(head == tail)
* set head and tail to -1 (indicates empty)

else, Increment head mod n.
* head+= 1
* head%n
* return the value in the temporary variable.