lecture 24
play

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


  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? Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 1

  2. Outline  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 2

  3. Project 5  Term class with public attributes. I.e., a struct with operations. Make operator<< and operator== non-member free functions.  Linked list toolkit in namespace Project5 and using Term as the type for the value_type typedef.  Use linked list toolkit as much as possible to implement Polynomial class operations. But some will require direct manipulation of list nodes and their data items. Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 3

  4. Project 5  Public part of Polynomial class definition is the same as Project 3 with added destructor, copy constructor, and assignment operator.  Private attribute is just head pointer to linked list of nodes containing Term objects.  Access Term fields using node pointers: Node *ptr = headPtr; Node::value_type aTerm = ptr->Data(); if (aTerm.expo == 0) out << aTerm.coeff; Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 4

  5. Dynamic Array vs. Linked List  Now that we have two different implementation techniques for containers, we can compare them and discuss when to use them.  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 5

  6. Dynamic Array + Easy to use – built into the language + Easy to do random access via index computation; O(1) operation - Hard to insert/remove in the middle of collection: must shift items forwards and backwards; O(n) operation - Hard to resize: must allocate new storage, copy elements, deallocate old storage; O(n) operation + Space overhead can be minimal Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 6

  7. Linked List - Harder to use: must implement nodes and operations - Cannot do random access: must always start from head node; O(n) operation + Easy to insert/remove in the middle of the list: allocate a new node and hook it up; O(1) operation + Never have to resize - Space overhead of one pointer per element Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 7

  8. Singly vs. Doubly Linked List  Linked list as presented is singly-linked. That is, there is a link in the "forward" direction only.  There are (at least) two disadvantages to having only one link:  we cannot scan the list in reverse order (from the tail pointer to the head pointer) easily  in order to insert/remove an item, we need to have a pointer to the previous node; this means keeping track of a previous pointer while we search for the position of insertion/removal Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 8

  9. Singly vs. Doubly Linked List  To mitigate these disadvantages, we could define a node to have a second link in the "backward" direction pointing to the previous node in the list. This is a doubly-linked list . + We could scan the list in reverse order by starting with the tail pointer and following the back links. + We can eliminate tracking a previous pointer since each node has a back link. - Space overhead is now two pointers per item Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 9

  10. Dnode Class class Dnode { public: typedef double value_type; ... private: value_type dataItem; Node *nextLink; Node *prevLink; }; dataItem nextLink prevLink 3.14 Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 10

  11. DListRemove 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 11

  12. How to Choose?  Answer depends on the nature of the application. Some guidelines: Frequent random access operations Use a dynamic array Operations occur in middle of collection Use a linked list Operations require moving both forwards Use a doubly-linked list and backwards through collection Frequent resizing may be needed Use a linked list to avoid resizing inefficiency Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 12

  13. STL Containers  The C++ STL define three general-purpose containers that use these implementation techniques  As you may recall, vector<T> is implemented using a dynamic array and has indexing.  list<T> is implemented using a doubly-linked list and is accessed using an iterator.  deque<T> (pronounced "deck") is implemented in such a way that makes all operations relatively efficient. Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 13

  14. list<T>  list<T> is defined in the <list> library, and it is in namespace std.  It has all the vector<T> operations except operator[ ]. In addition, it has push_front( ), pop_front( ), and front( ) that operate on the front (i.e., head) end of the list.  Iterators are classes that define pointer syntax for accessing collections, in general, and are the only way to access the items in a list<T>.  We will examine list<T> and iterators in more detail later in this course. Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 14

  15. deque<T>  deque<T> is defined in the <deque> library, and it is in namespace std.  It has all the vector<T> operations including operator[ ] and all of the additional list<T> operations, which makes it a good choice when you want to do both random access and insert/removal in the middle of the collection.  The underlying implementation is more complex than vector<T> or list<T> so that it can support all the operations efficiently. A discussion of this can be found at the end of Chapter 8. Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 15

Recommend


More recommend