Lec3

417 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
417
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lec3

  1. 1. Data Structures and Abstract Data Types Chapter 3 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  2. 2. Chapter Objectives <ul><li>Look at ADTs, implementations in detail </li></ul><ul><li>Introduce arrays as ADTs </li></ul><ul><li>See arrays implemented as C++ static arrays </li></ul><ul><li>(Optional) Describe multidimensional arrays </li></ul><ul><li>Extend pointers to use in dynamic arrays </li></ul><ul><li>(Optional) Show use of C++ structs to model objects with multiple attributes </li></ul><ul><li>Show example of procedural programming paradigm </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  3. 3. Dynamic Arrays <ul><li>Recall earlier mention of arrays being fixed size at compile time </li></ul><ul><ul><li>Space wasted by unused elements </li></ul></ul><ul><ul><li>Program cannot adjust if size set too small </li></ul></ul><ul><li>Dynamic (run time) allocation mechanism provided </li></ul><ul><ul><li>Acquire memory as needed </li></ul></ul><ul><ul><li>Release memory when no longer needed </li></ul></ul><ul><li>C++ commands </li></ul><ul><ul><li>new and delete </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  4. 4. The new Operator <ul><li>Syntax for arrays new Type [capacity] </li></ul><ul><li>This command issues a run-time request for a block of memory </li></ul><ul><ul><li>Asks for enough memory for the specified number of elements of the stated type </li></ul></ul><ul><li>Example int *arrayPtr; arrayPtr = new int[6]; </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  5. 5. Pointer Arithmetic <ul><li>Possible to alter pointer contents </li></ul><ul><ul><li>The pointer is a variable </li></ul></ul><ul><ul><li>It is not a pointer constant like an array name </li></ul></ul><ul><li>Example Given: </li></ul><ul><li>Then ptr++; </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  6. 6. The delete Operation <ul><li>Counterpart to the new operation </li></ul><ul><li>Requests memory be returned to the heap </li></ul><ul><ul><li>Can then be reused by later allocations </li></ul></ul><ul><li>Syntax delete pointerVariable; delete [ ] arrayPointerVariable; </li></ul><ul><li>Frees the dynamically memory whose address is stored in the variable </li></ul><ul><ul><li>Does not delete the variable </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  7. 7. Memory Leaks <ul><li>Important for programmer to make sure to deallocate memory originally allocated by new </li></ul><ul><li>What if new is called again for intPtr ? </li></ul><ul><li>Originally allocated memory now cannot be accessed, nor is it available for reallocation </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  8. 8. Aggregate Data Types <ul><li>Predefined types not always adequate to model the problem </li></ul><ul><ul><li>When objects have multiple attributes </li></ul></ul><ul><ul><li>When objects have collections of heterogeneous elements </li></ul></ul><ul><li>C++ provides structs and class es </li></ul><ul><ul><li>Create new types with multiple attributes </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  9. 9. Structures <ul><li>Characteristics </li></ul><ul><ul><li>has a fixed size </li></ul></ul><ul><ul><li>is ordered </li></ul></ul><ul><ul><li>elements may be of different size </li></ul></ul><ul><ul><li>direct access of elements by name (not index) </li></ul></ul><ul><ul><li>struct Date { int month, day, year; char dayOfWeek [12]; </li></ul></ul><ul><ul><li>}; </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  10. 10. FAQs about Structures <ul><li>structs can be nested (can contain struct objects) </li></ul><ul><li>Access members with </li></ul><ul><ul><li>name of struct object </li></ul></ul><ul><ul><li>dot (member selector operator) . </li></ul></ul><ul><ul><li>name of struct member </li></ul></ul><ul><ul><li>Date today = { 3, 4, 2005, &quot;Tuesday&quot;); </li></ul></ul><ul><ul><li>cout << today.month; </li></ul></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  11. 11. Array of Structures <ul><li>const int MAX_SIZE = 500; </li></ul><ul><li>enum HealthType { POOR, FAIR, GOOD, EXCELLENT }; </li></ul><ul><li>struct AnimalType // Declares struct type </li></ul><ul><li>{ </li></ul><ul><li>long id; </li></ul><ul><li>string name; </li></ul><ul><li>string genus; </li></ul><ul><li>string species; </li></ul><ul><li>string country; </li></ul><ul><li>int age; </li></ul><ul><li>float weight; </li></ul><ul><li>HealthType health; </li></ul><ul><li>}; </li></ul><ul><li>AnimalType animalZoo[MAX_SIZE]; // Declares array </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
  12. 12. Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 AnimalType animalZoo[MAX_SIZE]; animalZoo [0] [1] . . . . . . [498] [499] animalZoo[0].id 3456219 animalZoo[0].name “camel” animalZoo[0].genus “Camelus” animalZoo[0].species “specs” animalZoo[0].country “India” animalZoo[0].age 10 animalZoo[0].weight 992.8 animalZoo[0].health Fair
  13. 13. Find total weight of all elements of the animalZoo array <ul><li>float total = 0.0; </li></ul><ul><li>for (j = 0; j < MAX_SIZE; j++) </li></ul><ul><li>total += animalZoo[j].weight; </li></ul>Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

×