Lecture 11
Learning objectives
After this class, you should be able to:
- Given certain operations on a deque, implemented by a circular array, draw a figure that shows its state at the end of those operations.
- Give the time complexity of the different operations on a deque.
- Implement features of a deque.
- Give applications where a deque can be useful.
- Given certain operations on a stack or a queue, draw a figure that shows the state of the data structure at the end of those operations.
- Give the time complexity of the different operations on a stack or a queue.
- Implement features of a stack or a queue, using other data structures, such as deques or linked lists.
- Give applications where stacks or queues can be useful.
Reading assignment
- Section 3.8 (up to fig 3.27), class notes for deque implementation based on a circular array (do not use the text book's implementation, until the next lecture), Section 4.1-4.2, section 4.3 (the need for a priority queue).
- Sec 4.4, 4.5.
Exercises and review questions
- Exercises and review questions on current lecture's material
(Note: Use a deque implementation based on a circular array in all questions)
- Give a sequence of operations such that a deque looks as shown in the figure below. Also give the array positions corresponding to the beginning and end of the deque.
a b - Let us assume a deque has the following data:
c d a b and we now try to push_front an 'e'. Show how the deque will look like after this insert operation.
- Implement the
back
member function of a deque.- Implement the
front
member function of a deque.- Implement the
push_back
member function of a deque.- Implement the
pop_back
member function of a deque.- Implement the
pop_front
member function of a deque.- Implement the
[]
operator of a deque.- If a deque looked liked in the question 2, and we
push_front
ane
, but want the new deque to look like:
e a b c d then how should the
push_front
code be changed? Note that this is a different implementation of a deque.- Give a real-world application for deques. (Post your answer on the discussion board)
- Let us perform the following operations on a stack: push(3), push(2), push(1), pop(). Draw a figure to show the state of the stack. If the underlying data structure were a deque, with initial capacity 2, then show the final state of the deque too. If the underlying data structure were a doubly-linked list, then show the final state of the list too.
- Let us perform the following operations on a queue: push(3), push(2), push(1), pop(). Draw a figure to show the state of the queue. If the underlying data structure were a deque, with initial capacity 2, then show the final state of the deque too. If the underlying data structure were a doubly-linked list, then show the final state of the list too.
- Will it be efficient to implement a stack using a vector?
- Will it be efficient to implement a queue using a vector?
- Will it be efficient to implement a stack using a singly linked list?
- Will it be efficient to implement a queue using a singly linked list?
- Show an implementation of a stack's
pop
andpush
functions, where the underlying data structure is a doubly linked list.- Give a real-world application for (i) stacks and (ii) queues. (Post your answer on the discussion board)
- Questions on next lecture's material
- Write code to insert the following
int
s into an STLstack
object, in the order given below, and then pop them all:1, 2, 3, 4
. In which order are they popped?
Last modified: 12 Aug 2009