Stacks and Queues
implement a circular array has what kind of time operations
All constant time operations
What are the available containers
Vector list deque
How are Queues useful
Queues are useful when data needs to be retrieved in the same order in which it was stored.
What type of access pattern do Queues have?
Queues use a first-in, first-out or FIFO access pattern.
Explain the Array Implementation operations (Queue)
an array and a back index as follows: • clear sets the back index to 0. • isEmpty returns true if the back index is 0, false otherwise. • enqueue stores the item at the back index, then increments the back index. • dequeue moves all elements except the first one position to the left, and decrements the back index. • front returns the first item in the array.
create a queue using a deque
queue<int > queue1;
How are The stack and queue containers seen in the STL
they are container adapters which means they are built on existing containers.
When are Stacks useful
when data needs to be retrieved in the reverse order in which it was stored.
How do you fix this issue
you add a front index as well as a back index • clear sets the front and back indexes to 0. • isEmpty returns true if the back index is the same as the front index, false otherwise. • enqueue stores the item at the back index, then increments the back index. • dequeue increments the front index. • front returns the item at the front index.
The operations of Linked List Implementation (Queue)
• clear delete all of the nodes and set the head and tail pointer to null. • isEmpty returns true if the head pointer is null, false otherwise. • enqueue adds a new node containing the item to the back of the list. • dequeue removes the node at the front of the list. • front returns the item in the node at the front of the list.
Linked List Implementation function
• clear deletes all of the nodes and sets the head pointer to null. • isEmpty returns true if the head pointer is null, false otherwise. • push adds a new node containing the item to the front of the list. • pop removes the node at the front of the list. • top returns the item in the node at the front of the list.
What are the Queues operations
• clear removes all items from the queue. • isEmpty returns true if the queue is empty, false otherwise. • enqueue adds an item to the back of the queue. • dequeue removes an item from the front of the queue. • front returns the item on the front of the queue without removing it.
Describe the Stack operations clear(); isEmpty push pop top
• clear removes all items from the stack. • isEmpty returns true if the stack is empty, false otherwise. • push adds an item to the top of the stack. • pop Removes an item from the top of the stack. • top Returns the item on the top of the stack without removing it.
Describe the operations
• clear sets the top index to 0. • isEmpty returns true if the top index is 0, false otherwise. • push stores the item at the top index, then increments the top index. • pop decrements the top index. • top returns the item just before the top index.
What are the member function for a queue container
• empty() returns true if the queue has no items, false otherwise. • pop() removes the item at the front of the queue. • push() adds an item to the back of the queue. • size() returns the number of items in the queue. • back() returns the item at the back of the queue. • front() returns the item at the front of the queue.
what are the member function for a stack container
• empty() returns true if the stack has no items, false otherwise. • pop() removes the item at the top of the stack. • push() adds an item to the top of the stack. • size() returns the number of items in the stack. • top() returns the item at the top of the stack.
create a stack using a deque
stack<int > stack1;
create a queue using a list
<int , list<int > > queue2;
Linked List Implementation (Queue)
A queue can also be implemented as a linked list with a head and tail pointer.
How can Queues be implemented as arrays
A queue can be implemented as an array and a back index as follows: clear isEmpty enqueue dequeue front
What is Queues
A queue is a linear data structure in which elements are added to one end and removed from the other end.
Linked List Implementation (Stack)
A stack can also be implemented using a singly linked list with only a head pointer as follows clear isEmpty push pop top
How can Array implementation of a stack can done?
A stack can be implemented using an array and a top index as follows Clear isEmpty push pop top
What is a Stack
A stack is a linear data structure that can be accessed only at one end.
A vector
A vector, which provides an array based implementation.
How do you fix losing open spaces in the front of the array
Circular Array Implementation
Note that all of the above operations in Array implementation are constant time except?
Clear
A stack has the following operations:
Clear isEmpty push pop top
From all the Array Implementation operations (Queue) which is not a constant time
Dequeue is liner time
What is the time for all the operation?
Once again, all of the above operations are constant time.
Stacks use what type of pattern
Stacks use a last-in, first-out or LIFO access pattern.
how do you prevent the front index overlapping the back index
This can be dealt with by keeping track of the number of items in the queue and using this information to determine if the queue is empty. • enqueue check if queue is full first. May need to throw an exception o stores the item at the back index, then increments the back index (wrapping around if necessary). Increment the number of items. • Dequeue check if it is empty ( may need to throw an exception o increments the front index (wrapping around if necessary). o Decrement the number of items
create a stack using a list
<int , list<int > > stack3;
create a stack using a vector
<int , vector<int > > stack2;
A deque
A deque, which combines arrays and linked data to provide efficient access, insertion, and deletion.
A list
A list, which provides a linked list-based implementation.(double linked list)
A queue can be created using a
deque list.
A stack can be created using?
deque vector list
How can we implement a circular array
index = (index+1)%capacity;