Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ A limited number of tickets for the Hoops championship basketball game go on sale tomorrow and tickets orders are to be filled in the

C++

A limited number of tickets for the Hoops championship basketball game go on sale tomorrow and tickets orders are to be filled in the order in which they received. Write a program that a box-office cashier can use to enter the names and addresses of the persons ordering tickets together with the number of tickets requested and store this information in a list. The program should then produce a sequence of mailing labels (name, address, and number of tickets) for orders that can be filled.

Giving the List class (list.h & list.cpp), design the TicketOrder.h and TicketOrder.cpp. The test driver bbtickets.cpp as follow:

//--- bbticket.cpp ---

/*--------------------------------------------------------------------

Hoops basketball-ticket program.

Input (keyboard): number of tickets available,

Input (file): names, addresses of people ordering tickets,

and number of tickets requested

Output: mailing labels for ticket orders .

Condition: none.

Uses: List class with typedef change to

typedef TicketOrder ElementType;

--------------------------------------------------------------------*/

#include

#include

#include

using namespace std;

#include "List.h" // with ElementType = TicketOrder

int main()

{

// to do...

}

The List.h and list.cpp:

//----- List.h -----

#ifndef LINKEDLIST

#define LINKEDLIST

#include

#include "TicketOrder.h"

typedef TicketOrder ElementType;

class List

{

private:

class Node

{

public:

//------ Node DATA MEMBERS

ElementType data;

Node * next;

//------ Node OPERATIONS

//-- Default constrctor: initializes next member to

Node()

: next(0)

{ }

//-- Explicit-value constructor: initializes data member to dataValue

//-- and next member to 0

Node(ElementType dataValue)

: data(dataValue), next(0)

{ }

}; //--- end of Node class

typedef Node * NodePointer;

public:

//------ List OPERATIONS

List();

/*--------------------------------------------------------------------

Default constructor: builds an empty List object.

Precondition: None

Postcondition: first is 0 and mySize is 0.

--------------------------------------------------------------------*/

List(const List & origList);

/*--------------------------------------------------------------------

Copy constructor

Precondition: A copy of this list is needed.

Postcondition: A distincr copy has been constructed.

--------------------------------------------------------------------*/

~List();

/*--------------------------------------------------------------------

Destructor

Precondition: This list's lifetime is over.

Postcondition: This list has been destroyed.

--------------------------------------------------------------------*/

const List & operator=(const List & rightSide);

/*--------------------------------------------------------------------

Assignment operator

Precondition: This list must be assigned a value.

Postcondition: A copy of rightSide has been assigned to this list.

--------------------------------------------------------------------*/

bool List::empty();

/*--------------------------------------------------------------------

Check if this list is empty

Precondition: None.

Postcondition: true is returned if this list is empty, false if not.

--------------------------------------------------------------------*/

void insert(ElementType dataVal, int index);

/*--------------------------------------------------------------------

Insert a value into a list at a given index.

Precondition: index is a valid list index: 0 <= index <= mySize,

Postcondition: dataVal has been inserted into the list at position

index, provided index is valid..

--------------------------------------------------------------------*/

void erase(int index);

/*--------------------------------------------------------------------

Remove a value from a list at a given index.

Precondition: index is a valid list index: 0 <= index < mySize

Postcondition: dataVal at list position index has been removed,

provided index is valid.

--------------------------------------------------------------------*/

int search(ElementType dataVal);

/*--------------------------------------------------------------------

Search for an data value in this list.

Precondition: None

Postcondition: Index of node containing dataVal is returned

if such a node is found, -1r if not.

--------------------------------------------------------------------*/

void display(ostream & out) const;

/*--------------------------------------------------------------------

Display the contensts of this list.

Precondition: ostream out is open

Postcondition: Elements of this list have been output to out.

--------------------------------------------------------------------*/

int nodeCount();

/*--------------------------------------------------------------------

Count the elements of this list.

Precondition: None

Postcondition: Number of elements in this list is returned.

--------------------------------------------------------------------*/

private:

//------ DATA MEMBERS

NodePointer first;

int mySize;

}; //--- end of List class

ostream & operator<<(ostream & out, const List & aList);

istream & operator>>(istream & in, List & aList);

#endif

//----- List.cpp -----

#include

using namespace std;

#include "List.h"

//-- Definition of the class constructor

List::List()

: first(0), mySize(0)

{ }

//-- Definition of the copy constructor

List::List(const List & origList)

{

mySize = origList.mySize;

first = 0;

if (mySize == 0) return;

List::NodePointer origPtr, lastPtr;

first = new Node(origList.first->data); // copy first node

lastPtr = first;

origPtr = origList.first->next;

while (origPtr != 0)

{

lastPtr->next = new Node(origPtr->data);

origPtr = origPtr->next;

lastPtr = lastPtr->next;

}

}

//-- Definition of the destructor

inline List::~List()

{

List::NodePointer prev = first,

ptr;

while (prev != 0)

{

ptr = prev->next;

delete prev;

prev = ptr;

}

}

// Definition of empty()

bool List::empty()

{

return mySize == 0;

}

//-- Definition of the assignment operator

const List & List::operator=(const List & rightSide)

{

mySize = rightSide.mySize;

first = 0;

if (mySize == 0) return *this;

if (this != &rightSide)

{

this->~List();

List::NodePointer origPtr, lastPtr;

first = new Node(rightSide.first->data); // copy first node

lastPtr = first;

origPtr = rightSide.first->next;

while (origPtr != 0)

{

lastPtr->next = new Node(origPtr->data);

origPtr = origPtr->next;

lastPtr = lastPtr->next;

}

}

return *this;

}

//-- Definition of insert()

void List::insert(ElementType dataVal, int index)

{

if (index < 0 || index > mySize)

{

cerr << "Illegal location to insert -- " << index << endl;

return;

}

mySize++;

List::NodePointer newPtr = new Node(dataVal),

predPtr = first;

if (index == 0)

{

newPtr->next = first;

first = newPtr;

}

else

{

for(int i = 1; i < index; i++)

predPtr = predPtr->next;

newPtr->next = predPtr->next;

predPtr->next = newPtr;

}

}

//-- Definition of erase()

void List::erase(int index)

{

if (index < 0 || index >= mySize)

{

cerr << "Illegal location to delete -- " << index << endl;

return;

}

mySize--;

List::NodePointer ptr,

predPtr = first;

if (index == 0)

{

ptr = first;

first = ptr->next;

delete ptr;

}

else

{

for(int i = 1; i < index; i++)

predPtr = predPtr->next;

ptr = predPtr->next;

predPtr->next = ptr->next;

delete ptr;

}

}

//-- Definition of search()

int List::search(ElementType dataVal)

{

int loc;

List::NodePointer tempP = first;

for (loc = 0; loc < mySize; loc++)

if (tempP->data == dataVal)

return loc;

else

tempP = tempP->next;

return -1;

}

//-- Definition of display()

void List::display(ostream & out) const

{

List::NodePointer ptr = first;

while (ptr != 0)

{

out << ptr->data << " ";

ptr = ptr->next;

}

}

//-- Definition of the output operator

ostream & operator<<(ostream & out, const List & aList)

{

aList.display(out);

return out;

}

//-- Definition of nodeCount()

int List::nodeCount()

{

int count = 0;

List::NodePointer ptr = first;

while (ptr != 0)

{

count++;

ptr = ptr->next;

}

return count;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

What are the three major steps in the writing process? [LO-1]

Answered: 1 week ago