Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1.

Similar documents
Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Pointers, Dynamic Data, and Reference Types

Section Handout #4: Classes, Pointers, and Dynamic Memory

1 Short Answer (7 Points Each)

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Object-Oriented Programming for Scientific Computing

CA31-1K DIS. Pointers. TA: You Lu

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

19. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointers and References

CSE 143. Lecture 7: Linked List Basics reading: 16.2

17. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

COMP6771 Advanced C++ Programming

a data type is Types

Pointers and Dynamic Memory Allocation

Instantiation of Template class

Lecture 2, September 4

Homework #3 CS2255 Fall 2012

G52CPP C++ Programming Lecture 13

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Pointers II. Class 31

QUIZ. What is wrong with this code that uses default arguments?

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

COMP 2355 Introduction to Systems Programming

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Lecture Notes on Memory Layout

Programming Abstractions

CS 11 C track: lecture 5

15. Pointers, Algorithms, Iterators and Containers II

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Financial computing with C++

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

The paramaterless ctor (aka default ctor)

Dynamic Allocation of Memory

CS 161 Exam II Winter 2018 FORM 1

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Dynamic Data Structures

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

In Java we have the keyword null, which is the value of an uninitialized reference type

CSE 303: Concepts and Tools for Software Development

Constants, References

Programming Abstractions

377 Student Guide to C++

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Variables, Memory and Pointers

Linked Lists. References and objects

Linked Memory. Pointers Linked Lists. September 21, 2017 Cinda Heeren / Geoffrey Tien 1

CS

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

12. Pointers Address-of operator (&)

Section #4 Solutions

Chapter 17 vector and Free Store

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Dynamic Allocation of Memory

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS61C : Machine Structures

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CS 106X, Lecture 14 Classes and Pointers

Contents of Lecture 3

HW1 due Monday by 9:30am Assignment online, submission details to come

Software Design and Analysis for Engineers

Advances in Programming Languages: Regions

Linked List using a Sentinel

CS 61c: Great Ideas in Computer Architecture

Goals of this Lecture

CS250 Final Review Questions

Ch. 12: Operator Overloading

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Array Based Lists. Collections

4.1 Ordered Lists Revisited

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

EL2310 Scientific Programming

Smart Pointers in C++11

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CSCI-1200 Data Structures Spring 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

A brief introduction to C++

Transcription:

Lecture 9 Pointer, linked node, linked list TDDD86: DALP Utskriftsversion av Lecture in Data Structures, Algorithms and Programming Paradigms 22nd September 2017 Ahmed Rezine, IDA, Linköping University 9.1 Content Contents 9.2 1 Pointer and linked node 1.1 Introduction Arraybased data structure Many containers use arrays of elements for storage What are the benefits/drawbacks of using arrays? Benefits: fast to add/remove at the end; fast to access elements Drawbacks: slow to add/remove from the middle; array is wasting memory; need to increase capacity when full 9.3 1.2 Pointers Linked data structure Other containers use linked node object to store data Each node object stores a data element and a link to another object Benefits: quick to add/delete in all position Drawbacks: slower access to certain part of the data structure To understand link structure, we need to understand pointers... 9.4 Memory Address When a variable is declared, it is stored somewhere in memory We can get the memory address with the &-operator Memory address are usually written in hexadecimal form (base-16) Many common data types use 4 bytes (32 bits) of memory 1

int x = 42; int y = 17; int a[3] = {91, -3, 85; cout << x << endl; // 42 cout << &x << endl; // 0x7f8e20 cout << y << endl; // 17 cout << &y << endl; // 0x7f8e24 cout << &a[0] << endl; // 0x7f8e28 cout << &a[1] << endl; // 0x7f8e2c cout << &a[2] << endl; // 0x7f8e30 9.5 Pointer pointer: One variable that story a memory address Pointers are declared with * after the type We can refer to the value at the address of the pointer using *-operator (also called dereferencing) int x = 42; int* p = &x; cout << p << endl; // 0x7f8e20 cout << *p << endl; // 42 *p = 99; cout << x << endl; // 99 9.6 Pointer arithmetic We can modify a pointer with +, -, ++, --, etc. Incrementing T* by 1 move the pointer to the next object of type T, in effect incrementing the address by the size of T We can use the syntax [k] for random access. k is put after the pointer: (An array variable is actually just a pointer pointing to the first element) int a[4] = {91, -2, 85, 17; int* p = a; // p = &a[0]; p[1] = 5; // a[1] = 5; p++; // p = &a[1]; cout << *p << endl; // 5 *(p + 2) = 26; // a[3] = 26; cout << p[2] << endl; // 26 2

9.7 Dangling pointer If a pointer is pointing at arbitrary location in memory. If we access the value of such a pointer, the program will likely crash! int x = 42; int* p; cout << p << endl; cout << *p << endl; int a[3] = {91, -2, 85; int* p2 = a; cout << p2[5] << endl; // dangling pointer // 0xd34d00 // KABOOM // BOOM 9.8 nullptr nullptr: Pointer literal which means points to nothing (points to address 0x0) Meant to be used as a blank value to initialise the pointer Dereferencing nullptr leads to an immediate crash! int x = 42; int* p = nullptr; cout << p << endl; // 0 cout << *p << endl; // KABOOM // Test for validity if (p == nullptr) {... // true 9.9 3

1.3 Object Pointer to objects We can point to objects To access a member of the object, write: pointer->member Equivalent to (*pointer).member Date d(9, 19); Date* p = &d; cout << p->daysinmonth() << endl; // 30 p->nextday(); cout << *p << endl; // 20/9 9.10 Objects lifetime Items declared as variable lives until the end of the block (end of function, end of if-statement) This is called static allocation or stack allocation If we return the value, the object is copied (or moved) and the original destroyed How can we create an object that outlive a function? void foo() { int x = 42; Date d1(9, 19);... // x, d1 förstörs int main() { int a = 17; foo(); Date d2(10, 14);... return 0; // a, d2 förstörs 9.11 Dynamic allocation Operator new allocate long term memory for an object Called dynamic allocation or allocation on the heap Objects are kept alived even after the end of the function scope The memory allocated for objects in the heap must be freed explicitly delete void foo() { Date* d = new Date(10, 14); cout << d->daysinmonth() << endl; // 31 d->nextday(); cout << d << endl; // 0x3a67f4 cout << *d << endl; // 15/10... delete d; 9.12 4

const pointers What is the difference between const T*, T const * and T * const? const T* and T const * are the same, it is a pointer to a constant value. int a = 2; const int* p = &a; *p = 3; // Does not Compile! ++p; // Compile but dangerous! T * const is a constant pointer to a non constant value. int a = 2; int* const p = &a; *p = 3; // Compile! ++p; // Does not compile (and bad idea anyway)! const T* const is a constant pointer to a constant value. int a = 2; const int* const p = &a; *p = 3; // Does not Compile! ++p; // Does not compile (and bad idea anyway)! 9.13 references vs pointers What is the difference between a pointer (T*) and a reference (T&)? Pointers is a variable that contains a memory address A pointer can (generally) be reassigned A pointer support arithmetic operation To access the value, pointers need to be dereferenced (ie *p or p->) Reference are more aliases to a variable Reference are bound to a specific variable at initialisation Reference behaves like variable (ie r r.) int a = 2; int&b = a; int*c = &a; // Prints 0x7ffc3c776de4 0x7ffc3c776de4 0x7ffc3c776de8 0x7ffc3c776de4 std::cout << &a << " " << &b << " " << &c << " " << &*c << std::endl; 9.14 references vs pointers References can be bound to temporaries const int &x = int(12); //legal C++ int *y = &int(12); //illegal to dereference a temporary. This makes const& safer for use in argument lists. void f(std::vector<int>* v) { void g(const std::vector<int>& v) { void h(std::vector<int>& v) { void i(std::vector<int> v) { void j(std::vector<int>&& v) { f(&(std::vector<int>{10, 12, 13)); // Error: taking address of temporary g(std::vector<int>{10, 12, 13); // Works! h(std::vector<int>{10, 12, 13); // Error: invalid initialisation of // non-const reference on temporary i(std::vector<int>{10, 12, 13); // Works! But less efficient than g j(std::vector<int>{10, 12, 13); // Works! As performant as g, // but wrong meaning! 9.15 5

references vs pointers References may or may not have a memory location of their own! int a = 2; int&b = a; int*c = &a; // Print 4 4 std::cout << ((long long int)&c - (long long int)&a) << " " << sizeof(int) << std::endl; References are similar to T* const but may or may not be implemented as such, the standard does not specify! In many ways, references are similar to pointers. So why C++ have them? to confuse students? Mostly for convenience Originally introduced for operator overloading. Imagine operators without references (to avoid copying): MyClass operator+(myclass*, MyClass* ); MyClass operator+(myclass*, int ); MyClass a, b; int c; &a + MyClass(); // Not Work &a + &b; // Works but ugly &a + c; // More confusion 9.16 1.4 Linked nodes Data structure for linked nodes struct ListNode { T data; ListNode<T>* next; ; Each object stores: a value of type T a pointer to an other node Linked nodes can be linked in a chain to form a list of values 9.17 Example of use of ListNode int main() { ListNode<int>* list = new ListNode<int>(); list->data = 42; list->next = new ListNode<int>(); list->next->data = -3; list->next->next = new ListNode<int>(); list->next->next->data = 17; list->next->next->next = nullptr; cout << list->data << " " << list->next->data << " " << list->next->next->data << endl; // 42-3 17... return 0; 9.18 6

Linked node: problem 1 What sequence of operations transformation this list: into this one? 9.19 Linked node: problem 2 What sequence of operations transformation this list: into this one? 9.20 Linked node: problem 3 What sequence of operations transformation this list: into this one? 9.21 Linked node: problem 4 What sequence of operations transformation this list: into this one? 9.22 7

Linked node: problem 5 Suppose we have to do a long chain: ListNode<int>* list = new ListNode<int>(10); list->next = new ListNode(20); list->next->next =...; We don t know how long the chain should be How do we print all the values? 9.23 Traversing a list? A (badly) way of printing all the value in the list: while (list!= nullptr) { cout << list->data << endl; list = list->next; // move to the next node What is the problem with this solution? We lose the list while printing it! 9.24 Traversing a list The right way to print all the values in the list: ListNode* current = list; while (current!= nullptr) { cout << current->data << endl; current = current->next; // move to the next node Changing current does not affect the list 9.25 2 Linked list LinkedList Lets write a container class LinkedList It has similar members to ArrayList add, clear, get, insert, isempty, remove, size, tostring Listan representeras internt som en kedja av länkade noder Listan har en pekare till den första noden i listan nullptr signalerar slutet av listan; är första noden nullptr är listan tom 9.26 8

LinkedList.h class LinkedList { public: LinkedList(); ~LinkedList(); void add(const T& value); void clear(); T get(int index) const; void insert(int index, const T& value); bool isempty() const; void remove(int index); void set(int index, const T& value); int size() const; string tostring() const; private: int m_size; ListNode<T>* m_front; ; 9.27 Implement add // Appends the given value to the end of the list. void LinkedList<T>::add(const T& value) {... How do we add a new node to the end of the list? Spelar listans innehåll innan operation någon roll? 9.28 Adding to an empty list Before add(20): After: 9.29 Add to a non-empty list Before add(20) After add(20) 9

9.30 Code for add // Adds the given value to the end of the list. void LinkedList::add(const T& value) { if (m_front == nullptr) { // adding to an empty list m_front = new ListNode<T>(value); else { // adding to the end of an existing list ListNode<T>* current = m_front; while (current->next!= nullptr) { current = current->next; current->next = new ListNode<T>(value); 9.31 Code for get // Returns value in list at given index. // Precondition: 0 <= index < size() T LinkedList::get(int index) { ListNode<T>* current = m_front; for (int i = 0; i < index; i++) { current = current->next; return current->data; 9.32 Code for insert // Inserts the given value at the given index. // Precondition: 0 <= index <= size() void LinkedList<T>::insert(int index, const T& value) { if (index == 0) { // inserting at front of list m_front = new ListNode<T>(value, m_front); else { // inserting in general position in list ListNode<T>* current = m_front; for (int i = 0; i < index - 1; i++) { current = current->next; current->next = new ListNode<T>(value, current->next); 9.33 Implementing remove // Removes value at given index from list. // Precondition: 0 <= index < size void LinkedList<T>::remove(int index) {... 10

How do we remove a node from a given position in a list? Spelar listans innehåll innan operation någon roll? 9.34 Remove from a list Before removing element at index 1: After: 9.35 Remove at the beginning of the list Before removing element at index 0: After: 9.36 Removing with a single element Before: After: We must change front to be a nullptr instead of pointing to a node Do we need a special case to deal with this? 9.37 11

Code for remove // Removes value at given index from list. // Precondition: 0 <= index < size() void LinkedList::remove(int index) { ListNode<T>* trash; if (index == 0) { // removing first element trash = m_front; m_front = m_front->next; else { // removing elsewhere in the list ListNode<T>* current = m_front; for (int i = 0; i < index - 1; i++) { current = current->next; trash = current->next; current->next = current->next->next; delete trash; 9.38 Is that the best use of Linked List? 9.39 12