Answer to Question #329637 in C++ for Umer

Question #329637

Let’s implement the concept of doubly linked list with a real time example.

For a bogie we need following information:

struct bogie{

string Name;

char type; //B for Business Class, F for First Class, and E for Economy class

int capacity; //Between 50-70

int fare; //For Business Class 1200, For First Class 1000, For Economy class 500

bogie *next;

bogie *prev;

};

bogie *engine //use as head for the train

Define a class, name it Train and write following functions for that class:

i. Insert a bogie at front();

ii. Insert a bogie at given position() //position will be defined based on name of bogie

iii. Insert a bogie at the end()

iv. return seating capacity of each type()

v. return total number of seats()

vi. return maximum fare collection based on total capacity of train()


1
Expert's answer
2022-04-17T11:03:39-0400
#include<iostream>
#include<string>


using namespace std;


struct bogie
{
	string Name;
	char type; //B for Business Class, F for First Class, and E for Economy class
	int capacity; //Between 50-70
	int fare; //For Business Class 1200, For First Class 1000, For Economy class 500
	bogie *next;
	bogie *prev;
	bogie(){}
};


class Train
{
	bogie *engine;
public:
	Train():engine(NULL){}
	void front(string _Name, char _type, int _capacity, int _fare)
	{
		struct bogie *p = new bogie;
		p->Name = _Name;
		p->type = _type;
		p->capacity = _capacity;
		p->fare = _fare;
		p->prev = p->next = NULL;
		if (engine != NULL)
		{
			p->next = engine;
			engine->prev = p;
			engine = p;
		}
		else
		{
			p->next = engine;
			engine = p;
		}
	}
	void InsertAtPos(string _Name, char _type, int _capacity, int _fare)
	{
		struct bogie *p = new bogie;
		p->Name = _Name;
		p->type = _type;
		p->capacity = _capacity;
		p->fare = _fare;
		p->prev = p->next = NULL;
		struct bogie *x = engine;
		while (x->Name.compare(p->Name) > 0)
		{
			x = x->next;
		}
		p->next = x->next;
		p->prev = x;
		x->next = p;
	}
	void InsertAtEnd(string _Name, char _type, int _capacity, int _fare)
	{
		struct bogie *p = new bogie;
		p->Name = _Name;
		p->type = _type;
		p->capacity = _capacity;
		p->fare = _fare;


		struct bogie *x = engine;
		while (x->next!= 0)
		{
			x = x->next;
		}
		p->next = x->next;
		p->prev = x;
		x->next = p;
	}
	void ReturnCapByType()
	{
		struct bogie *x = engine;
		int CapB = 0, CapF = 0, CapE = 0;
		while (x != NULL)
		{
			if (x->type == 'B')
				CapB += x->capacity;
			else if (x->type == 'F')
				CapF += x->capacity;
			else if (x->type == 'E')
				CapE += x->capacity;
			x = x->next;
		}
		cout << "\nCapacity for Business Class is " << CapB
			<< "\nCapacity for First Class is " << CapF
			<<"\nCapacity for Economy Class is " << CapE;
	}
	void TotalSeats()
	{
		struct bogie *x = engine;
		int totalSeats = 0;
		while (x != NULL)
		{
			totalSeats += x->capacity;
			x = x->next;
		}
		cout << "\nTotal capacity of seats are " << totalSeats;
	}
	void MaximumFareCollect()
	{
		struct bogie *x = engine;
		double totalFare = 0;
		while (x != NULL)
		{
			totalFare += x->capacity*x->fare;
			x = x->next;
		}
		cout << "\nMaximum fare collection based on total capacity is " << totalFare;
	}
	void Display()
	{
		struct bogie *x = engine;
		while (x != NULL)
		{
			cout << x->Name << " " << x->type << " " << x->capacity << " " << x->fare<<endl;
			x = x->next;
		}
	}
};


int main()
{
	Train t;
	t.front("4", 'E', 70, 500);
	t.front("1", 'B', 50, 1200);
	t.InsertAtPos("3", 'F', 60, 1000);
	t.InsertAtEnd("2", 'E', 70, 500);
	t.Display();
	t.ReturnCapByType();
	t.TotalSeats();
	t.MaximumFareCollect();
}







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS