CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Chapter 17 Linked List Saurav Karmakar Spring 2007.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Pointers OVERVIEW.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Writing a Good Program 8. Elementary Data Structure.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
slides adapted from Marty Stepp and Hélène Martin
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Programming Abstractions
Programming Abstractions
UNIT – I Linked Lists.
Doubly Linked List Review - We are writing this code
Programming Abstractions
Dynamic Memory CSCE 121 J. Michael Moore.
Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
CSC 143 Queues [Chapter 7].
Programming Abstractions
Chapter 17: Linked Lists.
CSE 12 – Basic Data Structures
Lecture No.02 Data Structures Dr. Sohail Aslam
Linked Lists.
Dynamic Memory CSCE 121.
Presentation transcript:

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics: 1. Pointers and new/delete (finish from Wed.) 2. Making your own C++ class  Implement Vector 3. How to manage dynamic memory in a class 2

Pointers and new/delete Finish up the example from Wednesday

Dynamic memory allocation int * p1 = new int[3];//0x12 *p1 = 5; int * p2 = new int;//0x4 *p2 = 7; int * p3 = new int;//0x20 *p3 = ; // important phone # *p1 = *p2; cout << p1 << “ “ << *p1 << endl; p1 = p2; cout << p1 << “ “ << *p1 << endl; delete p1; p2 = p3; delete p1; delete p2; cout << *p3 << endl; //print important phone #

Dynamic memory allocation int * p1 = new int[3];//0x12 *p1 = 5; int * p2 = new int;//0x4 *p2 = 7; int * p3 = new int;//0x20 *p3 = ; // important phone # *p1 = *p2; cout << p1 << “ “ << *p1 << endl; p1 = p2; cout << p1 << “ “ << *p1 << endl; delete p1; p2 = p3; delete p2; cout << *p3 << endl; //print important phone # These last four lines… A.Looks good! B.Didn’t do enough deleting C.Did too much deleting D.Accessed memory after deleting E.Other/none/more

Making your own class IntVector class

Practice making a class  We will do a simplified Vector implementation  Stanford library Vector uses template that can hold any type  Vector, Vector >, …  For simplicity, ours will only hold int  Don’t worry, we’ll learn templates later!

DESIGN! It matters  Client expectations: What does an int Vector need to do?  Hold ints  Know how many ints it is holding  Quickly return an int  Quickly change the value of an int  Grow/shrink by adding/removing ints at any index  All this becomes the.h file

Implementation #1: Array  We will just use an array  We need it to change size sometimes  THANK YOU, DYNAMIC MEMORY!

Implementation #2: Linked List  Array is fast to access an element, but slow to insert or remove one in the middle  Linked list is a structure designed to help with this issue

Quick note: accessing a member variable in a C++ object  Let’s say you have a pointer to a ListNode:  ListNode * head;  You want to access the next ListNode after that:  ListNode* theNextOne = head.next;  ListNode* theNextOne = (*head).next;  OR  ListNode* theNextOne = head->next;

FIRST RULE OF LINKED LISTS CLUB: DRAW A PICTURE OF LINKED LISTS Seriously. Seriously. Do no attempt to code linked lists without pictures.

Draw a picture! head->next->next = new ListNode; head->next->next->data = 40; Before: A. After: B. After: C. Using “next” that is NULL gives error D. Other/none/more than one datanext 10 datanext 20 NULL head datanext 10 datanext 40 head datanext 20 NULL datanext 10 datanext 20 head datanext 40 NULL

Add to front head NULL Add to the end Different cases of add head NULL Add to the middle NULL

Remove from the front NULL head Special cases of remove Remove in the middle NULL head NULL head Remove from the end NULL