Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview

Similar presentations


Presentation on theme: "Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview"— Presentation transcript:

1 Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview

2 Data Structures: Exam 2 Preview
Lecture outline Announcements/reminders Program 3 due 4/5 Exam 1: Monday, 2/25, 3-5 PM, Ball 214 Will be allowed two double-sided 8.5” x 11” note sheets No electronic devices Today’s lecture Exam 2 Preview 6/4/2019 Data Structures: Exam 2 Preview

3 Data Structures: Exam 2 Preview
Exam 2 notes Allowed two 8.5” x 11” double-sided sheets of notes No other notes, electronic devices (calculator, phone) Exam will last two hours We’ll start at 3:00—please be on time!! Covers lectures 14-16; 18-22 Exam sections (1+ questions in each) Class relationships (composition) Dynamic allocation Stacks 6/4/2019 Data Structures: Exam 2 Preview

4 Data Structures: Exam 2 Preview
Review: Classes Classes: programmer-defined types Concrete implementation of ADT Objects: instances of a class Each class typically contains Data members: attributes for each object Each object has own copy of data members Member functions: Tasks specific to class Data/functions can be: Public: accessible anywhere Private: accessible only in member functions Members private by default Private functions also known as helper functions 6/4/2019 Data Structures: Exam 2 Preview

5 Review: Initialization lists
Constructor initializes data members when new object is created With composition (object inside object), should call constructors for inner objects when parent object created 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) {} 6/4/2019 Data Structures: Exam 2 Preview

6 Review: Dynamic allocation with new
Use new to dynamically allocate space new int allocates space for 1 integer new int[20] allocates an array of 20 integers new “returns” pointer to first byte Directly initialize with value in parentheses e.g. new int(3) Pointer must hold address generated by new int *p1 = new int(3); // p1  single int = 3 int *p2 = new int[20]; // p2  array of // ints Dynamically allocated data must be deleted delete p1; // Deallocate single object delete [] p2; // Deallocate array 6/4/2019 Data Structures: Exam 2 Preview

7 Review: Dynamically allocated objects
May want to dynamically allocate objects Ex. array of Points where size is unknown at compile time Point *pointArray; int numPoints; cin >> numPoints; pointArray = new Point[numPoints]; Ex. linked list data structure—to add an element to the list, must allocate new element class linkedList { private: int elem; linkedList *next; public: linkedList(); linkedList(int val); void addElement(int i); ... } void linkedList::addElement(int i) { next = new linkedList(i); 6/4/2019 Data Structures: Exam 2 Preview

8 Review: Referencing objects thru pointers
Recall: use dot operator (.) to reference members of object, e.g: Point p1; p1.setX(2); With pointers, use -> linkedList *list1 = new linkedList; list1->addElement(2); 6/4/2019 Data Structures: Exam 2 Preview

9 Data Structures: Exam 2 Preview
Review: const const keyword  value won’t be changed Define constant values const int CAPACITY = 1024; Indicate function argument won’t be modified Used with reference arguments when pass-by-reference used to save space ElementType f(const Stack &myStack); const methods won’t modify calling object bool empty() const; Given Stack S, if I write: S.empty(); S is the calling object  empty() accesses member(s) of object S 6/4/2019 Data Structures: Exam 2 Preview

10 Review: Default function arguments
Prototypes can specify default values Example: Stack constructor in .h file Stack(unsigned maxSize = 1024); If argument provided, maxSize = argument Otherwise, maxSize = 1024 This constructor: default & parameterized! Stack S1(256);  creates stack w/max size 256 Stack S2;  creates stack w/max size 1024 Can be generalized to any function Given prototype int f(int a, int b=10); x = f(5, 15);  a = 5, b = 15 y = f(-1);  a = -1, b = 10 6/4/2019 Data Structures: Exam 2 Preview

11 Constructors and dynamic allocation
Can constructor simply initialize data members? What’s wrong with following constructor? Stack::Stack(unsigned maxSize = 1024) : tos(-1), cap(maxSize) {} Doesn’t initialize list! How should pointer be initialized? Either NULL or to some dynamically allocated memory 6/4/2019 Data Structures: Exam 2 Preview

12 Review: Constructors and dyn. allocation
Some cases: structure starts “empty” Don’t allocate anything Pointer = 0; (or NULL) Array-based stack Allocate array of desired capacity No C++-specific equivalent to realloc() Track # items actually in list using mySize Stack::Stack(unsigned maxSize = 1024) : cap(maxSize), tos(-1) { list = new double[cap]; } 6/4/2019 Data Structures: Exam 2 Preview

13 Data Structures: Exam 2 Preview
Review: Destructors Destructor: function called at end of object lifetime End of function in which object is declared -or- dynamically allocated object deleted Necessary if object has dynamically allocated member(s) In linked data structures, destructor must deallocate all nodes Naming syntax: ~Class(); (i.e., ~Stack()) Stack destructor: Stack::~Stack() { delete [] list; } 6/4/2019 Data Structures: Exam 2 Preview

14 Data Structures: Exam 2 Preview
Review: Stack ADT Stack is last-in, first-out (LIFO) data structure Definition Ordered collection of data items Can only be accessed at one end (top of stack) Operations Construction (start with empty stack) Check if stack is empty Push: add data to the top of the stack Pop: remove data from the top of the stack Read item at top of stack 6/4/2019 Data Structures: Exam 2 Preview

15 Review: Array-based stack
class Stack { public: Stack(unsigned maxSize = 1024); // Constructor ~Stack(); // Destructor bool empty() const; // true if stack empty void push(const double &val); // Push val to top // of stack void pop(); // Remove top of stack double top(); // Read top of stack private: double *list; // Actual data stored on the stack int tos; // Index for top of stack unsigned cap; // Capacity (max size) of stack }; list points to dynamically allocated array tos = index of top-most element Start at -1 (nothing on stack, so use invalid index) cap = array size Ensures accesses in bounds 6/4/2019 Data Structures: Exam 2 Preview

16 Data Structures: Exam 2 Preview
Review: Linked stack Series of nodes Node: data + pointer(s) to other node(s) In linked stack, single pointer to next node Pointer in last node = NULL Stack object Single data member: pointer to top node Push: allocate new node, point to old top node, change top pointer to new node Pop: store address of old top node, change top pointer to 2nd node, delete old top node 6/4/2019 Data Structures: Exam 2 Preview

17 Data Structures: Exam 2 Preview
Final notes Next time: Exam 2—please be on time!! Reminders: Program 4 still to be posted; due date TBD 6/4/2019 Data Structures: Exam 2 Preview


Download ppt "Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview"

Similar presentations


Ads by Google