1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <string>;
#include <iostream>;
using namespace std;
struct event_t {
event_t( std::string const &title, unsigned const tickets, unsigned const tickets_available, event_t *next = nullptr)
: title( title ),
tickets( tickets ),
tickets_available( tickets_available ),
next( next )
{}
bool is_sellout() const { return tickets_available == 0; }
std::string title;
unsigned tickets;
unsigned tickets_available;
event_t *next;
};
std::ostream & operator<<( std::ostream & os, event_t const &event )
{
os << "Event \"" << event.title << "\"\nTickets:\t" << event.tickets << "\nTickets available:\t" << event.tickets_available;
return os;
}
void print_event( event_t const & event )
{
std::cout << event << '\n';
}
void print_event_list( event_t const & first_event )
{
event_t const *current = &first_event;
print_event( *current );
return current->next ? print_event_list( *current->next ) : 0;
}
void print_event_list_sellout( event_t const & first_event )
{
event_t const *current = &first_event;
if( current->is_sellout() ) {
print_event( *current );
}
return current->next ? print_event_list_sellout( *current->next ) : 0;
}
int main()
{
event_t event_four( "foobarbaz", 2000, 2000 );
event_t event_three( "foobar", 1000, 0, &event_four );
event_t event_two( "bar", 5000, 4500, &event_three );
event_t event_one( "foo", 10000, 0, &event_two );
std::cout << "event_two:\n";
print_event( event_two );
std::cout << "\nEvent list:\n";
print_event_list( event_one );
std::cout << "\nEvent list sellout:\n";
print_event_list_sellout( event_one );
}
| |