vector class homogeneous aggregate with random access
play

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


  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 2. 1 Duke CPS 100

  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? 2. 2 Duke CPS 100

  3. What is a pointer, why use pointers? linked list, benefits? ● indirect reference struct Node ➤ index in book { ➤ forwarding address string info; Node * next; ➤ universal email: }; ola@acm.org Node * list = ________; ● pointer benefits ➤ multiple pointers to “ dog ” “ cat ” same object list ➤ self-referential Node * temp = new Node; structures temp->info = “ant”; ➤ attach/detach object temp->next = list->next; ➤ facilitates inheritance list->next = temp; how to remove cat? 2. 3 Duke CPS 100

  4. pointer operations, properties ● a pointer is a variable/object “ dog ” “ cat ” ➤ points to something list ➤ how to find what’s pointed to? ➤ * has lots of meanings ● provide a reference (something to point to) Node * temp = new Node; ➤ list = new Node; temp->info = “ant”; ➤ list = 0; list = NULL; temp->next = list->next; list->next = temp; ➤ temp->next = list; ➤ temp = lastNode(list); 2. 4 Duke CPS 100

  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 2. 5 Duke CPS 100

  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? 2. 6 Duke CPS 100

  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? 2. 7 Duke CPS 100

  8. Using a header node “pear” “plum” ● advantages of header node first ➤ ➤ an empty list first ● disadvantages? ➤ ● what about counting nodes? ● what about printing nodes? in makeListFromFile code, how are first and last initialized? 2. 8 Duke CPS 100

  9. Deleting one node, some nodes, all nodes ● delete operator Node * temp = new Node; delete temp; ➤ pointer is argument, but through pointer deletes storage “pear” “plum” ➤ access after deletion? first ➤ delete first? first->next? ● Do NOT delete until kinks worked out of code 2. 9 Duke CPS 100

  10. specified node, all nodes, assume header node void remove(Node * list) void remove(Node * list, { const string & key) Node * temp = 0; { Node * temp = 0; while (list != 0) { while (list != 0) { if (list->info == key) { list = list->next; } } } list = list->next; } “pear” “plum” } first 2. 10 Duke CPS 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend