List Abstract Data Types - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

List Abstract Data Types

Description:

We have reached the point where we can customize our own versions of list types ... PAPA 3762. SOCW 5758. SOC 3713. SPE 5423. SPC 3090. SPPA 3662. THEA 2773. WMST 5060 ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 33
Provided by: william732
Learn more at: http://www.cs.siue.edu
Category:
Tags: abstract | data | list | papa | types

less

Transcript and Presenter's Notes

Title: List Abstract Data Types


1
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.
  • Wed 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 theres 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!

2
To Start Out A New PhoneListing Class
//////////////////////////////////////////////////
/// // phoneListing.h
// // The class definition for the
PhoneListing class // ////////////////////////////
///////////////////////// ifndef
PHONE_LISTING_H include ltiostreamgt include
ltstringgt 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 lt (PhoneListing listing)
bool operator lt (PhoneListing listing)
bool operator gt (PhoneListing listing)
bool operator gt (PhoneListing listing)
bool operator ! (PhoneListing listing)
3
phoneListing.h (Continued)
// Accessor functions string
getName() const string getPhoneNumber()
const // Friend functions friend
istream operator gtgt (istream sourceFileStream,
PhoneListing
listing) friend ostream operator ltlt
(ostream destinationFileStream,
const PhoneListing
listing) private // Data members
string name string phoneNumber define
PHONE_LISTING_H endif
4
phoneListing.cpp
//////////////////////////////////////////////////
//////// // phoneListing.cpp
// // The class implementation for
the PhoneListing class. // ///////////////////////
/////////////////////////////////// include
ltiostreamgt include ltstringgt include
"phoneListing.h" using namespace std // This
default constructor sets up the data members with
no values. // PhoneListingPhoneListing() //
This initializing constructor sets up the data
members in the obvious way. // PhoneListingPhone
Listing(string newName, string newPhoneNumber)
name newName phoneNumber
newPhoneNumber // This member function sets
the PhoneListing's name // // data member to the
parameterized value. // void
PhoneListingsetName(string newName) name
newName return
5
phoneListing.cpp (Continued)
// This member function sets the PhoneListing's
phoneNumber // // data member to the
parameterized value. // void
PhoneListingsetPhoneNumber(string
newPhoneNumber) phoneNumber
newPhoneNumber return // Assignment
operator sets both data members of this to
copies // // of their counterparts in the
parameterized PhoneListing.
// PhoneListing PhoneListingoperator (const
PhoneListing listing) name listing.name
phoneNumber listing.phoneNumber return
this // Equality operator based upon
equality between PhoneListings' name values.
// bool PhoneListingoperator (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
PhoneListingoperator lt (PhoneListing
listing) return (name lt listing.name)
6
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 PhoneListingoperator
lt (PhoneListing listing) return (name lt
listing.name) // Greater-than operator based
upon whether name value of this // // is greater
than name value of parameterized PhoneListing.
// bool PhoneListingoperator gt (PhoneListing
listing) return (name gt 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 PhoneListingoperator
gt (PhoneListing listing) return (name gt
listing.name) // Inequality operator based
upon inequality between PhoneListings' name
values. // bool PhoneListingoperator !
(PhoneListing listing) return (name !
listing.name)
7
phoneListing.cpp (Continued)
// Accessor function for the value of the name
data member. // string PhoneListinggetName()
const return name // Accessor function
for the value of the phoneNumber data member.
// string PhoneListinggetPhoneNumber() const
return phoneNumber // Input operator for
the PhoneListing class retrieves // // name
value, followed by phoneNumber value.
// istream operator gtgt (istream
inputFileStream, PhoneListing listing)
inputFileStream gtgt listing.name gtgt
listing.phoneNumber return inputFileStream
// Output operator for the PhoneListing class
outputs // // name value, followed by phoneNumber
value. // ostream operator ltlt (ostream
outputFileStream, const PhoneListing
listing) outputFileStream ltlt listing.name
ltlt " " ltlt listing.phoneNumber return
outputFileStream
8
Now Lets Set Up Our Own List Class!
///////////////////////////////////////////// //
list.h // // The
class definition for the List class
// /////////////////////////////////////////////
ifndef LIST_H include ltiostreamgt include
ltstringgt 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 entryMAX_LIST_SIZE
int length // Member function
int Index(int position) const define
LIST_H endif
9
list.cpp
//////////////////////////////////////////////////
// list.cpp
// // The class implementation for the List
class. // ////////////////////////////////////////
////////// include ltiostreamgt include
ltstringgt include ltcassertgt include
"list.h" include "phoneListing.h" using
namespace std // This default constructor sets
up the List as one with no elements.
// ListList() length 0 // This copy
constructor sets up the this List // // as a
duplicate of the parameterized List.
// ListList(const List lst) length
lst.getLength() for (int i 1 i lt length
i) entryIndex(i) lst.entryIndex(i)
// This accessor function retrieves the value
of the length data member. // int
ListgetLength() const return length
10
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 Listoperator (int
position) assert((position gt 0) (position
lt length)) return entryIndex(position)
// The assignment operator gives the this List
duplicate // // values for each data member in
the parameterized List. // List Listoperator
(const List lst) length lst.getLength()
for (int i 1 i lt length i)
entryIndex(i) lst.entryIndex(i) return
this
11
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 Listinsert(int
position, elementType elt) if ((position lt
1) (position gt length 1) (length gt
MAX_LIST_SIZE)) return false else
length for (int i length i gt
position i--) entryIndex(i)
entryIndex(i-1) entryIndex(position)
elt return true // This member
function removes the element at the parameterized
// // position from the this List (if the
position is valid ). // bool Listremove(int
position) if ((position lt 1) (position gt
length)) return false else
for (int i position1 i lt length i)
entryIndex(i-1) entryIndex(i)
length-- return true
12
list.cpp (Continued)
// This member function retrieves the element at
the parameterized // // position from the this
List (if the position is kosher). // bool
Listretrieve(int position, elementType elt)
if ((position lt 1) (position gt length))
return false else elt
entryIndex(position) return true
// This member function converts the
parameterized position into the // //
corresponding index of the array that is the
entry data member. // int ListIndex(int
position) const return position-1
13
Now Lets 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
ltiostreamgt include ltfstreamgt include
ltstringgt 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)
14
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(phoneBo
ok, soughtName, soughtPhoneListing))
outputListing(soughtPhoneListing) else
outputNoListing(soughtName) return
15
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 directoryFileName50
List directory PhoneListing nextListing
int position 1 cout ltlt "Enter the file
name of the telephone directory data " cin
gtgt directoryFileName directoryFile.open(direct
oryFileName) directoryFile gtgt nextListing
while (!directoryFile.eof())
directory.insert(position, nextListing)
position directoryFile gtgt nextListing
directoryFile.close() return
directory
16
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 ltlt "Enter the name of the party whose number
you want " cin gtgt 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 lt directory.getLength() i)
directory.retrieve(i, listing)
if (dummyListing listing) return
true return false
17
phoneDriver.cpp (Continued)
//////////////////////////////////////////////////
/////////////////////////// // This function
outputs the PhoneListing found after a successful
search. // ///////////////////////////////////////
////////////////////////////////////// void
outputListing(PhoneListing listing) cout ltlt
"Your requested listing - " ltlt listing ltlt endl ltlt
endl return //////////////////////////////
//////////////////////////////////////////////////
// // This function outputs a message indicating
that the search was unsuccessful.
// ///////////////////////////////////////////////
/////////////////////////////////// void
outputNoListing(string personsName) cout ltlt
"No listing with name " ltlt personsName ltlt endl
ltlt endl return
18
phoneDriver.cpp (Continued)
ACCT 2633 AS 3180 ANTH 2744 ART 3071 BIOL 3927 BSE
D 2504 CHEM 2042 CE 2533 CMIS 2504 CS 2386 CNST 20
88 CI 3082 DANC 2773 ESCI 3620 ECON 2542 EDUC 3277
EDAD 3277 ECE 2524 ENG 2060 ENVS 3311 FIN 2638 FL
3510 GEOG 2090 GRN 3454 HED 3252
HIST 2414 IE 3389 IT 3277 MGMT 2750 MS 2638 MKTG 3
221 MC 2230 MATH 2382 ME 3389 MUS 3900 NURS 3956 P
HIL 2250 PHYS 2472 POLS 3572 PROD 2638 PSYC 2202 P
APA 3762 SOCW 5758 SOC 3713 SPE 5423 SPC 3090 SPPA
3662 THEA 2773 WMST 5060
Sample Output
Contents of phoneData.txt
19
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.
  • Wed 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!

20
Defining Our SortedList Class
////////////////////////////// // sortedList.h
// // The class definition for // // the
SortedList class // //////////////////////////
//// ifndef SORTED_LIST_H include
ltiostreamgt include ltstringgt include
"phoneListing.h" using namespace std const int
MAX_LIST_SIZE 100 typedef PhoneListing
elementType
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 entryMAX_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
21
sortedList.cpp
//////////////////////////////////////////////////
////// // sortedList.cpp
// // The class implementation for the
SortedList class. // /////////////////////////////
/////////////////////////// include
ltiostreamgt include ltstringgt include
ltassert.hgt include "sortedList.h" include
"phoneListing.h" using namespace std // This
default constructor sets up the SortedList as one
with no elements. // SortedListSortedList()
length 0 // This copy constructor sets up
the this SortedList // // as a duplicate of the
parameterized SortedList. // SortedListSorted
List(const SortedList lst) length
lst.getLength() for (int i 1 i lt length
i) entryIndex(i) lst.entryIndex(i)
// This accessor function retrieves the value
of the length data member. // int
SortedListgetLength() const return
length
22
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 SortedListoperator (int
position) assert((position gt 0) (position
lt length)) return entryIndex(position)
// The assignment operator gives the this
SortedList duplicate // // values for each data
member in the parameterized List. // SortedList
SortedListoperator (const SortedList
srtLst) length srtLst.getLength() for
(int i 1 i lt length i)
entryIndex(i) srtLst.entryIndex(i)
return this
23
sortedList.cpp (Continued)
// This member function inserts the parameterized
element // // into the this SortedList at the
appropriate position // // (if there's room).
// bool
SortedListinsert(elementType elt) int
position if (length gt MAX_LIST_SIZE)
return false else retrieve(elt,
position) length for (int ilength i
gt position i--) entryIndex(i)
entryIndex(i-1) entryIndex(position)
elt return true
// This member function removes the // //
parameterized element from the this // //
SortedList (if its in the list). // bool
SortedListremove(elementType elt) int
position if (!retrieve(elt, position))
return false else for (int
iposition1 i lt length i)
entryIndex(i-1) entryIndex(i)
length-- return true
24
sortedList.cpp (Continued)
// This member function retrieves the
parameterized element // // from the this
SortedList (if its in the list), and sets // //
the position parameter to its location in the
list. // bool SortedListretrieve(elementTy
pe 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
SortedListIndex(int position) const return
position-1
25
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
SortedListbinarySearch(int firstPosition, int
lastPosition,
elementType soughtElt, int position) int
middlePosition (firstPosition lastPosition) /
2 if (lastPosition lt firstPosition)
position firstPosition return false
else if (entryIndex(middlePosition)
soughtElt) position middlePosition
return true else if
(entryIndex(middlePosition) lt soughtElt)
return binarySearch(middlePosition1,
lastPosition, soughtElt, position) else
return binarySearch(firstPosition,
middlePosition-1, soughtElt, position)
26
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. // //////////////////////////
//////////////////////////////////////////// inc
lude ltiostreamgt include ltfstreamgt include
ltstringgt include "sortedList.h" include
"phoneListing.h" using namespace
std SortedList queryUserForDirectory() void
outputDirectory(SortedList directory) string
queryUserForName() bool searchDirectory(SortedLis
t directory, string personsName, PhoneListing
listing) void outputListing(PhoneListing
listing) void outputNoListing(string
personsName)
27
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(phoneB
ook) soughtName queryUserForName() if
(searchDirectory(phoneBook, soughtName,
soughtPhoneListing)) outputListing(soughtPho
neListing) else outputNoListing(soughtNa
me) return
28
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. // //////////////////////////
///////////////////////////////////////////// Sort
edList queryUserForDirectory() ifstream
directoryFile char directoryFileName50
SortedList directory PhoneListing
nextListing cout ltlt "Enter the file name of
the telephone directory data " cin gtgt
directoryFileName directoryFile.open(directory
FileName) directoryFile gtgt nextListing
while (!directoryFile.eof())
directory.insert(nextListing)
directoryFile gtgt nextListing
directoryFile.close() return directory
29
phoneDriver2.cpp (Continued)
//////////////////////////////////////////////////
////////////// // This function outputs the
entire parameterized SortedList.
// ///////////////////////////////////////////////
///////////////// void outputDirectory(SortedList
directory) for (int i 1 i lt
directory.getLength() i) cout ltlt
directoryi ltlt endl cout ltlt endl
return ///////////////////////////////////////
///////////// // This function asks the user for
a string value // // that will be used to search
the SortedList. // ////////////////////////////
//////////////////////// string
queryUserForName() string name cout ltlt
"Enter the name of the party whose number you
want " cin gtgt name return name
30
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 directoryposition
return true else return
false ////////////////////////////////////////
///////////////////////////////////// // This
function outputs the PhoneListing found after a
successful search. // ////////////////////////////
/////////////////////////////////////////////////
void outputListing(PhoneListing listing)
cout ltlt "Your requested listing - " ltlt listing ltlt
endl ltlt endl return
31
phoneDriver2.cpp (Continued)
//////////////////////////////////////////////////
/////////////////////////// // This function
outputs the PhoneListing found after a successful
search. // ///////////////////////////////////////
////////////////////////////////////// void
outputListing(PhoneListing listing) cout ltlt
"Your requested listing - " ltlt listing ltlt endl ltlt
endl return //////////////////////////////
//////////////////////////////////////////////////
// // This function outputs a message indicating
that the search was unsuccessful.
// ///////////////////////////////////////////////
/////////////////////////////////// void
outputNoListing(string personsName) cout ltlt
"No listing with name " ltlt personsName ltlt endl
ltlt endl return
32
phoneDriver2.cpp (Continued)
Write a Comment
User Comments (0)
About PowerShow.com