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

Slides:



Advertisements
Similar presentations
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Advertisements

Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
More C++ Features True object initialisation
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
Yan Shi CS/SE 2630 Lecture Notes
ECE Application Programming
ECE Application Programming
ECE Application Programming
Linked Lists Chapter 6 Section 6.4 – 6.6
ECE Application Programming
Data Structures and Algorithms
CISC181 Introduction to Computer Science Dr
classes and objects review
Class: Special Topics Copy Constructors Static members Friends this
Stack and Queue APURBO DATTA.
This pointer, Dynamic memory allocation, Constructors and Destructor
Pointers and Dynamic Variables
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Dynamically Allocated Memory
Pointers and Linked Lists
Function and class templates
Array Lists Chapter 6 Section 6.1 to 6.3
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
Data Structures and Algorithms
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
9-10 Classes: A Deeper Look.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
9-10 Classes: A Deeper Look.
The List Container and Iterators
Data Structures & Programming
Presentation transcript:

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

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

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

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

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

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 20 // ints Dynamically allocated data must be deleted delete p1; // Deallocate single object delete [] p2; // Deallocate array 6/4/2019 Data Structures: Exam 2 Preview

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

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

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

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

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

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

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

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

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

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

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