Lecture 7
Learning objectives
After this class, you should be able to:
- Declare a vector class and use its following features: back, begin, empty, erase, front, insert, [ ], pop_back, push_back, rbegin, rend, constructors.
- Explain the semantics of insertion into a vectors whose current storage is already full.
- Explain the limitations of vectors.
- Explain how linked lists help overcome some of these limitations.
- Explain the idea behind linked lists.
- Give a set of operations on a linked list, draw a figure that shows the state of the linked list.
- Analyze the time complexity of operations, and the space requirements for the linked list class.
- Implement the linked list class.
- Give applications where a linked list might be useful.
- Write code using the following features of an STL list class: back, front, push_back, pop_back, push_front, pop_front, begin, end, pop_back, pop_front, list(), erase, remove, size, merge, sort, and unique.
Reading assignment
- Sections 3.1-3.4, class notes for complexity analysis.
- Lecture: Vectors.
- Page 84 (3rd edition).
- Chapter 3, sections 3.2, 3.3, 3.5.
- Lecture: Linked lists.
- Pages 94 and 104 (3rd edition).
Exercises and review questions
- Exercises and review questions on current lecture's material
- Read the
Lec6/usevec.cpp
code and predict its output. Compare it with the output you get when you run the code.- Replace
erase
in the fileusevec.cpp
withpop_back
.- Instead of doubling the capacity during push_backs, when size becomes equal to capacity, let us increase the capacity by 10. What is the amortized time complexity per
push_back
?- Instead of doubling the capacity during push_backs, when size becomes equal to capacity, let us make it three times the previous capacity.What is the amortized time complexity per
push_back
?- What is the asymptotic space requirement of a linked list, as a function of the number of items present in the list? How does this compare with that for a vector?
- What is the asymptotic time requirement to check for membership in a linked list? How does this compare with that for a vector?
- Draw a figure to show the state of a doubly linked list after we have performed the following operations on it:
- Pushback 'a'
- Pushback 'a'
- Pushfront 'b'
- Pushfront 'd'
- Remove 'b'
- If we used linked lists to represent sets of items (note that a set cannot have duplicate items, and so we first need to check if an item is present, before inserting it into the list), then give the asymptotic time complexity to insert an element into a linked list.
- If we used linked lists to represent sets of items, then give an algorithm to compute the union of two sets, and give the asymptotic time complexity.
- If we used linked lists to represent sets of items, then give an algorithm to compute the intersection of two sets, and give the asymptotic time complexity.
- If we used linked lists to represent sets of items, then give an algorithm to compute the difference of two sets, and give the asymptotic time complexity.
- If we used linked lists to represent sets of items, then give an algorithm to compare two sets for equality, and give the asymptotic time complexity.
- If you wanted to create a sorted linked list, then give an algorithm to perform insertion, and give its asymptotic time complexity.
- Can binary search be performed on a sorted linked list? Justify your answer.
- Give an application where linked lists would be preferable to vectors, and one where vectors would be preferable to linked lists. (Post your answer on the discussion board)
- Questions on next lecture's material
- What is the difference between a stack and a queue?
Last modified: 14 Aug 2012