Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Similar documents
CSI33 Data Structures

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

CSI33 Data Structures

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

CSI33 Data Structures

Vector and Free Store (Pointers and Memory Allocation)

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Instantiation of Template class

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

Link-Based Implementations. Chapter 4

Chapter 17 vector and Free Store

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

Lecture 15a Persistent Memory & Shared Pointers

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Linked List Practice Questions

Pointers. Developed By Ms. K.M.Sanghavi

Chapter 17 vector and Free Store

CS201- Introduction to Programming Current Quizzes

[0569] p 0318 garbage

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Variables, Memory and Pointers

Assignment of Objects

Review for Test 1 (Chapter 1-5)

CS 162, Lecture 25: Exam II Review. 30 May 2018

Arrays in C++ Instructor: Andy Abreu

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Chapter 17: Linked Lists

C++ Programming Lecture 4 Software Engineering Group

CS 161 Exam II Winter 2018 FORM 1

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Pointers. Memory. void foo() { }//return

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

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

Chapter 17: Linked Lists

Abstract. Vector. Overview. Building from the ground up. Building from the ground up 1/8/10. Chapter 17 vector and Free Store

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Assignment of Structs

Chapter 17 vector and Free Store. Bjarne Stroustrup

CSI33 Data Structures

Introduction to Programming Using Java (98-388)

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

C++ Object-Oriented Programming

Object-Oriented Principles and Practice / C++

Dynamic Allocation of Memory

vector and Free Store

Object-Oriented Programming

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

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

CPSC 427: Object-Oriented Programming

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CA341 - Comparative Programming Languages

Tokens, Expressions and Control Structures

Pointers and Dynamic Memory Allocation

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)

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

IS 0020 Program Design and Software Tools

CS 2604 Homework 1 Greatest Hits of C++ Summer 2000

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

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

This examination has 11 pages. Check that you have a complete paper.

Homework #3 CS2255 Fall 2012

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000

5 Phases of Software Life Cycle

CS 376b Computer Vision

Fast Introduction to Object Oriented Programming and C++

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Dynamic Data Structures

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

CS201 Latest Solved MCQs

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Data Abstraction. Hwansoo Han

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Introducing C++ to Java Programmers

Object Oriented Programming. Solved MCQs - Part 2

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSI33 Data Structures

Object-Oriented Principles and Practice / C++

ADT: Design & Implementation

Object-Oriented Principles and Practice / C++

Lecture 10 Notes Linked Lists

Doubly-Linked Lists

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Dynamic Memory Allocation

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

CPSC 427: Object-Oriented Programming

Absolute C++ Walter Savitch

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

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

Transcription:

Outline 1 Chapter 11: C++ Linked Structures

A ListNode Class To support a Linked List container class LList, a ListNode class is used for the individual nodes. A ListNode object has two attributes: item and link. Public access is allowed for these attributes the only class using the ListNode class is the LList class.

A ListNode Class In Python: Data in a node can be of any type a linked list can be heterogeneous. All values are references; the link attribute need not be declared to be a pointer. A link with the None value is used to indicate the end of a list.

A ListNode Class In C++: A typedef statement allows the type of data to be specified at compile time. (The Linked List will still be homogeneous, but at least a different version of the class for another type can be compiled for another program by simply changing the typedef statement). The item attribute must be declared to be a particular type (for now). The link attribute must be declared to be a pointer to another ListNode. A link with the NULL value (a C++ pointer set to 0) is used to indicate the end of a list.

A ListNode Class In C++: Later, we will see that the C++ Standard Template Library allows homogeneous lists for different data types (say, a list for the int type and a list for the Rational type) to be written at the same time, using a template class. Homogeneous lists for different data types can then be declared and used in the same program. The linked lists of these types will still be homogeneous. A list of integers can coexist with a list of doubles, but there can be no list containing integers and doubles mixed together.

Header File: ListNode.h typedef int ItemType; class ListNode { friend class LList; public: ListNode(ItemType item, ListNode* link=null); private: ItemType item ; ListNode *link ; ;

Header File: LList.h The main differences between writing Python and C++ linked structure classes: the need to write a destructor, copy constructor, and assignment operator for the class our C++ class must also explicitly deallocate memory (not required by Python)

Header File: LList.h class LList { public: LList(); LList(const LList& source); ~LList(); LList& operator=(const LList& source); int size() return size ; void append(itemtype x); void insert(size t i, ItemType x); ItemType pop(int i=-1); ItemType& operator[](size t position);

Header File: LList.h private: void copy(const LList &source); void dealloc(); ListNode* find(size t position); ItemType delete(size t position); ListNode *head ; int size ; ;

find method ListNode* LList:: find(size t position) { ListNode *node = head ; size t i; for (i=0; i<position; i++) { node = node->link ; return node;

delete method ItemType LList:: delete(size t position) { ListNode *node, *dnode; ItemType item; if (position == 0) { dnode = head ; head = head ->link ; item = dnode->item ; delete dnode; else { node = find(position - 1); if (node!= NULL) { dnode = node->link ; node->link = dnode->link ; item = dnode->item ; delete dnode; size -= 1; return item;

insert method void LList::insert(size t i, ItemType x) { ListNode *node; if (i == 0) { head = new ListNode(x, head ); else { node = find(i - 1); node->link = new ListNode(x, node->link ); size += 1;

Destructor LList::~LList() { dealloc(); void LList::dealloc() { ListNode *node, *dnode; node = head ; while (node) { dnode = node; node = node->link ; delete dnode;

Linked Dynamic Memory Errors - Breaking Links The integrity of a linked structure depends on the correct maintenance of all the links, since these are required to access the information in the structure. In our linked list class, if the ListNode s link attribute is set incorrectly, the resulting list will not be valid: If the link is incorrectly set to NULL, the list will be shortened, losing all data after that node. A memory leak will also occur, since there is no way to access the nodes to deallocate them. If the link is set to a node further along on the list, all nodes in between will be stranded: their data will be lost and their memory will not be deallocated. If the link is incorrectly set to a node earlier in the list, then a circular structure results (traversing the list becomes an infinite loop).

Linked Dynamic Memory Errors - Breaking Links // this code is incorrect void LList::insert(size t i, ItemType x) { ListNode *node; if (i == 0) { head = new ListNode(x, head ); else { node = find(i - 1); node->link = new ListNode(x); // incorrect size += 1;

Linked Dynamic Memory Errors - More Errors Python doesn t allow to use a name that has not been defined or is value None. Example: node = None, and we attempt to execute node.link or node.item, the Python interpreter will catch this problem and generate an exception and traceback (if you don t catch it). In C++ if you try to dereference an uninitialized pointer or a pointer that refers to a deallocated object, the run-time environment will attempt to access the memory location, resulting in garbage data or a memory fault that crashes your program.

As of now, we have two list classes defined in C++: List and LList. 1 Finish up the in-class List work from the previous lecture. 2 Define cout operation for the objects of type LList.

3. Using testllist.cpp write the program that performs the same operations using LList class: (I) 1) Creates an array of 20 elements, 2) fills it with 1 2, 2 2,..., (20) 2, 3) displays it, 4) then adds all of them and displays the sum, then (II) then 5) Define a friend function cin for List class, 6) Ask the user for a size of an array (now many values the user plans to enter), 7) Create an array of capacity = 10 * size, 8) Get the numbers from the user to store in the array, 9) add 10 to each member of the array and display it.