Download presentation
Presentation is loading. Please wait.
Published byDonald Marshall Johnston Modified over 8 years ago
1
class List { public: // TYPEDEF and MEMBER CONSTANTS enum { CAPACITY = 30 }; // Or: static const size_t CAPACITY = 30; typedef double Item; // CONSTRUCTOR List( ); // MODIFICATION MEMBER FUNCTIONS void start( ); void advance( ); void insert(const Item& entry); void attach(const Item& entry); void remove_current( ); // CONSTANT MEMBER FUNCTIONS size_t size( ) const; bool is_item( ) const; Item current( ) const; private: Item data[CAPACITY]; size_t used; size_t current_index; };
2
// CONSTRUCTOR for the List class: // List( ) // Postcondition: The List has been initialized // as an empty List. List::List() { used = 0; }
3
// void start( ) // Postcondition: The first item on the List // becomes the current item // (but if the List is empty, then there is no // current item). List::start() { if (used > 0) current_index = 0; }
4
// void advance( ) // Precondition: is_item returns true. // Postcondition: If the current item was already the last // item on the List, then there is no longer any current // item. Otherwise, the new current item is the item // immediately after the original current item. List::advance() { }
5
// void insert(const Item& entry) // Precondition: size( ) < CAPACITY. // Postcondition: A new copy of entry has been inserted in // the List before the current item. If there was no current // item, then the new entry has been inserted at the front of // the List. In either case, the newly inserted item is now // the current item of the List. void List::insert(const Item& entry) { }
6
// void attach(const Item& entry) // Precondition: size( ) < CAPACITY. // Postcondition: A new copy of entry has been inserted // in the List after the current item. If there was no current // item, then the new entry has been attached to the end of // the List. In either case, the newly inserted item is now // the current of the List. void List::attach(const Item& entry) { }
7
// void remove_current( ) // Precondition: is_item returns true. // Postcondition: The current item has been removed // from the List, and the item after this (if there is one) // is now the new current item. void List::remove_current() { }
8
// size_t size( ) const // Postcondition: The return value is the number of // items on the List. size_t List::size() const { return used; }
9
// bool is_item( ) const // Postcondition: A true return value indicates that there // is a valid "current" item that may be retrieved by // activating the current member function (listed next). // A false return value indicates that there is no valid // current item. bool List::is_item() const { }
10
// Item current( ) const // Precondition: is_item( ) returns true. // Postcondition: The item returned is the current // item on the List. Item List::current() const { return data[current_index]; }
11
Array data 0 1 2 3 4 5 6 7 8 9 ? 0 used current_index After constructor called
12
Array data 0 1 2 3 4 5 6 7 8 9 used current_index After start called (list empty) 0 ?
13
Array data 0 1 2 3 4 5 6 7 8 9 used current_index After start called (list not empty) 4 0 EBAD
14
Array data 0 1 2 3 4 5 6 7 8 9 used current_index Before and After advance is called (current item was ‘B’ now ‘E’) 4 2 EBAD used current_index 4 3 0 1 2 3 4 5 6 7 8 9 EAD Array data B
15
0 1 2 3 4 5 6 7 8 9 used current_index Before and After advance is called (current item ‘E’, now ?) 4 3 EBAD used current_index 4 ? 0 1 2 3 4 5 6 7 8 9 EAD Array data B
16
0 1 2 3 4 5 6 7 8 9 used current_index Before and After insert is called (List empty, insert ‘X’) 0 ? used current_index 1 0 0 1 2 3 4 5 6 7 8 9 X Array data
17
0 1 2 3 4 5 6 7 8 9 used current_index Before and After insert is called (current item ‘B’, insert ‘X’) 4 2 EBAD used current_index 5 2 0 1 2 3 4 5 6 7 8 9 EBAD Array data X
18
0 1 2 3 4 5 6 7 8 9 used current_index Before and After attach is called (No current item - List empty, attach ‘X’) 0 ? used current_index 1 0 0 1 2 3 4 5 6 7 8 9 X Array data
19
0 1 2 3 4 5 6 7 8 9 used current_index Before and After attach is called (current item ‘B’, attach ‘X’) 4 2 EBAD used current_index 5 3 0 1 2 3 4 5 6 7 8 9 EXAD Array data B
20
0 1 2 3 4 5 6 7 8 9 used current_index Before and After attach is called ( No current item- List not empty, attach ‘X’ ) 4 ? EBAD used current_index 5 4 0 1 2 3 4 5 6 7 8 9 XEAD Array data B
21
0 1 2 3 4 5 6 7 8 9 used current_index Before and After remove_current is called ( current item is ‘A’ ) 4 1 EBAD used current_index 3 1 0 1 2 3 4 5 6 7 8 9 ED Array data B
22
0 1 2 3 4 5 6 7 8 9 used current_index Before and After remove_current is called ( current item is ‘E’ ) 4 3 EBAD used current_index 3 ? 0 1 2 3 4 5 6 7 8 9 BD Array data A
23
0 1 2 3 4 5 6 7 8 9 used current_index Before and After remove_current is called ( one item in List, current item is ‘D’ ) 1 0 D used current_index 0 ? 0 1 2 3 4 5 6 7 8 9 Array data
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.