EECE.3220: Data Structures Spring 2017

Similar documents
Lab 2: ADT Design & Implementation

ADT: Design & Implementation

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 7: Stacks. Exercises 7.2

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

III. Classes (Chap. 3)

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

การทดลองท 8_2 Editor Buffer Array Implementation

Lecture 7. Log into Linux New documents posted to course webpage

Linked List using a Sentinel

CSCE 110 PROGRAMMING FUNDAMENTALS

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

" Templates for Functions. ! Templates for Data Abstraction. "Syntax for Class Templates. ! This swap algorithm only works for int values:

Introduction & Review

Part of the Picture: Simulation

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

Due Date: See Blackboard

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

Chapter 13. If the list is in increasing order; no interchanges are required.

Due Date: See Blackboard

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CSCI 123 Introduction to Programming Concepts in C++

C++_ MARKS 40 MIN

Inheritance and Polymorphism

CPSC 427: Object-Oriented Programming

Object Oriented Design

Chapter 11. if (prev!= 0) { newptr->next = prev->next; prev->next = newptr; if (current == 0) // new last element in priority queue myback = newptr;

CPSC 427: Object-Oriented Programming

Due Date: See Blackboard

ADTs & Classes. An introduction

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

ADT: Lists, Stacks and Queues

CSI33 Data Structures

Due Date: See Blackboard

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

Set Implementation Version 1

Programming Abstractions

Datastructuren André Deutz

Operator Overloading

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Intermediate Programming & Design (C++) Classes in C++

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

Operator Overloading in C++ Systems Programming

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

CPSC 427: Object-Oriented Programming

CS 247: Software Engineering Principles. ADT Design

Programming in C/C Lecture 3

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

C++ For Science and Engineering Lecture 27

Object-Oriented Programming in C++

CpSc212 Goddard Notes Chapter 10. Linked Lists

PIC 10A. Lecture 17: Classes III, overloading

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSCI 104 Templates. Mark Redekopp David Kempe

Abstract Data Types (ADT) and C++ Classes

Circle all of the following which would make sense as the function prototype.

Intermediate Programming, Spring 2017*

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Object Oriented Programming COP3330 / CGS5409

Review for Test 1 (Chapter 1-5)

Chapter 17: Linked Lists

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Class and Function Templates

CSI33 Data Structures

Interview Questions of C++

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

Object oriented programming

Topics. bool and string types input/output library functions comments memory allocation templates classes

Do not write in this area TOTAL. Maximum possible points: 75

Function Overloading

Unified Modeling Language a case study

Come and join us at WebLyceum

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

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

Introduction to the C programming language

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Introduction to the C programming language

Chapter 18 - C++ Operator Overloading

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Financial computing with C++

lecture09: Linked Lists

Chapter 17: Linked Lists

Due Date: See Blackboard

Defensive Programming

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Ch 2 ADTs and C++ Classes

Comp151. Generic Programming: Container Classes

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

CMPT 135: Midterm Answer Key

CPSC 427: Object-Oriented Programming

Expression Trees and the Heap

Shahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods

Transcription:

EECE.3220: Data Structures Spring 2017 Lecture 14: Key Questions February 24, 2017 1. Describe the characteristics of an ADT to store a list. 2. What data members would be necessary for a static array-based list implementation? 1

EECE.3220: ECE Application Programming Spring 2017 M. Geiger Lecture 14: Key Questions 3. Describe the basic algorithm for inserting an element into an array-based list. 4. Describe the basic algorithm for removing an element from an array-based list. 2

EECE.3220: ECE Application Programming Spring 2017 M. Geiger Lecture 14: Key Questions 5. Describe the key characteristics of the static array-based List implementation (files attached) 6. Explain the use of the const keyword with variables, arguments, and methods. 3

EECE.3220: ECE Application Programming Spring 2017 M. Geiger Lecture 14: Key Questions 7. Explain the general practice of operator overloading and the specific example (<<) shown for the List class, both as a true non-member function and a friend function. 4

EECE.3220: ECE Application Programming Spring 2017 M. Geiger Lecture 14: Key Questions 8. Explain the new and delete operators. 9. If we implement a list using dynamic arrays, what parts of the class stay the same? What s different? What needs to be added? 5

EECE.3220: ECE Application Programming Spring 2017 M. Geiger Lecture 14: Key Questions 10. Explain how the constructor needs to change in a dynamically allocated list. 11. Explain the purpose and operation of a destructor. 6

EECE.3220: ECE Application Programming Spring 2017 M. Geiger Lecture 14: Key Questions 12. Explain the purpose and operation of copy constructors and the = operator. Why are the default versions of these functions not suitable for objects with dynamically allocated members? 7

List.h List.h ds_test Created by Michael Geiger on 2/24/17. Figure 6.1A from Nyhoff text #include <iostream> using std::ostream; #ifndef LIST #define LIST const int CAPACITY = 1024; typedef int ElementType; Maximum array size Define "ElementType" as generic type name Can change specific type by changing "int" class List { public: /******** Function Members ********/ /***** Class constructor *****/ List(); Construct a List object. Precondition: None Postcondition: An empty List object has been constructed; mysize is 0. /***** empty operation *****/ bool empty() const; Check if a list is empty. Precondition: None Postcondition: true is returned if the list is empty, false if not. /***** insert and erase *****/ void insert(elementtype item, int pos); Insert a value into the list at a given position. Precondition: item is the value to be inserted; there is room in the array (mysize < CAPACITY); and the position satisfies 0 <= pos <= mysize. Postcondition: item has been inserted into the list at the position determined by pos (provided there is room and pos is a legal position). void erase(int pos); Page 1 of 2

List.h Remove a value from the list at a given position. Precondition: The list is not empty and the position satisfies 0 <= pos < mysize. Postcondition: element at the position determined by pos has been removed (provided pos is a legal position). ----------------------------------------------------------------------*/ /***** output *****/ void display(ostream & out) const; Display a list. Precondition: out is a reference parameter Postcondition: The list represented by this List object has been inserted into ostream out. private: /******** Data Members ********/ int mysize; current size of list stored in myarray ElementType myarray[capacity]; array to store list elements ; --- end of List class ------ Prototype of output operator ostream & operator<< (ostream & out, const List & alist); #endif Page 2 of 2

List.cpp List.cpp ds_test Created by Michael Geiger on 2/24/17. Figure 6.1B from Nyhoff text #include "List.h" using namespace std; --- Definition of class constructor List::List() : mysize(0) { --- Definition of empty() bool List::empty() const { return (mysize == 0); --- Definition of display() void List::display(ostream & out) const { for (int i = 0; i < mysize; i++) out << myarray[i] << " "; --- Definition of output operator ostream & operator<< (ostream & out, const List & alist) { alist.display(out); return out; --- Definition of insert() void List::insert(ElementType item, int pos) { if (mysize == CAPACITY) { cerr << "*** No space for list element -- terminating " "execution ***\n"; exit(1); if (pos < 0 pos > mysize) { cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. ***\n"; return; First shift array elements right to make room for item for(int i = mysize; i > pos; i--) myarray[i] = myarray[i - 1]; Now insert item at position pos and increase list size myarray[pos] = item; mysize++; Definition of erase() Page 1 of 2

List.cpp void List::erase(int pos) { if (mysize == 0) { cerr << "*** List is empty ***\n"; return; if (pos < 0 pos >= mysize) { cerr << "Illegal location to delete -- " << pos << ". List unchanged. ***\n"; return; Shift array elements left to close the gap for(int i = pos; i < mysize; i++) myarray[i] = myarray[i + 1]; Decrease list size mysize--; Page 2 of 2

DList.h List.h ds_test Created by Michael Geiger on 2/24/17. Figure 6.2A from Nyhoff text /*-- List.h --------------------------------------------------------------- This header file defines the data type List for processing lists. Basic operations are: Constructor Destructor Copy constructor Assignment operator empty: Check if list is empty insert: Insert an item erase: Remove an item display: Output the list << : Output operator -- #include <iostream> using std::ostream; #ifndef LIST #define LIST typedef int ElementType; class List { public: /******** Function Members ********/ /***** Class constructor *****/ List(int maxsize = 1024); Construct a List object. Precondition: maxsize is a positive integer with default value 1024. Postcondition: An empty List object is constructed; mycapacity == maxsize (default value 1024); myarrayptr points to a run-time array with mycapacity as its capacity; and mysize is 0. /***** Class destructor *****/ ~List(); Destroys a List object. Precondition: The life of a List object is over. Postcondition: The memory dynamically allocated by the constructor for the array pointed to by myarrayptr has been returned to the heap. ------------------------------------------------------------------*/ Page 1 of 3

DList.h /***** Copy constructor *****/ List(const List & origlist); Construct a copy of a List object. Precondition: A copy of origlist is needed; origlist is a const reference parameter. Postcondition: A copy of origlist has been constructed. /***** Assignment operator *****/ List & operator=(const List & origlist); Assign a copy of a List object to the current object. Precondition: none Postcondition: A copy of origlist has been assigned to this object. A reference to this list is returned. /***** empty operation *****/ bool empty() const; Check if a list is empty. Precondition: None Postcondition: true is returned if the list is empty, false if not. /***** insert and erase *****/ void insert(elementtype item, int pos); Insert a value into the list at a given position. Precondition: item is the value to be inserted; there is room in the array (mysize < CAPACITY); and the position satisfies 0 <= pos <= mysize. Postcondition: item has been inserted into the list at the position determined by pos (provided there is room and pos is a legal position). void erase(int pos); Remove a value from the list at a given position. Precondition: The list is not empty and the position satisfies 0 <= pos < mysize. Postcondition: element at the position determined by pos has been removed (provided pos is a legal position). ----------------------------------------------------------------------*/ /***** output *****/ void display(ostream & out) const; Page 2 of 3

DList.h Display a list. Precondition: The ostream out is open. Postcondition: The list represented by this List object has been inserted into out. private: /******** Data Members ********/ int mysize; current size of list stored in array int mycapacity; capacity of array ElementType * myarrayptr; pointer to dynamically-allocated array ; --- end of List class ------ Prototype of output operator ostream & operator<< (ostream & out, const List & alist); #endif Page 3 of 3

DList.cpp List.cpp ds_test Created by Michael Geiger on 2/24/17. Figure 6.2B from Nyhoff text #include <cassert> #include <new> using namespace std; Necessary for (nothrow) version of new #include "DList.h" --- Definition of class constructor List::List(int maxsize) : mysize(0), mycapacity(maxsize) { myarrayptr = new(nothrow) ElementType[maxSize]; assert(myarrayptr!= 0); --- Definition of class destructor List::~List() { delete [] myarrayptr; --- Definition of the copy constructor List::List(const List & origlist) : mysize(origlist.mysize), mycapacity(origlist.mycapacity) { --- Get new array for copy myarrayptr = new(nothrow) ElementType[myCapacity]; if (myarrayptr!= 0) check if memory available --- Copy origlist's array into this new array for(int i = 0; i < mycapacity; i++) myarrayptr[i] = origlist.myarrayptr[i]; else { cerr << "*Inadequate memory to allocate List ***\n"; exit(1); --- Definition of the assignment operator List & List::operator=(const List & origlist) { if (this!= &origlist) { check for list = list mysize = origlist.mysize; mycapacity = origlist.mycapacity; -- Allocate a new array if necessary if (mycapacity!= origlist.mycapacity) { delete[] myarrayptr; myarrayptr = new(nothrow) ElementType[myCapacity]; if (myarrayptr == 0) check if memory available Page 1 of 3

DList.cpp { cerr << "*Inadequate memory to allocate stack ***\n"; exit(1); --- Copy origlist's array into this new array for(int i = 0; i < mycapacity; i++) myarrayptr[i] = origlist.myarrayptr[i]; return *this; --- Definition of empty() bool List::empty() const { return mysize == 0; --- Definition of display() void List::display(ostream & out) const { for (int i = 0; i < mysize; i++) out << myarrayptr[i] << " "; --- Definition of output operator ostream & operator<< (ostream & out, const List & alist) { alist.display(out); return out; --- Definition of insert() void List::insert(ElementType item, int pos) { if (mysize == mycapacity) { cerr << "*** No space for list element -- terminating " "execution ***\n"; exit(1); if (pos < 0 pos > mysize) { cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. ***\n"; return; First shift array elements right to make room for item for(int i = mysize; i > pos; i--) myarrayptr[i] = myarrayptr[i - 1]; Now insert item at position pos and increase list size myarrayptr[pos] = item; mysize++; --- Definition of erase() void List::erase(int pos) { if (mysize == 0) { cerr << "*** List is empty ***\n"; return; Page 2 of 3

DList.cpp if (pos < 0 pos >= mysize) { cerr << "Illegal location to delete -- " << pos << ". List unchanged. ***\n"; return; Shift array elements left to close the gap for(int i = pos; i < mysize; i++) myarrayptr[i] = myarrayptr[i + 1]; Decrease list size mysize--; Page 3 of 3