Data Structures. data object. set or collection of instances. integer = {0, +1, -1, +2, -2, +3, -3, } daysofweek = {S,M,T,W,Th,F,Sa}

Similar documents
Data Structures and Programming 資料結構與程式設計. Topic 3 Linear Lists Array. AbstractDataType linearlist. Linear List - Array Representation

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Chapter1 Basic Concepts

Programming Abstractions

Data Structures and Programming 資料結構與程式設計. Topic 6 Stacks. Stacks. Stack of Cups. Stacks. top. top. bottom. bottom

Example:(problem) int a,b cin >> a cin >> b // b = 0

C++ Programming Assignment 3

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

CSCE 110 PROGRAMMING FUNDAMENTALS

Lab 2: ADT Design & Implementation

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

1 #include <iostream> 2 using namespace std; 3 4 // implementing the dynamic List ADT using Linked List 5 6 class Node{ 7 8 private: 9 int data; 10

05-01 Discussion Notes

Object oriented programming

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

Operator overloading: extra examples

Programming II (CS300)

EECE.3220: Data Structures Spring 2017

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

Comp151. Generic Programming: Container Classes

Programming in C++: Assignment Week 8

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

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

Object oriented programming

pointers & references

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

1. Which of the following best describes the situation after Line 1 has been executed?

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

TEMPLATES AND EXCEPTION HANDLING

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Introduction to Programming using C++

Assignment 5: MyString COP3330 Fall 2017

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

G52CPP C++ Programming Lecture 17

CS 247: Software Engineering Principles. ADT Design

Assignment of Objects

Due Date: See Blackboard

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Programming, numerics and optimization

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

1 Short Answer (5 Points Each)

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Operator Overloading and Templates. Linear Search. Linear Search in C++ second attempt. Linear Search in C++ first attempt

Exceptions, Case Study-Exception handling in C++.

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

Object oriented programming

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

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Solved Exercises from exams: Midterm(1)

Operator Overloading in C++ Systems Programming

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Pointers. Developed By Ms. K.M.Sanghavi

CSCE 110 PROGRAMMING FUNDAMENTALS

COMP322 - Introduction to C++ Lecture 01 - Introduction

Midterm Examination. Instructor: Gary Chan Date: Saturday, 23 October 2010 Time: 2:30pm 4:00pm Venue: LTC

Example Final Questions Instructions

Review Questions for Final Exam

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

Exercise 6.2 A generic container class

PRACTICE MIDTERM EXAM #2

1 Short Answer (7 Points Each)

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

Data Structures and Algorithms

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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)

The Class Construct Part 2

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Do not turn to the next page until the start of the exam.

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

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

Special Member Functions

CSI33 Data Structures

CSCE 206: Structured Programming in C++

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

Ch6. Linear Lists Linked Representation

COP Programming Assignment #7

Linked List using a Sentinel

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

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

A class is a user-defined type. It is composed of built-in types, other user-defined types and

Example Final Questions Instructions

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Due Date: See Blackboard

Transcription:

Data Structures data object set or collection of instances integer = {0, +1, -1, +2, -2, +3, -3, } daysofweek = {S,M,T,W,Th,F,Sa}

Data Object instances may or may not be related mydataobject = {apple, chair, 2, 5.2, red, green, Jack}

Data Structure Data object + relationships that exist among instances and elements that comprise an instance Among instances of integer 369 < 370 280 + 4 = 284

Ordering Relationships You can have ordering relationships 2 < 1< 0 < 1 (relational operators such as <, >, =, etc). Elements could be related by operations such as add, subtract, etc.

Data Structure Among elements that comprise an instance 369 3 is more significant than 6 3 is immediately to the left of 6 9 is immediately to the right of 6

Linear (or Ordered) Lists instances are of the form (e 0, e 1, e 2,, e n-1 ) where e i denotes a list element n >= 0 is finite list size is n

Linear Lists L = (e 0, e 1, e 2, e 3,, e n-1 ) relationships e 0 is the zero th (or front) element e n-1 is the last element e i immediately precedes e i+1

Linear List Examples/Instances Students in COP3530 = (Jack, Jill, Abe, Henry, Mary,, Judy) Exams in COP3530 = (exam1, exam2, exam3) Days of Week = (S, M, T, W, Th, F, Sa) Months = (Jan, Feb, Mar, Apr,, Nov, Dec)

Linear List Operations size() determine list size L = (a,b,c,d,e) size = 5

Linear List Operations get(theindex) get element with given index L = (a,b,c,d,e) get(0) = a get(2) = c get(4) = e get(-1) = error get(9) = error

Linear List Operations indexof(theelement) determine the index of an element L = (a,b,d,b,a) indexof(d) = 2 indexof(a) = 0 indexof(z) = -1

Linear List Operations remove(theindex) remove and return element with given index remove(2) returns c L = (a,b,c,d,e,f,g) and L becomes (a,b,d,e,f,g) index of d,e,f, and g decrease by 1

Linear List Operations remove(theindex) remove and return element with given index L = (a,b,c,d,e,f,g) remove(-1) => error remove(20) => error

Linear List Operations add(theindex, theelement) add an element so that the new element has a specified index L = (a,b,c,d,e,f,g) add(0,h) => L = (h,a,b,c,d,e,f,g) index of a,b,c,d,e,f, and g increase by 1

Linear List Operations add(theindex, theelement) L = (a,b,c,d,e,f,g) add(2,h) => L = (a,b,h,c,d,e,f,g) index of c,d,e,f, and g increase by 1 add(10,h) => error add(-6,h) => error

Data Structure Specification Language independent Abstract Data Type C++ Abstract Class

Linear List Abstract Data Type AbstractDataType LinearList { } instances ordered finite collections of zero or more elements operations empty(): return true iff the list is empty, false otherwise size(): return the list size (i.e., number of elements in the list) get(index): return the indexth element of the list indexo f(x): return the index of the first occurrence of x in the list, return -1 if x is not in the list erase(index): remove the indexth element, elements with higher index have their index reduced by 1 insert(theindex, x): insert x as the indexth element, elements with theindex >= index have their index increased by 1 output(): output the list elements from left to right

Linear List As C++ Abstract Class template<class T> class linearlist { public: virtual ~linearlist() {}; virtual bool empty() const = 0; virtual int size() const = 0; virtual T& get(int theindex) const = 0; virtual int indexof(const T& theelement)const = 0; virtual void erase(int theindex) = 0; virtual void insert(int theindex, const T& theelement) = 0; virtual void output(ostream& out) const = 0; };

Extending A C++ Class template<class T> class arraylist : public linearlist<t> { // code for all abstract methods of linearlist must come here }

Data Type Of Array element[] Data type of list elements is unknown. Define element[]to be of template typet.

Length of Array element[] Don t know how many elements will be in list. Must pick an initial length and dynamically increase as needed. Use variable arraylengthto store current length of array element[].

Increasing Array Length Length of array element[] is 6. a b c d e f First create a new and larger array T* newarray = new T[15];

Increasing Array Length Now copy elements from old array to new one. a b c d e f a b c d e f copy(element, element + 6, newarray);

Increasing Array Length Finally, delete old array and rename new array. delete [] element; element = newarray; arraylength = 15; element[0] a b c d e f

template<class T> void changelength1d(t*& a, int oldlength, int newlength) { if (newlength < 0) throw illegalparametervalue(); } T* temp = new T[newLength]; // new array int number = min(oldlength, newlength); // number to copy copy(a, a + number, temp); delete [] a; // deallocate old memory a = temp;

How Big Should The New Array Be? At least 1more than current array length. Cost of increasing array length when array is full is Θ(old length). Cost ofninsert operations done on an initially empty linear list increases by Θ(n 2 ).

Space Complexity element[6] a b c d e f newarray = new char[7]; space needed = 2 * newlength 1 = 2 * maxlistsize 1

Array Doubling Double the array length. a b c d e f newarray = new char[12]; a b c d e f Time for n inserts goes up byq(n). Space needed = 1.5*newLength. Space needed <= 3*maxListSize 3

How Big Should The New Array Be? Resizing by any constant factor new length = c * old length increases the cost of n inserts by Θ(n). Resizing by an additive constant increases the cost of n add operations by Θ(n 2 ).

How Big Should The New Array Be? Resizing by any constant factor new length = c * old length requires at most (1+c) * (maxlistsize -1) space. Resizing by an additive constant c requires at most (maxlistsize 1) + (maxlistsize 1 + c) = 2 * (maxlistsize 1) + c space.

What Does C++ Do? STL class vector c = 1.5 arraylist of text c = 2

The Class arraylist General purpose implementation of linear lists. Unknown number of lists.

Create An Empty List arraylist<int> a(100), b; arraylist<double> c(10), d; linearlist<int> *d = new arraylist<int>(1000); linearlist<char> *f = new arraylist<char>(20);

Using A Linear List cout << a.size() << endl; a.insert(0, 2); d->insert(0, 4); a.output(); cout << a << endl; a.erase(0); if (a.empty()) a.insert(0, 5);

Array Of Linear Lists linearlist<int> * x[4]; x[0] = new arraylist<int>(20); x[1] = new chain<int>(); x[2] = new chain<int>(); x[3] = new arraylist<int>(15); for (int i = 0; i < 4; i++) x[i].insert(0, i);

The Class arraylist // include statements come here using namespace std; template<class T> class arraylist : public linearlist<t> { public: // constructor, copy constructor and destructor arraylist(int initialcapacity = 10); arraylist(const arraylist<t>&); ~arraylist() {delete [] element;}

The Class arraylist // ADT methods bool empty() const {return listsize == 0;} int size() const {return listsize;} T& get(int theindex) const; int indexof(const T& theelement) const; void erase(int theindex); void insert(int theindex, const T& theelement); void output(ostream& out) const; // additional method int capacity() const {return arraylength;}

The Class arraylist }; protected: void checkindex(int theindex) const; // throw illegalindex if theindex invalid T* element; // 1D array to hold list elements int arraylength; // capacity of the 1D array int listsize; // number of elements in list

A Constructor template<class T> arraylist<t>::arraylist(int initialcapacity) {// Constructor. if (initialcapacity < 1) {ostringstream s; s << "Initial capacity = " << initialcapacity << " Must be > 0"; throw illegalparametervalue(s.str()); } arraylength = initialcapacity; element = new T[arrayLength]; listsize = 0; }

Copy Constructor template<class T> arraylist<t>::arraylist(const arraylist<t>& thelist) {// Copy constructor. } arraylength = thelist.arraylength; listsize = thelist.listsize; element = new T[arrayLength]; copy(thelist.element, thelist.element + listsize, element);

The Method checkindex template<class T> void arraylist<t>::checkindex(int theindex) const {// Verify that theindex is between 0 and } // listsize - 1. if (theindex < 0 theindex >= listsize) {ostringstream s; } s << "index = " << theindex << " size = " << listsize; throw illegalindex(s.str());

The Method get template<class T> T& arraylist<t>::get(int theindex) const {// Return element whose index is theindex. } // Throw illegalindex exception if no such // element. checkindex(theindex); return element[theindex];

template<class T> The Method indexof int arraylist<t>::indexof(const T& theelement) {// Return index of first occurrence of theelement. // search for theelement } const int theindex = (int) (find(element, element + listsize, theelement) - element); // check if theelement was found if (theindex == listsize) return -1; // not found else return theindex;

The Method erase template<class T> void arraylist<t>::erase(int theindex) {// Delete the element whose index is theindex. checkindex(theindex); // valid index, shift elements with higher // index copy(element + theindex + 1, element + listsize,element + theindex); } element[--listsize].~t(); // invoke destructor

The Method insert template<class T> void arraylist<t>::insert(int theindex, const T& theelement) {// Insert theelement. if (theindex < 0 theindex > listsize) {// invalid index // code to throw an exception comes here } // valid index, make sure we have space if (listsize == arraylength) {// no space, double capacity changelength1d(element, arraylength, 2 * arraylength); arraylength *= 2; }

The Method insert // shift elements right one position copy_backward(element + theindex, element + listsize, element + listsize + 1); element[theindex] = theelement; } listsize++;

The Method output template<class T> void arraylist<t>::output(ostream& out) const {// Put the list into the stream out. copy(element, element + listsize, ostream_iterator<t>(out, " ")); }

Overloading << // overload << template <class T> ostream& operator<<(ostream& out, const arraylist<t>& x) {x.output(out); return out;}