Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need Help ERGENTLY

willswills Posts: 1Member
Use the solution for as the basis for this PROGRAM. Use the template-based linked list implementation provided for this PROGRAM
Then, with the array replaced by a linked list, there is no need to limit the number of entries the user wishes to make and we can remove this limit.
To achieve this you need to make some changes including:
Define an object of class List at the beginning of your main function,if Entry is the name of the class or struct you use to hold the data for a single refueling.
Change all the accesses to the array into the appropriate calls to the list modifier or observer methods.
If the user enters new data, your program will call the append method with this new data.
The user indicates that he/she does not want to enter any more data by entering 0 for both miles and gallons.
Explain which problems you face when you try to change the QuickSort function to use linked lists.
*************************************************************************
USE THIS SOLUTION AND CHANGE IT TO LINKED LIST IMPLMENTATION USING node_h and LIST_H CODING BELOW.


#include
#include
#include
#define LITERSPERGALLON 3.7854117
#define KILOMETERSPERMILE 1.609347
#define KPHUNDREDkm 100

class DATA
{
private:
int m_a;
int m_l;
int m_miles;
int m_gallons;
int m_mpg;
int m_kpl;

int MilesToKilometers(int miles)
{
return (int)(miles*KILOMETERSPERMILE);
}
int GallonsToLitres(int gallons)
{
return (int)(gallons*LITERSPERGALLON);
}
int MilesAndGallonsTolitersper100km(int miles, int gallons)
{
return (int)(((gallons*LITERSPERGALLON)/(miles*KILOMETERSPERMILE))*KPHUNDREDkm);
}

public:
DATA() //constructer
{
m_miles = 0; //innitialisation of variables
m_gallons = 0;
m_a = 0;
m_l = 0;
m_kpl = 0;
}
~DATA(){}; //destractor

void SetInformation (int miles, int gallons)//sets information
{
m_miles = miles;
m_gallons = gallons;
}
void CalculateAll()//calculation
{
m_a = MilesToKilometers(m_miles);
m_l = GallonsToLitres(m_gallons);
m_kpl = MilesAndGallonsTolitersper100km(m_miles, m_gallons);
}
void PrintAll() //displays to the screen
{
cout << "Distance(km) "<< m_a <<", Liters(l) "<<m_l<<", l/100km "<<m_kpl<<endl;
}
};

void DoCalculations(DATA *values,int count);
void PrintResults(DATA *values,int count);

int main(void)
{
char response;

// Initialize the first 5 elements of our arrays
DATA values[10];
values[0].SetInformation(300, 15);
values[1].SetInformation(280 ,14);
values[2].SetInformation(200 ,11);
values[3].SetInformation(100 ,6);
values[4].SetInformation(200 ,12);
int count, miles, gallons;

// User entries start at 5
count=5;
cout<<"*************************************************************"<<endl;
cout<< "This program takes number of miles & gallons and
";
cout<< "converts it to Milespergallon & Kilometersperliter"<<endl;
cout<<"********************************************************************"<<endl;

// We can have maximum of 10 entries
while(count<10)
{
cout << "
Do u want to do a calculation? <Y/any character to end>: ";
cin >>response;
cout<<endl;

if(!(toupper(response) =='Y')) //if the user doesn't want to use program.
{
break;
}
cout<<endl;

cout << "Enter the miles: ";
cin >> miles;
cout << "Enter the gallons: ";
cin >> gallons;
values[count].SetInformation(miles ,gallons);
// Increment one per user entry
count++;
}

DoCalculations(values,count); //calling functions outside main
PrintResults(values,count); //calling functions outside main
cout<<endl;
system("PAUSE");
return 0;
}


// All arrays are passed by reference, so we can change them!
void DoCalculations(DATA values [],int count)
{
int i;
for (i=0; i<count;i++)
{
values[i].CalculateAll(); //calls calculateall function
}
}

void PrintResults(DATA values[],int count)
{
int i;
for(i=0;i<count;i++)
{
values[i].PrintAll(); //calls printall function
}
}


*************************************************************************
#ifndef NODE_H
#define NODE_H
#include <stddef.h>

template

class List;


template

class Node {
friend class List;


private:
Type nData;

Node *pNext;

Node *pPrev;


public:
// constructor and descructor

Node (Type data)

nData = data;

pNext = NULL;

pPrev = NULL;
}

~Node() {};

// observer

Type getData() {return nData;}

Node *getNext() {return pNext;}

Node *getPrev() {return pPrev;}

// modifier

void setData(Type data) {
nData = data;

}

};

#endif
**************************************************************************

#ifndef LIST_H
#define LIST_H

#include "Node.h"
#include

template
class List{
private:
Node *pHead;
Node *pLast;

public:
List() {pHead = pLast = NULL;};
List(Node *a, Node *b) {pHead = a; pLast = b;};
~List();

// modifier
Node *append(Type value);
Node *insertAfter(Type value, Node *pAfter);
Node *insertBefore(Type value, Node *pBefore);
void remove(Node *pNode);

// observer
bool isEmpty() {return pHead == NULL;};
Node *head() {return pHead;};
Node *last() {return pLast;};
};

template
List::~List() {
while (!isEmpty ())
remove(pHead);
}

template
Node *List::append(Type value) {
Node *p = new Node(value);

if (!p)
return NULL;

if (pHead) {
p->pNext = pHead;
pHead->pPrev = p;
} else
pLast = p;

pHead = p;
return p;
}

template
Node *List::insertAfter(Type value, Node *pAfter) {
Node *p = new Node(value);

if (!p)
return NULL;

if (!pAfter) // assume empty list
pHead = pLast = p;
else {
p->pPrev = pAfter;
p->pNext = pAfter->pNext;
if (!pAfter->pNext)
pLast = p;
else {
pAfter->pNext->pPrev = p;
pAfter->pNext = p;
}
}
return p;
}

template
Node *List::insertBefore(Type value, Node *pBefore);
Node *p = new Node(value);

if (!p)
return NULL;

if (!pBefore) // assume empty list
pHead = pLast = p;
else {
p->pNext = pBefore;
p->pPrev = pBefore->pPrev;
if (!pBefore->pPrev)
pHead = p;
else {
pBefore->pPrev->pNext = p;
pBefore->pPrev = p;
}
}
return p;
}

template
void List::remove(Node *pWhich) {
if (!pWhich)
return;

if (pHead != pWhich) // there's a previous
pWhich->pPrev->pNext = pWhich->pNext;
else
pHead = pWhich->pNext;

if (pLast != pWhich) // there's a next
pWhich->pNext->pPrev = pWhich->pPrev;
else
pLast = pWhich->pPrev;

delete pWhich;
}

#endif
Sign In or Register to comment.