Carrano - Chapter 3CS 150141 List Abstract Data Types We have reached the point where we can customize our own versions of list types that do not encounter.

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Carrano - Chapter 4CS Arrays: Advantages and Disadvantages Advantages: Random access of elements is facilitated via indices.  To find a[i], merely.
CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Savitch - Chapters 9&11 CS Pointers A pointer holds the memory address of a variable. Main Memory Byte # Byte # Byte # Byte.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Modeling Data with Arrays, Structs, & Classes Chapter 11.
Savitch - Chapter 11CS Extending the Class Capabilities Let’s extend the definition of the BankAccount class to illustrate some additional capabilities.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Data Abstraction and Object-Oriented Design Problem Solving,
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
11 Introduction to Object Oriented Programming (Continued) Cats.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 C++ Classes and Data Structures Course link…..
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
CSCE 210 Data Structures and Algorithms

A pointer is the memory address of a variable.
Chapter 13 Applied Arrays: Lists and Strings
Data Abstraction: The Walls
Problems With Character Arrays
Chapter Structured Types, Data Abstraction and Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Chapter 4 Linked Lists
Chapter 4 Linked Lists.
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 4 Linked Lists.
Data Abstraction: The Walls
Operator Overloading.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS150 Introduction to Computer Science 1
CIS 199 Final Review.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 18 - Operator Overloading
Data Abstraction: The Walls
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
File I/O in C++ I.
Presentation transcript:

Carrano - Chapter 3CS List Abstract Data Types We have reached the point where we can customize our own versions of list types that do not encounter the problems we normally associate with traditional C++ arrays. We’d like to be able to do the following: Pass a list by value to a function! Have a function that returns a list! Assign a list a new value with an assignment statement! Insert a new element in the list at a particular location, and have the list make room for the new element! Remove an element from the list, and have the list adjust the subsequent elements so there’s no gap between elements! Access a list element at a particular position without “crashing” if the index is inappropriate! Access the first element at position #1 and the last element at position #length!

Carrano - Chapter 3CS To Start Out: A New PhoneListing Class ///////////////////////////////////////////////////// // phoneListing.h // // The class definition for the PhoneListing class // ///////////////////////////////////////////////////// #ifndef PHONE_LISTING_H #include using namespace std; class PhoneListing { public: // Constructor PhoneListing(); PhoneListing(string newName, string newPhoneNumber); // Member functions void setName(string newName); void setPhoneNumber(string newPhoneNumber); // Overloaded operators PhoneListing& operator = (const PhoneListing &listing); bool operator == (PhoneListing listing); bool operator < (PhoneListing listing); bool operator <= (PhoneListing listing); bool operator > (PhoneListing listing); bool operator >= (PhoneListing listing); bool operator != (PhoneListing listing);

Carrano - Chapter 3CS phoneListing.h (Continued) // Accessor functions string getName() const; string getPhoneNumber() const; // Friend functions friend istream& operator >> (istream &sourceFileStream, PhoneListing &listing); friend ostream& operator << (ostream &destinationFileStream, const PhoneListing &listing); private: // Data members string name; string phoneNumber; }; #define PHONE_LISTING_H #endif

Carrano - Chapter 3CS phoneListing.cpp ////////////////////////////////////////////////////////// // phoneListing.cpp // // The class implementation for the PhoneListing class. // ////////////////////////////////////////////////////////// #include #include "phoneListing.h" using namespace std; // This default constructor sets up the data members with no values. // PhoneListing::PhoneListing() { } // This initializing constructor sets up the data members in the obvious way. // PhoneListing::PhoneListing(string newName, string newPhoneNumber) { name = newName; phoneNumber = newPhoneNumber; } // This member function sets the PhoneListing's name // // data member to the parameterized value. // void PhoneListing::setName(string newName) { name = newName; return; }

Carrano - Chapter 3CS phoneListing.cpp (Continued) // This member function sets the PhoneListing's phoneNumber // // data member to the parameterized value. // void PhoneListing::setPhoneNumber(string newPhoneNumber) { phoneNumber = newPhoneNumber; return; } // Assignment operator: sets both data members of *this to copies // // of their counterparts in the parameterized PhoneListing. // PhoneListing& PhoneListing::operator = (const PhoneListing &listing) { name = listing.name; phoneNumber = listing.phoneNumber; return *this; } // Equality operator: based upon equality between PhoneListings' name values. // bool PhoneListing::operator == (PhoneListing listing) { return (name == listing.name); } // Less-than operator: based upon whether name value of *this // // is less than name value of parameterized PhoneListing. // bool PhoneListing::operator < (PhoneListing listing) { return (name < listing.name); }

Carrano - Chapter 3CS phoneListing.cpp (Continued) // Less-than-or-equal-to operator: based upon whether name value of *this // // is less than or equal to name value of parameterized PhoneListing. // bool PhoneListing::operator <= (PhoneListing listing) { return (name <= listing.name); } // Greater-than operator: based upon whether name value of *this // // is greater than name value of parameterized PhoneListing. // bool PhoneListing::operator > (PhoneListing listing) { return (name > listing.name); } // Greater-than-or-equal-to operator: based upon whether name value of *this // // is greater than or equal to name value of parameterized PhoneListing. // bool PhoneListing::operator >= (PhoneListing listing) { return (name >= listing.name); } // Inequality operator: based upon inequality between PhoneListings' name values. // bool PhoneListing::operator != (PhoneListing listing) { return (name != listing.name); }

Carrano - Chapter 3CS phoneListing.cpp (Continued) // Accessor function for the value of the name data member. // string PhoneListing::getName() const { return name; } // Accessor function for the value of the phoneNumber data member. // string PhoneListing::getPhoneNumber() const { return phoneNumber; } // Input operator for the PhoneListing class: retrieves // // name value, followed by phoneNumber value. // istream& operator >> (istream &inputFileStream, PhoneListing &listing) { inputFileStream >> listing.name >> listing.phoneNumber; return inputFileStream; } // Output operator for the PhoneListing class: outputs // // name value, followed by phoneNumber value. // ostream& operator << (ostream &outputFileStream, const PhoneListing &listing) { outputFileStream << listing.name << ": " << listing.phoneNumber; return outputFileStream; }

Carrano - Chapter 3CS Now Let’s Set Up Our Own List Class! ///////////////////////////////////////////// // list.h // // The class definition for the List class // ///////////////////////////////////////////// #ifndef LIST_H #include #include "phoneListing.h" using namespace std; const int MAX_LIST_SIZE = 100; typedef PhoneListing elementType; class List { public: // Class constructors List(); List(const List &lst); // Member functions int getLength() const; elementType& operator [ ] (int position); List& operator = (const List &lst); bool insert(int position, elementType elt); bool remove(int position); bool retrieve(int position, elementType &elt); protected: // Data members elementType entry[MAX_LIST_SIZE]; int length; // Member function int Index(int position) const;}; #define LIST_H #endif

Carrano - Chapter 3CS list.cpp ////////////////////////////////////////////////// // list.cpp // // The class implementation for the List class. // ////////////////////////////////////////////////// #include #include "list.h" #include "phoneListing.h" using namespace std; // This default constructor sets up the List as one with no elements. // List::List() { length = 0; } // This copy constructor sets up the *this List // // as a duplicate of the parameterized List. // List::List(const List &lst) { length = lst.getLength(); for (int i = 1; i <= length; i++) entry[Index(i)] = lst.entry[Index(i)]; } // This accessor function retrieves the value of the length data member. // int List::getLength() const { return length; }

Carrano - Chapter 3CS list.cpp (Continued) // The subscript operator retrieves the element in the parameterized position // // of the entry data member. Note that the starting index for this operator // // is one, unlike the standard C++ starting index of zero. // elementType& List::operator [ ] (int position) { assert((position > 0) && (position <= length)); return entry[Index(position)]; } // The assignment operator gives the *this List duplicate // // values for each data member in the parameterized List. // List& List::operator = (const List &lst) { length = lst.getLength(); for (int i = 1; i <= length; i++) entry[Index(i)] = lst.entry[Index(i)]; return *this; }

Carrano - Chapter 3CS list.cpp (Continued) // This member function inserts the parameterized element into the *this List // // at the parameterized position (if there's room and the position is kosher). // bool List::insert(int position, elementType elt) { if ((position length + 1) || (length >= MAX_LIST_SIZE)) return false; else { length++; for (int i = length; i > position; i--) entry[Index(i)] = entry[Index(i-1)]; entry[Index(position)] = elt; return true; } // This member function removes the element at the parameterized // // position from the *this List (if the position is valid ). // bool List::remove(int position) { if ((position length)) return false; else { for (int i = position+1; i <= length; i++) entry[Index(i-1)] = entry[Index(i)]; length--; return true; }

Carrano - Chapter 3CS list.cpp (Continued) // This member function retrieves the element at the parameterized // // position from the *this List (if the position is kosher). // bool List::retrieve(int position, elementType &elt) { if ((position length)) return false; else { elt = entry[Index(position)]; return true; } // This member function converts the parameterized position into the // // corresponding index of the array that is the entry data member. // int List::Index(int position) const { return position-1; }

Carrano - Chapter 3CS Now Let’s Test The List Class ////////////////////////////////////////////////////////////// // phoneDriver.cpp // // // This program file tests the List class by loading a List // // variable and searching it with a user-supplied value. // ////////////////////////////////////////////////////////////// #include #include "list.h" #include "phoneListing.h" using namespace std; List queryUserForDirectory(); string queryUserForName(); bool searchDirectory(List directory, string personsName, PhoneListing &listing); void outputListing(PhoneListing listing); void outputNoListing(string personsName);

Carrano - Chapter 3CS phoneDriver.cpp (Continued) /////////////////////////////////////////////////////////////////////////////// // The main function coordinates the user queries for the input file and the // // name being sought in the PhoneListing that is input. It also coordinates // // the output of the sought information (or its unavailability). // /////////////////////////////////////////////////////////////////////////////// void main() { List phoneBook; string soughtName; PhoneListing soughtPhoneListing; phoneBook = queryUserForDirectory(); soughtName = queryUserForName(); if (searchDirectory(phoneBook, soughtName, soughtPhoneListing)) outputListing(soughtPhoneListing); else outputNoListing(soughtName); return; }

Carrano - Chapter 3CS phoneDriver.cpp (Continued) /////////////////////////////////////////////////////////////////////// // This function asks the user for the file name containing the List // // data, and then retrieves that data, using an end-of-file signal. // /////////////////////////////////////////////////////////////////////// List queryUserForDirectory() { ifstream directoryFile; char directoryFileName[50]; List directory; PhoneListing nextListing; int position = 1; cout << "Enter the file name of the telephone directory data: "; cin >> directoryFileName; directoryFile.open(directoryFileName); directoryFile >> nextListing; while (!directoryFile.eof()) { directory.insert(position, nextListing); position++; directoryFile >> nextListing; } directoryFile.close(); return directory; }

Carrano - Chapter 3CS phoneDriver.cpp (Continued) ///////////////////////////////////////////////// // This function asks the user for a string // // value that will be used to search the List. // ///////////////////////////////////////////////// string queryUserForName() { string name; cout << "Enter the name of the party whose number you want: "; cin >> name; return name; } //////////////////////////////////////////////////////////////////////////////////// // This function searches the parameterized List for a PhoneListing with the // // parameterized personsName. If it finds it, it sets the PhoneListing parameter // // and returns the value true; otherwise, it merely returns the value false. // //////////////////////////////////////////////////////////////////////////////////// bool searchDirectory(List directory, string personsName, PhoneListing &listing) { PhoneListing dummyListing(personsName, ""); for (int i = 1; i <= directory.getLength(); i++) { directory.retrieve(i, listing); if (dummyListing == listing) return true; } return false; }

Carrano - Chapter 3CS phoneDriver.cpp (Continued) ///////////////////////////////////////////////////////////////////////////// // This function outputs the PhoneListing found after a successful search. // ///////////////////////////////////////////////////////////////////////////// void outputListing(PhoneListing listing) { cout << "Your requested listing - " << listing << endl << endl; return; } ////////////////////////////////////////////////////////////////////////////////// // This function outputs a message indicating that the search was unsuccessful. // ////////////////////////////////////////////////////////////////////////////////// void outputNoListing(string personsName) { cout << "No listing with name: " << personsName << endl << endl; return; }

Carrano - Chapter 3CS phoneDriver.cpp (Continued) ACCT2633 AS3180 ANTH2744 ART3071 BIOL3927 BSED2504 CHEM2042 CE2533 CMIS2504 CS2386 CNST2088 CI3082 DANC2773 ESCI3620 ECON2542 EDUC3277 EDAD3277 ECE2524 ENG2060 ENVS3311 FIN2638 FL3510 GEOG2090 GRN3454 HED3252 HIST2414 IE3389 IT3277 MGMT2750 MS2638 MKTG3221 MC2230 MATH2382 ME3389 MUS3900 NURS3956 PHIL2250 PHYS2472 POLS3572 PROD2638 PSYC2202 PAPA3762 SOCW5758 SOC3713 SPE5423 SPC3090 SPPA3662 THEA2773 WMST5060 Contents of phoneData.txt Sample Output

Carrano - Chapter 3CS The Sorted List Abstract Data Type We will now develop a class that implements a list that is always kept sorted, using whatever definition of sorting is associated with the type of element held in the list. We’d like to be able to do the following: Insert a new element and have it automatically placed in its proper position in the list! Use a binary search to find elements in the list, and to locate where new elements should go in the list! Plus all of the other things we could do with our regular list!

Carrano - Chapter 3CS Defining Our SortedList Class class SortedList { public: // Class constructors SortedList(); SortedList(const SortedList &srtLst); // Member functions int getLength() const; elementType& operator [ ] (int position); SortedList& operator = (const SortedList &srtLst); bool insert(elementType elt); bool remove(elementType elt); bool retrieve(elementType elt, int &position); protected: // Data members elementType entry[MAX_LIST_SIZE]; int length; // Member function int Index(int position) const; bool binarySearch(int firstPosition, int lastPosition, elementType soughtElt, int &position); }; #define SORTED_LIST_H #endif ////////////////////////////// // sortedList.h // // The class definition for // // the SortedList class // ////////////////////////////// #ifndef SORTED_LIST_H #include #include "phoneListing.h" using namespace std; const int MAX_LIST_SIZE = 100; typedef PhoneListing elementType;

Carrano - Chapter 3CS sortedList.cpp //////////////////////////////////////////////////////// // sortedList.cpp // // The class implementation for the SortedList class. // //////////////////////////////////////////////////////// #include #include "sortedList.h" #include "phoneListing.h" using namespace std; // This default constructor sets up the SortedList as one with no elements. // SortedList::SortedList() { length = 0; } // This copy constructor sets up the *this SortedList // // as a duplicate of the parameterized SortedList. // SortedList::SortedList(const SortedList &lst) { length = lst.getLength(); for (int i = 1; i <= length; i++) entry[Index(i)] = lst.entry[Index(i)]; } // This accessor function retrieves the value of the length data member. // int SortedList::getLength() const { return length; }

Carrano - Chapter 3CS sortedList.cpp (Continued) // The subscript operator retrieves the element in the parameterized position // // of the entry data member. Note that the starting index for this operator // // is one, unlike the standard C++ starting index of zero. // elementType& SortedList::operator [ ] (int position) { assert((position > 0) && (position <= length)); return entry[Index(position)]; } // The assignment operator gives the *this SortedList duplicate // // values for each data member in the parameterized List. // SortedList& SortedList::operator = (const SortedList &srtLst) { length = srtLst.getLength(); for (int i = 1; i <= length; i++) entry[Index(i)] = srtLst.entry[Index(i)]; return *this; }

Carrano - Chapter 3CS sortedList.cpp (Continued) // This member function inserts the parameterized element // // into the *this SortedList at the appropriate position // // (if there's room). // bool SortedList::insert(elementType elt) { int position; if (length >= MAX_LIST_SIZE) return false; else { retrieve(elt, position); length++; for (int i=length; i > position; i--) entry[Index(i)] = entry[Index(i-1)]; entry[Index(position)] = elt; return true; } // This member function removes the // // parameterized element from the *this // // SortedList (if it’s in the list). // bool SortedList::remove(elementType elt) { int position; if (!retrieve(elt, position)) return false; else { for (int i=position+1; i <= length; i++) entry[Index(i-1)] = entry[Index(i)]; length--; return true; }

Carrano - Chapter 3CS sortedList.cpp (Continued) // This member function retrieves the parameterized element // // from the *this SortedList (if it’s in the list), and sets // // the position parameter to its location in the list. // bool SortedList::retrieve(elementType elt, int &position) { return binarySearch(1, length, elt, position); } // This member function converts the parameterized position into the // // corresponding index of the array that is the entry data member. // int SortedList::Index(int position) const { return position-1; }

Carrano - Chapter 3CS sortedList.cpp (Continued) // This member function performs a binary search on the SortedList for // // the soughtElt value. If it is successful, then parameter position // // will be assigned the location in the entry data member where the // // soughtElt was found; otherwise, it is assigned the location where // // it would be found if it were in the entry data member. // bool SortedList::binarySearch(int firstPosition, int lastPosition, elementType soughtElt, int &position) { int middlePosition = (firstPosition + lastPosition) / 2; if (lastPosition < firstPosition) { position = firstPosition; return false; } else if (entry[Index(middlePosition)] == soughtElt) { position = middlePosition; return true; } else if (entry[Index(middlePosition)] < soughtElt) return binarySearch(middlePosition+1, lastPosition, soughtElt, position); else return binarySearch(firstPosition, middlePosition-1, soughtElt, position); }

Carrano - Chapter 3CS Testing The SortedList Class ////////////////////////////////////////////////////////////////////// // phoneDriver2.cpp // // // This program file tests the SortedList class by loading a // // SortedList variable and searching it with a user-supplied value. // ////////////////////////////////////////////////////////////////////// #include #include "sortedList.h" #include "phoneListing.h" using namespace std; SortedList queryUserForDirectory(); void outputDirectory(SortedList directory); string queryUserForName(); bool searchDirectory(SortedList directory, string personsName, PhoneListing &listing); void outputListing(PhoneListing listing); void outputNoListing(string personsName);

Carrano - Chapter 3CS phoneDriver2.cpp (Continued) /////////////////////////////////////////////////////////////////////////////// // The main function coordinates the user queries for the input file and the // // name being sought in the PhoneListing that is input. It also coordinates // // the output of the sought information (or its unavailability). // /////////////////////////////////////////////////////////////////////////////// void main() { SortedList phoneBook; string soughtName; PhoneListing soughtPhoneListing; phoneBook = queryUserForDirectory(); outputDirectory(phoneBook); soughtName = queryUserForName(); if (searchDirectory(phoneBook, soughtName, soughtPhoneListing)) outputListing(soughtPhoneListing); else outputNoListing(soughtName); return; }

Carrano - Chapter 3CS phoneDriver2.cpp (Continued) /////////////////////////////////////////////////////////////////////// // This function asks the user for the file name containing the List // // data, and then retrieves that data, using an end-of-file signal. // /////////////////////////////////////////////////////////////////////// SortedList queryUserForDirectory() { ifstream directoryFile; char directoryFileName[50]; SortedList directory; PhoneListing nextListing; cout << "Enter the file name of the telephone directory data: "; cin >> directoryFileName; directoryFile.open(directoryFileName); directoryFile >> nextListing; while (!directoryFile.eof()) { directory.insert(nextListing); directoryFile >> nextListing; } directoryFile.close(); return directory; }

Carrano - Chapter 3CS phoneDriver2.cpp (Continued) //////////////////////////////////////////////////////////////// // This function outputs the entire parameterized SortedList. // //////////////////////////////////////////////////////////////// void outputDirectory(SortedList directory) { for (int i = 1; i <= directory.getLength(); i++) cout << directory[i] << endl; cout << endl; return; } //////////////////////////////////////////////////// // This function asks the user for a string value // // that will be used to search the SortedList. // //////////////////////////////////////////////////// string queryUserForName() { string name; cout << "Enter the name of the party whose number you want: "; cin >> name; return name; }

Carrano - Chapter 3CS phoneDriver2.cpp (Continued) ///////////////////////////////////////////////////////////////////////////////////// // This function searches the parameterized SortedList for a PhoneListing with the // // parameterized personsName. If it finds it, it sets the PhoneListing parameter // // and returns the value true; otherwise, it merely returns the value false. // ///////////////////////////////////////////////////////////////////////////////////// bool searchDirectory(SortedList directory, string personsName, PhoneListing &listing) { int position; PhoneListing dummyListing(personsName, ""); if (directory.retrieve(dummyListing, position)) { listing = directory[position]; return true; } else return false; } ///////////////////////////////////////////////////////////////////////////// // This function outputs the PhoneListing found after a successful search. // ///////////////////////////////////////////////////////////////////////////// void outputListing(PhoneListing listing) { cout << "Your requested listing - " << listing << endl << endl; return; }

Carrano - Chapter 3CS phoneDriver2.cpp (Continued) ///////////////////////////////////////////////////////////////////////////// // This function outputs the PhoneListing found after a successful search. // ///////////////////////////////////////////////////////////////////////////// void outputListing(PhoneListing listing) { cout << "Your requested listing - " << listing << endl << endl; return; } ////////////////////////////////////////////////////////////////////////////////// // This function outputs a message indicating that the search was unsuccessful. // ////////////////////////////////////////////////////////////////////////////////// void outputNoListing(string personsName) { cout << "No listing with name: " << personsName << endl << endl; return; }

Carrano - Chapter 3CS phoneDriver2.cpp (Continued)