Lecture 8
Learning objectives
After this class, you should be able to:
- Given certain operations on 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 circular array.
- Implement features of a circular array.
- Give applications where a circular array 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 circular arrays or linked lists.
- Give applications where stacks or queues can be useful.
Reading assignment
- Sections 3.6, 3.7.
- Lecture: Stacks and Queues.
Exercises and review questions
- Exercises and review questions on current lecture's material
(Note: Use a circular array based implementation unless we mention otherwise.)
- Give a sequence of operations such that a circular array looks as shown in the figure below. Also give the array positions corresponding to the beginning and end of the circular array.
a b - Let us assume a circular array has the following data:
c d a b and we now try to push_front an 'e'. Show how the circular array will look like after this insert operation.
- Implement the
back
member function of a circular array.- Implement the
front
member function of a circular array.- Implement the
push_back
member function of a circular array.- Implement the
pop_back
member function of a circular array.- Implement the
pop_front
member function of a circular array.- Implement the
[]
operator of a circular array.- If a circular array looked liked in the question 2, and we
push_front
ane
, but want the new circular array 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 circular array.- Give a real-world application for circular arrays. (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 circular array, with initial capacity 2, then show the final state of the circular array 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 circular array, with initial capacity 2, then show the final state of the circular array 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
- None.
Last modified: 19 Sep 2011