#include using namespace std; class Record { public: int id; Record* next; Record* prev; }; class List { public: Record* first; Record* last; void Add(Record r); void Print(); List(); }; List::List()...

1 answer below »

I just need help with writing a finishing a while loop.


I highlighted the comment in the word doc:


// write code that will add to middle(finish this!!)




#include using namespace std; class Record { public: int id; Record* next; Record* prev; }; class List { public: Record* first; Record* last; void Add(Record r); void Print(); List(); }; List::List() { first = NULL; last = NULL; } void List::Add(Record r) { // adds records to list in order of id // deep copy Record* myRec = new Record; myRec->id = r.id; // case empty list, adding first record if (first == NULL && last == NULL) { first = myRec; last = myRec; myRec->next = NULL; myRec->prev = NULL; return; } // find the spot to insert Record* current; current = first; while (current) { if (myRec->id < current-="">id) { break; } current = current->next; } // insert in front of current // insert front if (first == current) { myRec->next = first; myRec->prev = NULL; first = myRec; current->prev = myRec; } else if (current == NULL) { // add to back myRec->next = NULL; myRec->prev = last; last = myRec; myRec->prev->next = myRec; } // write code that will add to middle(finish this!!) } void List::Print() { Record* current; current = first; while (current) { cout < current-="">id < endl;="" current="current-">next; } } int main() { List myList; Record rec; rec.id = 10; myList.Add(rec); rec.id = 3; myList.Add(rec); rec.id = 5; myList.Add(rec); rec.id = 4; myList.Add(rec); rec.id = 2; myList.Add(rec); rec.id = 6; myList.Add(rec); rec.id = 15; myList.Add(rec); myList.Print(); }
Answered 1 days AfterApr 15, 2021

Answer To: #include using namespace std; class Record { public: int id; Record* next; Record* prev; }; class...

Arun Shankar answered on Apr 16 2021
155 Votes
#include
using namespace std;
class Record
{
public:
int id;
Record* next;

Record* prev;
};
class List
{
public:
Record* first;
Record* last;
void Add(Record r);
void Print();
List();
};
List::List()
{
first = NULL;
last = NULL;
}
void List::Add(Record r)
{
// adds records to list in order of id
// deep copy
Record* myRec = new Record;
myRec->id = r.id;
// case empty list, adding first record

if (first == NULL && last == NULL)
{
first = myRec;
last = myRec;
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here