Lecture 6 ADT Unsorted List more PowerPoint PPT Presentation

presentation player overlay
1 / 10
About This Presentation
Transcript and Presenter's Notes

Title: Lecture 6 ADT Unsorted List more


1
Lecture 6ADT Unsorted List (more)
Logical level
ID Goods Quantity 23 C book
10 12 beer can 100 45 blank cd
30 32 pair of socks 40
2
Specification for ADT Unsorted List (part 1)
ifndef UNSORTED_TYPE_H define
UNSORTED_TYPE_H include ltstringgt define
MAX_ITEMS 100 struct ItemType int ID // the
key field string productName int
quantity
UnsortedType.h
3
Specification for ADT Unsorted List (part 2)
class UnsortedTypepublic // Constructor
UnsortedType() // Transformers void
MakeEmpty() void InsertItem(ItemType item)
void DeleteItem(ItemType item) // Observers
bool IsFull() const int LengthIs() const
void RetrieveItem(ItemType item, bool found)
// Iterators void ResetList() void
GetNextItem(ItemType item) private //here
come the data members endif
UnsortedType.h
4
A client of the class UnsortedType
// TO DO include appropriate header
files include UnsortedType.h int main()
bool flag ItemType item UnsortedType
StudentShopList // TO DO assign an input
file stream infile to file // stock.dat and
an output file stream outfile // to file
forsale.dat while (infile gtgt item.ID)
if (!(infile gtgt item.productName)) break if
(!(infile gtgt item.quantity)) break
StudentShopList.InsertItem(item) while
(cin gtgt item.ID) StudentShopList.RetrieveIte
m(item, flag) if (flag) outfile ltlt item.ID
ltlt ltlt (75 item.quantity)
/ 100
studentshop.cc
5
Implementation of ADT Unsorted List (or how the
ADT Unsorted List looks inside)
Internal Data Structure
int length ItemType infoMAX_ITEMS int
currentPos
6
Implementation of ADT Unsorted List
Data members
class UnsortedType public private
int length ItemType infoMAX_ITEMS int
currentPos
UnsortedType.h
Implementation of the member functions
include UnsortedType.h // Constructor UnsortedT
ypeUnsortedType() length 0
UnsortedType.cc
7
Implementation of ADT Unsorted List (more)
Implementation of the member function
RetrieveItem
void UnsortedType RetrieveItem(ItemType item,
bool found) bool moreToSearch int location
0 found false moreToSearch (location
lt length) while (moreToSearch !found)
if (infolocation.ID item.ID) found
true item infolocation else
location moreToSearch (location lt
length)
UnsortedType.cc
length 8
8
Implementation of ADT Unsorted List (more)
Implementation of the member function DeleteItem
(bad solution)
UnsortedType.cc
void UnsortedTypeDeleteItem(ItemType item) //
Pre items key has been initialized one and
only // one element in the list has a key
that matches // items // Post No element
in the list has a key that matches //
items int location 0 while
(infolocation.ID ! item.ID) location
for(int i location i lt length-2 i)
infoi infoi1 length--
Deleting Peter
7
length 8
Sarah
9
Implementation of ADT Unsorted List (more)
Implementation of the member functions
Implementation of the member function DeleteItem
(better solution)
UnsortedType.cc
void UnsortedTypeDeleteItem(ItemType
item) // Pre items key has been initialized
one and only // one element in the list
has a key that matches // items // Post
No element in the list has a key that matches //
items int location 0 while
(infolocation.ID ! item.ID) location
infolocation infolength-1 length--
Deleting Peter
7
length 8
Sarah
10
Implementation of ADT Unsorted List (more)
Implementation of the Iterators
void UnsortedTypeResetList() // Post
currentPos points the first item in // the
list currentPos 0 void
UnsortedTypeGetNextItem(ItemType item) // Pre
currentPos lt length // Post currentPost moves to
the next item in // the list item
infocurrentPos currentPos
UnsortedType.cc
Write a Comment
User Comments (0)
About PowerShow.com