CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 8. 150202. CARRANO C++ INTERLUDE 2, CHAPT 4, 6.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
CSE115/ENGR160 Discrete Mathematics 03/22/12 Ming-Hsuan Yang UC Merced 1.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
CSS342: Induction1 Professor: Munehiro Fukuda. CSS342: Induction2 Today’s Topics Review of sequence and summations Mathematical induction Strong form.
1 Strong Mathematical Induction. Principle of Strong Mathematical Induction Let P(n) be a predicate defined for integers n; a and b be fixed integers.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CUSACK CHAPT 4.
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.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
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(
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
Chapter 4 Link Based Implementations
Link Based Implementations
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
5.13 Recursion Recursive functions Functions that call themselves
Linked Lists Chapter 6 Section 6.4 – 6.6
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Linked lists.
Class: Special Topics Copy Constructors Static members Friends this
Chapter 4 Linked Lists
LinkedList Class.
Chapter 16-2 Linked Structures
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Copy Constructor CSCE 121 J. Michael Moore.
Follow me for a walk through...
Chapter 4 Linked Lists.
Applied Discrete Mathematics Week 9: Integer Properties
Introduction to C++ Linear Linked Lists
Follow me for a walk through...
Linked Lists.
Follow me for a walk through...
Linked lists.
Copy Constructor CSCE 121.
Presentation transcript:

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.

Agenda HW3 Questions Finish Induction Assignment and Copy Constructor. Linked Lists

Induction Axiom: The principle of mathematical induction A property P(n) that involves an integer n is true for all n ≥ 0 if the following are true: 1.P(0) is true. 2.If P(k) is true for any k ≥ 0, then P(k+1) is true.

Proof by Induction Write a recursive function which calculates x n Prove the correctness of the recursive solution using induction int pow(int x, int n) { if (n == 0) return 1; else return x * pow(x, n-1); }

Proof by Induction for x n 1.Basis: When n = 0, pow(x, 0) = 1. x 0 = 1. The recursive function is correct. 2.Inductive hypothesis: When n = k, assume that pow(x, k) is correct, i.e., x k. 3.Inductive step: Show the pow(x, k+1) is correct. pow(x, k+1) = x * pow(x, k) By the inductive hypothesis, pow(x, k) returns the value x k. Thus, pow(x, k+1)= x * x k = x k+1 4.If pow(x, k) is correct, pow(x, k+1) is correct. Therefore, by the principle of mathematical induction, pow(x, n) is correct for any n ≥ 1.

Prove: a+ar 1 +ar 2 +ar 3 + … +ar n =a(r n+1 – 1)/(r- 1)

Strong Form of Mathematical Induction A property P(n) that involves an integer n is true for all n ≥ 0 if the following are true: 1.P(0) is true. 2.If P(0), P(1), …, P(k) are true for any k ≥ 0, then P(k+1) is true. Difference from ordinary form of mathematical induction: P(k+1) is factorized in a combination of two or more of P(0) through to P(k). Proof by strong form of mathematical induction 1.Base case or (basis): prove P(0) is true. 2.Inductive step: I.Inductive hypothesis: Assume P(1),P(2),..,P(k) is true for any k ≥ 0 II.Inductive conclusion: Prove P(k+1) is true.

Prove using induction: Every Integer > 1 Can Be Written as a Product of Prime Integers 1.Basis: n = 2. 2 is a prime number itself, and thus can be written as a product of prime integers. The proposition is true. 2.Strong Inductive hypothesis: Assume that the proposition is true for each of the integers 2, 3, … k 3.Inductive step: Show the proposition is true for k + 1. If k + 1 is a prime number, there is nothing more to show. If k + 1 is NOT a prime number, k + 1 is divisible and can be written k + 1= x * y where 1 < x < k + 1 and 1 < y < k + 1 Notice that x and y can be written as a product of prime integers by inductive hypothesis. If k is a product of prime numbers, k+1 is a product of prime numbers. Therefore, by the principle of mathematical induction, the proposition is true when n ≥ 1.

Computer Scientist of the week Charles Babbage Mathematician, Philosopher, Mechanical Engineer Invented the first mechanical computer Difference Engine: Computed values of polynomial functions If completed at the time would have had parts Finished in 1991 Analytical Engine Punch card based Branching and looping supported Currently being build: 7Hz and 675 Bytes

Copy Constructor Assignment Overload

Assignment / Copy Constructor 1) Bird b1("eagle", 123); 2) Bird b2; cout << "bird 1 " << b1; cout << "bird 2 " << b2; 3) b2 = b1; cout << "bird 2 " << b2; 4) Bird b3 = b1; cout << "bird 3 " << b3; class Bird { friend ostream& operator<<(ostream &, Bird &); public: Bird(); Bird(string name, int ID); void setFlu(bool flu); ~Bird(); private: string name; bool flu; int id; }; 1)Constructor(string, int) 2)Default Constructor 3)Assignment 4)Copy Constructor

Assignment/Copy Constructor All objects have implicit assignment operator and Copy constructor Chains to the assignment operators of the class members Built-in types do straight-forward copies This often works. However, when memory is dynamically allocated in the class it has problems. Need to override = and copy constructor in these cases Assignment: MyClass& operator=(const MyClass &myobj) Copy Constructor: MyClass(const MyClass &source)

Bird::Bird() { } Bird::Bird(string name, int n) { birdName = name; id = n; flu = false; } Bird::Bird(const Bird &b) { cout << "copy constructorcalled " << endl; } Bird& Bird::operator=(const Bird &b) { cout << "assignement has been called!!" << endl; } Overriding copy and == constructor

Assignment If Assignment = is over-ridden then all copying / allocation must be done on the object General steps MyClass& MyClass::operator= (const MyClass &source) { 1) If (this == &source) return; 2) //chain all member’ assignments operators 3) // manage all dynamic memory that has been utilized // de-allocate memory in destination // allocate new memory for a deep copy or just copy ref for a shallow copy 4) return *this; }

Copy Constructor Can be implemented with call to default constructor and then assignment (may not be most efficient) MyClass::MyClass(const MyClass &source) { MyClass(); *this = source; } Copy Constructor is invoked in these three cases: MyObj o1 = o2 Pass by Value Return by Value

Linked Lists…

A node DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013 Struct Node { string item; Node *next; }

A linked list FIGURE 4-2 SEVERAL NODES LINKED TOGETHER Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

A linked list with headPtr FIGURE 4-3 A HEAD POINTER TO THE FIRST OF SEVERAL LINKED NODES Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Let’s build an Int Stack Use a linked list as a data structure Use the following “node” structure struct Node { int value; Node *next; }; Overload the following operators: << Assign = + Change into a sorted list Add Remove / Peek function

bool IntStack::Push(int val) { Node *insNode; insNode = new Node; insNode->value = val; insNode->next = head; head = insNode; return true; } bool IntStack::Pop(int &val) { if (head == NULL) { return false; } else { Node *temp; temp = head; val = temp->value; head = head->next; delete temp; return true; } Push/Pop impl. IntStack::IntStack() { head = NULL; } IntStack::~IntStack() { this->Clear(); }

More next time…