1 / 15

EECE.3220 Data Structures

EECE.3220 Data Structures. Instructor: Dr . Michael Geiger Spring 2017 Lecture 14: Lists. Lecture outline. Announcements/reminders Program 3 to be posted; due Monday, 3/6 Basic use of classes Plan to return exams Monday Today’s lecture Review: composition, initialization lists

vizcaino
Download Presentation

EECE.3220 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECE.3220Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 14: Lists

  2. Lecture outline • Announcements/reminders • Program 3 to be posted; due Monday, 3/6 • Basic use of classes • Plan to return exams Monday • Today’s lecture • Review: composition, initialization lists • Lists as ADTs • Array-based lists • Statically allocated arrays + overloaded operators • Dynamically allocated arrays + default arguments, copy constructors, and destructors Data Structures: Lecture 14

  3. Review: Composition example • A rectangle is a shape that has a: • point of origin • width • height • Can implement this concept by defining a class named Rectangle • Methods might include: • Accessing width/height/origin • Setting width/height/origin • Calculating area • .h files on next two slides • Most function definitions self-explanatory Data Structures: Lecture 14

  4. Review: Point.h class Point { public: Point();// Default constructor Point(double X, double Y); // Parameterized constructor void setX(double newX); // Set X coordinate void setY(double newY); // Set Y coordinate double getX(); // Returns X coordinate double getY(); // Returns Y coordinate void printPoint(ostream &out); // Output Point as // (xCoord, yCoord) private: double xCoord; // X coordinate double yCoord; // Y coordinate }; Data Structures: Lecture 14

  5. Review: Rectangle.h class Rectangle { public: Rectangle(); // Default constructor Rectangle(double h, double w, // Parameterized const. double x, double y); double getHeight();// Return height double getWidth(); // Return width Point getOrigin(); // Return origin void setHeight(double h); // Change height void setWidth(double w); // Change width void setOrigin(Point p); // Change origin double area(); // Return area of rectangle private: double width; double height; Point origin; // Lower left corner }; Data Structures: Lecture 14

  6. Review: Initialization lists • How would we write Rectangle constructor(s)? • Could use Point set functions • Ideally, we’d like to call Point constructor as well • Create new Point every time we create Rectangle object • Use an initialization list • Explicitly calls constructors for member data • Requires parameterized constructor to be defined • Can be used for predefined types as well • Example: Rectangle::Rectangle() : height(1), width(1), origin(0,0) {} Data Structures: Lecture 14

  7. List ADT • Common problem in program: store collection—or list—of things • Grocery list, grade list, list of students, etc. • Common properties • Homogeneous (elements all have same type) • Finite length (number of elements) • Length could be 1 or even 0 • Elements arranged sequentially • Well-defined first and last element • All except last have unique successor • All except first have unique predecessor Data Structures: Lecture 14

  8. List ADT (continued) • A sequence of a finite number of data items, all of the same type • Basic operations • Construction: create empty list • Empty: check if the list is empty • Insert: add an item to the list • Delete: remove an item from the list • Traverse: go through part or all of list, accessing and processing elements in order • Types of traversal include search, output (to screen or file), copy, rearrange (usually sort) • List ADT used for several data structures • Linked lists, stacks, queues Data Structures: Lecture 14

  9. Array-based lists • Implementing ADT • Define necessary data members • Define methods described in ADT design • Common list implementation: array • Built-in type in most languages • Sequential memory storage • Straightforward algorithms Data Structures: Lecture 14

  10. Static array-based list • Statically allocated array: size chosen at compile time (example: intarr[10];) • List using static array requires • Actual array • Maximum size / capacity • Number of elements actually stored in array • Some operations are relatively easy • Constructor: array allocated at compile time, so just need to set size to 0 • empty(): returns true if size == 0 • traverse: for loop to go through all elements for (i = 0; i < size; i++) <process array[i]> Data Structures: Lecture 14

  11. Static array-based list: insert algorithm • Pseudocode to insert item at position pos • If size == capacity • List is full (error); end insert operation • If pos < 0 or pos > size • Illegal position error; end insert operation • Otherwise, shift elements to make room, then insert item and update array: • For i = size topos + 1 • array[i] = array[i-1] • array[pos] = item • size++ • Worst-case time? Average time? Best-case time? • Worst & average: O(n), best: O(1) • Best-case occurs in stacks & queues Data Structures: Lecture 14

  12. Static array-based list: delete algorithm • Pseudocode to remove item at position pos • If size == 0 • List is empty (error); end delete operation • If pos < 0 or pos >= size • Illegal position error; end delete operation • Otherwise, shift elements to close gap, overwriting removed element • For i = pos to size – 2 • array[i] = array[i+1] • size-- Data Structures: Lecture 14

  13. Implementing List class with static array • List.h& List.cpp provided in handout/online • Two declarations outside class • Maximum array size, used for allocation • constint CAPACITY = 1024; • constkeyword means value won’t change • Common to put constant name in all caps • Element type • Redefining list type  changing one line in .h file • Will use typedef for now • Allows you to specify different name for existing type • Example: typedefintElementType; • Will later discuss C++ template mechanism Data Structures: Lecture 14

  14. const variables, arguments, methods • const keyword  value won’t be changed • Define constant values • constint CAPACITY = 1024; • Indicate function argument won’t be modified • Used with reference arguments when pass-by-reference used to save space • ElementType f(constList &myList); • const methods won’t modify calling object • bool empty() const; • Given List L, if I write: L.empty(); • L is the calling object  empty() accesses member(s) of object L Data Structures: Lecture 14

  15. Final notes • Next time: more on array-based lists • Reminders: • Program 3 to be posted; due Monday, 3/6 • Basic use of classes • Plan to return exams Monday Data Structures: Lecture 14

More Related