vector class homogeneous aggregate with random access templated - - PowerPoint PPT Presentation

vector class homogeneous aggregate with random access
SMART_READER_LITE
LIVE PREVIEW

vector class homogeneous aggregate with random access templated - - PowerPoint PPT Presentation

vector class homogeneous aggregate with random access templated class: Vector<int> iv; Vector<string> sv; accessible using #include vector.h construct by specifying # elements (initial value) access with indexes


slide-1
SLIDE 1

Duke CPS 100

  • 2. 1

vector class

  • homogeneous aggregate with random access

➤ templated class: Vector<int> iv; Vector<string> sv; ➤ accessible using #include “vector.h” ➤ construct by specifying # elements (initial value) ➤ access with indexes starting at 0

for(k=0; k < eltCount; k++) cout << list[k] << endl;

  • growable, using resize or, this semester, append

➤ choice: vector tracks # elts or client code does

slide-2
SLIDE 2

Duke CPS 100

  • 2. 2

What is a class?

  • encapsulates state and behavior

➤ state is the innards, of which a class is comprised ➤ behavior is what a class does, how it is known

  • state is (typically) private, behavior public/private

➤ private functions callable from public, not client

  • class is constructed, and destructed (sometimes)

➤ automatic when an object/variable is defined ➤ (destruction) when object gone (deleted/scope)

  • examples: ifstream, string, vector, dice, date,...

➤ what about int, char, double, bool?

slide-3
SLIDE 3

Duke CPS 100

  • 2. 3

What is a pointer, why use pointers?

  • indirect reference

➤ index in book ➤ forwarding address ➤ universal email:

  • la@acm.org
  • pointer benefits

➤ multiple pointers to

same object

➤ self-referential

structures

➤ attach/detach object ➤ facilitates inheritance

struct Node { string info; Node * next; }; linked list, benefits? Node * list = ________;

“dog” “cat”

list Node * temp = new Node; temp->info = “ant”; temp->next = list->next; list->next = temp; how to remove cat?

slide-4
SLIDE 4

Duke CPS 100

  • 2. 4

pointer operations, properties

  • a pointer is a variable/object

➤ points to something ➤ how to find what’s

pointed to?

➤ * has lots of meanings

  • provide a reference

(something to point to)

➤ list = new Node; ➤ list = 0; list = NULL; ➤ temp->next = list; ➤ temp = lastNode(list);

Node * temp = new Node; temp->info = “ant”; temp->next = list->next; list->next = temp;

“dog” “cat”

list

slide-5
SLIDE 5

Duke CPS 100

  • 2. 5

using linked lists

  • facilitate splicing: adding or deleting nodes

➤ time to splice is independent of # nodes in list

(compare to vector)

➤ 33 elements means 33 nodes (compare vector)

  • problems managing memory (using new)

➤ possible to access “bad” memory ➤ need to clean up, throw unused space back

slide-6
SLIDE 6

Duke CPS 100

  • 2. 6

List traversals

int nodeCount(Node * list) { int total = 0; while (list != 0) { total++; list = list->next; } return total; } //.... int animalCount = nodeCount(animalList);

  • what is value of list after loop? is this a problem?
  • alternatives to loop test?
  • what about a recursive version?
slide-7
SLIDE 7

Duke CPS 100

  • 2. 7

Storing words from a file in a list

Node * makeListFromFile(istream & input) // precondition: input is readable // postcondition: returns pointer to first node of list // with strings in input, in same order as in input { Node * first = 0; Node * last = 0; string word; while (input >> word) { last->next = new Node; last = last->next; last->info = word; last->next = 0; } return first; }

  • Are there any problems here? anything missing?
slide-8
SLIDE 8

Duke CPS 100

  • 2. 8

Using a header node

  • advantages of header node

➤ ➤

  • disadvantages?

  • what about counting nodes?
  • what about printing nodes?

“plum” “pear” first first

an empty list

in makeListFromFile code, how are first and last initialized?

slide-9
SLIDE 9

Duke CPS 100

  • 2. 9

Deleting one node, some nodes, all nodes

  • delete operator

Node * temp = new Node; delete temp; ➤ pointer is argument, but through pointer deletes

storage

➤ access after deletion? ➤ delete first? first->next?

  • Do NOT delete until kinks worked out of code

“plum” “pear” first

slide-10
SLIDE 10

Duke CPS 100

  • 2. 10

specified node, all nodes, assume header node

void remove(Node * list, const string & key) { Node * temp = 0; while (list != 0) { if (list->info == key) { } list = list->next; } } void remove(Node * list) { Node * temp = 0; while (list != 0) { list = list->next; } } “plum” “pear” first