Programming Abstractions

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CS 240: Data Structures Thursday, June 21 th Lists – Array based, Link based Dynamic vs Static.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
Data structures Abstract data types Java classes for Data structures and ADTs.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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 Working with Pointers An exercise in destroying your computer.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
 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.
STACKS & QUEUES for CLASS XII ( C++).
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Pointers and Linked Lists
Programming Abstractions
Programming Abstractions
Pointers and Linked Lists
Programming Abstractions
Lecture 04: Sequences structures
Programming Abstractions
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Data Structure and Algorithms
Programming Abstractions
UNIT-3 LINKED LIST.
Programming Abstractions
Programming Abstractions
Hashing Exercises.
Dynamic Memory CSCE 121 J. Michael Moore.
CSCE 210 Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
void Pointers Lesson xx
Pointers and References
Circular Buffers, Linked Lists
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
Popping Items Off a Stack Lesson xx
Pointers and Linked Lists
Pointers, Dynamic Data, and Reference Types
CSC 143 Queues [Chapter 7].
Programming Abstractions
Review & Lab assignments
Dynamic Data Structures
Dynamic Memory A whole heap of fun….
Chapter 16 Linked Structures
Recall: stacks and queues
Introduction to C++ Linear Linked Lists
CSE 12 – Basic Data Structures
Dynamic Memory A whole heap of fun….
Lists.
Data Structures & Algorithms
Recall: stacks and queues
Pointers and dynamic objects
COP 3330 Object-oriented Programming in C++
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Lecture 6: References and linked nodes reading: 16.1
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Topics: This week: Memory and Pointers Monday: revisit some topics from last week in more detail: Deeper look at new/delete dynamic memory allocation Deeper look at what a pointer is Today: Finish up the music album example Linked nodes Friday: Linked List data structure (if we have time) priority queues and binary trees Hat tip to Victoria Kirst of Google for some of today’s slides!

Pointers recap

**Pointers recap so far** (bookmark this slide) The first pointer we saw was a dynamically allocated array type* name = new type[length]; int* data = new int[10]; A pointer is a variable that stores a memory address A memory address is just a number, like an array index but where the array is the entire memory of the computer You can use the address-of operator & to ask for the address of any named variable in your program int x = 3; int* ptr = &x; Many common types of variables (like ints) consume 4 bytes of memory, so addresses increment by 4 between adjacent variables. Other types (like doubles and pointers) might take 8 or more bytes to store because they are more complex. In general, you don’t really need to worry about this detail for this course, but it’s good to be aware of it. Sharing information between several objects is a common use case for a pointer Each album object contains a pointer to the artist object, so they can all share the artist information instead of many copies DRAW DIAGRAM ON WHITEBOARD to contrast two approaches.

Next steps with pointers and structs/classes/objects

Pointers replace redudant copies with a “please see,” like a book/paper citation "Britney Spears", 34, "Snickers", 163 { "Blackout", 2007, "Britney Spears", 34, "Snickers", 163 } { "Circus", 2008, "Britney Spears", 34, "Snickers", 163 } Redundancy:  britney blackout circus 256 512 1024 Sharing/ efficiency:  "Britney Spears", age: 34, food: "Snickers", height: 163 title: "Blackout", year: 2007, artist: Please see the "britney" object title: "Blackout", year: 2007, artist: 256 title: "Circus", year: 2008, artist: Please see the "britney" object title: "Blackout", year: 2007, artist: 256 britney 256 blackout circus

Fixing the Album/Artist example with pointers "Circus" 2007 circus 1024 struct Artist { string name; int age; string favorite_food; int height; // in cm }; struct Album { string title; int year; Artist* artist; }; "Blackout" 2007 blackout 512 britney 256 stack heap Artist* britney = new Artist; // TODO: now we need to set the fields of britney Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; -10044 234123523 3567 -10044 256 britney{ "Britney Spears", 34, "Snickers", 163};

Fixing the Album/Artist example with pointers struct Artist { string name; int age; string favorite_food; int height; // in cm }; struct Album { string title; int year; Artist* artist; }; Artist* britney = new Artist; // TODO: now we need to set the fields of britney britney.name = "Britney Spears"; // no! type is Artist* not Artist // we need a new tool that says // “follow the pointer” Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; britney{ "Britney Spears", 34, "Snickers", 163};

"Dereferencing" a pointer You can follow ("dereference") a pointer by writing *variable_name int x = 10 int* ptr_to_x = &x; cout << *ptr_to_x << endl; // 10 40 x 10 36 82391 32 23532 28 93042

Fixing the Album/Artist example with pointers struct Artist { string name; int age; string favorite_food; int height; // in cm }; struct Album { string title; int year; Artist* artist; }; Artist* britney = new Artist; // TODO: now we need to set the fields of britney (*britney).name = "Britney Spears"; // this works but really clunky Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; britney{ "Britney Spears", 34, "Snickers", 163};

-> operator: Dereferencing and accessing a member struct Artist { string name; int age; string favorite_food; int height; // in cm }; struct Album { string title; int year; Artist* artist; }; Artist* britney = new Artist; // TODO: now we need to set the fields of britney britney->name = "Britney Spears"; // ptr->member is the exact same as (*ptr).member Album blackout = { "Blackout", 2007, britney }; Album circus = { "Circus", 2008, britney }; britney{ "Britney Spears", 34, "Snickers", 163};

Linked Nodes Another important application of pointers We’ll start by looking at a limitation of the array

Arrays What are arrays good at? What are arrays bad at? 3 10 7 8 132121 124112 834252 926073 234132 645453 list index: 1 2 3 4 5 6 7 8 9

Memory is a giant array... Insertion - O(n) Deletion - O(n) list 3 10 7 8 index: 1 2 3 4 5 6 7 8 9 What are the most annoying operations on a tightly packed book shelf, liquor cabinet, shoe closet, etc? Insertion - O(n) Deletion - O(n) Lookup - O(1) Let's brainstorm ways to improve insertion and deletion....

Add to front Before: index: 1 2 3 4 5 6 7 8 9 After: index: 1 2 3 4 5 What if we were trying to add an element "20" at index 0? 3 10 7 8 Before: index: 1 2 3 4 5 6 7 8 9 20 3 10 7 3 8 10 7 8 7 8 After: index: 1 2 3 4 5 6 7 8 9

Add to front Wouldn't it be nice if we could just do something like: 3 10 7 8 index: 1 2 3 4 5 6 7 8 9 2. "Then the next elements are here!" 20 "Start here instead!"

Now we add to the front again: Arrows everywhere! 15 3 10 7 8 index: 1 2 3 4 5 6 7 8 9 20

Another visualization... 15 3 10 7 8 index: 1 2 3 4 5 6 7 8 9 20

Another visualization... 15 3 10 7 8 index: 1 2 3 4 5 6 7 8 9 20

This is a list of linked nodes! 20 3 15 10 8 A list of linked nodes (or a linked list) is composed of interchangeable nodes Each element is stored separately from the others (vs contiguously in arrays) Elements are chained together to form a one-way sequence using pointers

Linked Nodes A great way to exercise your pointer understanding

Linked Node We can chain these together in memory: data next 10 data struct LinkNode { int data; LinkNode *next; } We can chain these together in memory: LinkNode *node1 = new LinkNode; // complete the code to make picture node1->data = 10; node1->next = NULL; LinkNode *node = new LinkNode; node->data = 10; node->next = node1; data next 10 data next 75 NULL node

FIRST RULE OF LINKED NODE/LISTS CLUB: DRAW A PICTURE OF LINKED LISTS Do no attempt to code linked nodes/lists without pictures!

List code example: Draw a picture! struct LinkNode { int data; LinkNode *next; } Before: front->next->next = new LinkNode; front->next->next->data = 40; After: Using “next” that is NULL gives error Other/none/more than one data next 10 data next 20 NULL front data next 10 data next 40 data next 20 NULL front data next 10 data next 20 data next 40 NULL front

List code example: Draw a picture! struct LinkNode { int data; LinkNode *next; } Before: Write code that will put these in the reverse order. data next 10 data next 20 data next 40 NULL front