Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 1
Lecture 24
No files for today. Project 5 posted; due in two weeks, but start
working on it now!
Reminder: Project 4 due today. Questions?
Lecture 24 No files for today. Project 5 posted; due in two weeks, - - PowerPoint PPT Presentation
Lecture 24 No files for today. Project 5 posted; due in two weeks, but start working on it now! Reminder: Project 4 due today. Questions? Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 1 Outline
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 1
No files for today. Project 5 posted; due in two weeks, but start
Reminder: Project 4 due today. Questions?
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 2
Project 5 Choosing an implementation technique
Dynamic array Linked list – singly linked, doubly linked
STL containers
vector<T> list<T> deque<T>
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 3
Term class with public attributes. I.e., a struct
Linked list toolkit in namespace Project5 and
Use linked list toolkit as much as possible to
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 4
Public part of Polynomial class definition is the
Private attribute is just head pointer to linked list
Access Term fields using node pointers:
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 5
Now that we have two different implementation
Some evaluation criteria:
Ease of use Efficiency of access operations Efficiency of insertion/removal Efficiency of changing size Efficiency of space
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 6
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 7
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 8
Linked list as presented is singly-linked. That
There are (at least) two disadvantages to
we cannot scan the list in reverse order (from the
in order to insert/remove an item, we need to have
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 9
To mitigate these disadvantages, we could
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 10
class Dnode { public: typedef double value_type; ... private: value_type dataItem; Node *nextLink; Node *prevLink; };
3.14
dataItem nextLink prevLink
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 11
void DListRemove (Dnode *itemPtr) { Dnode *prevPtr = itemPtr->BackLink(), *nextPtr = itemPtr->Link(); // Hook previous node to next node, if there is one if (prevPtr != 0) prevPtr->SetLink(nextPtr); // Hook next node to previous node, if there is one if (nextPtr != 0) nextPtr->SetBackLink(prevPtr); // Deallocate node delete itemPtr; }
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 12
Answer depends on the nature of the
Frequent random access operations Use a dynamic array Operations occur in middle of collection Use a linked list Operations require moving both forwards and backwards through collection Use a doubly-linked list Frequent resizing may be needed Use a linked list to avoid resizing inefficiency
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 13
The C++ STL define three general-purpose containers
As you may recall, vector<T> is implemented using a
list<T> is implemented using a doubly-linked list and is
deque<T> (pronounced "deck") is implemented in
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 14
list<T> is defined in the <list> library, and it is in
It has all the vector<T> operations except operator[ ].
Iterators are classes that define pointer syntax for
We will examine list<T> and iterators in more detail
Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 15
deque<T> is defined in the <deque> library, and it is in
It has all the vector<T> operations including operator[ ]
The underlying implementation is more complex than