Lecture 24 No files for today. Project 5 posted; due in two weeks, - - PowerPoint PPT Presentation

lecture 24
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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?

slide-2
SLIDE 2

Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 2

Outline

 Project 5  Choosing an implementation technique

 Dynamic array  Linked list – singly linked, doubly linked

 STL containers

 vector<T>  list<T>  deque<T>

slide-3
SLIDE 3

Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 3

Project 5

 Term class with public attributes. I.e., a struct

with operations. Make operator<< and

  • perator== 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.

slide-4
SLIDE 4

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

  • f nodes containing Term objects.

 Access Term fields using node pointers:

Node *ptr = headPtr; Node::value_type aTerm = ptr->Data(); if (aTerm.expo == 0)

  • ut << aTerm.coeff;
slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

  • peration
  • Hard to resize: must allocate new storage, copy

elements, deallocate old storage; O(n) operation + Space overhead can be minimal

slide-7
SLIDE 7

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

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

slide-9
SLIDE 9

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

Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 10

Dnode Class

class Dnode { public: typedef double value_type; ... private: value_type dataItem; Node *nextLink; Node *prevLink; };

3.14

dataItem nextLink prevLink

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Wednesday, October 20 CS 215 Fundamentals of Programming II - Lecture 24 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 and backwards through collection Use a doubly-linked list Frequent resizing may be needed Use a linked list to avoid resizing inefficiency

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

  • perations efficiently. A discussion of this can be

found at the end of Chapter 8.