Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this. Read the files carefully. Points one and two have been marked in the definition of OLList::insert and point three in function

Please help with this. Read the files carefully. Points one
and two have been marked in the definition of OLList::insert and point three in function OLList::remove.
Draw memory diagram when program reached these points for the first time.
Submit your diagrams. Please provide a digital copy of your diagram using a scanner and post it as part of your lab
report on the D2L. Also make sure the scanned copy of your diagram is clear and readable.
Ex E:
// lab4ExE.cpp
//
// ENSF 337 Spring 2024 Lab 4 Exercise E
//
#include
using namespace std;
#include "OLList.h"
int main()
{
OLList the_list;
cout << "List just after creation: ";
the_list.print();
the_list.insert(330);
the_list.insert(440);
the_list.insert(220);
the_list.insert(110);
cout << "List after some insertions: ";
the_list.print();
the_list.remove(550);
the_list.remove(330);
cout << "List after some removals: ";
the_list.print();
return 0;
}
OLLIST.h:
// OLList.h
// ENSF 337 Spring 2024 Lab 4 Exercises E and F
#ifndef OLLIST_H
#define OLLIST_H
typedef int ListItem;
struct Node {
ListItem item;
Node *next;
};
class OLList {
public:
OLList(); // PROMISES: Creates empty list.
OLList(const OLList& source);
OLList& operator =(const OLList& rhs);
~OLList();
void insert(const ListItem& itemA);
// PROMISES:
// A node with a copy of itemA is added in
// a way that preserves the nondecreasing
// order of items in nodes.
void remove(const ListItem& itemA);
// PROMISES:
// If no node has an item matching itemA,
// list is unchanged.
// Otherwise exactly one node with
// its item == itemA is removed.
void print() const;
// PROMISES:
// Prints items in list on a single line, with commas separating
// the items and square brackets marking the ends of the list.
// NOTE:
// This is different from the print function presented in lectures.
private:
Node *headM;
void destroy();
// Deallocate all nodes, set headM to zero.
void copy(const OLList& source);
// List becomes copy of source.
};
#endif
OLLIST.cpp:
// OLList.cpp
// ENSF 337 Spring 2024 Lab 4 Exercise E and F
#include
#include
using namespace std;
#include "OLList.h"
OLList::OLList()
: headM(nullptr)
{
}
OLList::OLList(const OLList& source)
{
copy(source);
}
OLList& OLList::operator =(const OLList& rhs)
{
if (this != &rhs){
destroy();
copy(rhs);
}
return *this;
}
OLList::~OLList()
{
destroy();
}
void OLList::print() const
{
cout <<'[';
if (headM != nullptr){
cout <<''<< headM->item;
for (const Node *p = headM->next; p != nullptr; p = p->next)
cout <<","<< p->item;
}
cout <<"]
";
}
void OLList::insert(const ListItem& itemA)
{
Node *new_node = new Node;
new_node->item = itemA;
if (headM == nullptr || itemA <= headM->item ){
new_node->next = headM;
headM = new_node;
// point one
}
else {
Node *before = headM; // will point to node in front of new node
Node *after = headM->next; // will be nullptr/0 or point to node after new node
while(after != nullptr && itemA > after->item){
before = after;
after = after->next;
}
new_node->next = after;
before->next = new_node;
// point two
}
}
void OLList::remove(const ListItem& itemA)
{
// if list is empty, do nothing
if (headM == nullptr || itemA < headM->item)
return;
Node *doomed_node = nullptr;
if (itemA == headM->item){
doomed_node = headM;
headM = headM->next;
}
else {
Node *before = headM;
Node *maybe_doomed = headM->next;
while(maybe_doomed != nullptr && itemA > maybe_doomed->item){
before = maybe_doomed;
maybe_doomed = maybe_doomed->next;
}
// point three
}
// the remaining part of this function is missing. As part of exercise A
// students are supposed to complete the rest of the definition of this function.
}
void OLList::destroy()
{
// this function is not properly designed. As part of the exercise A
// students are supposed to remove the folloiwng lines and
// complete the definition of this helper function.
cout << "OLList::destroy was called but isn't ready for use"
<<"--program is terminated.
";
headM = nullptr;
}
void OLList::copy(const OLList& source)
{
// this function is not properly designed. As part of the exercise A
// students are supposed to remove the folloiwng lines and
// complete the definition of this helper function.
// The only effect of the next line is to tell the compiler
// not to generate an "unused argument" warning. Don't leave it
// it in y

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

Step: 3

blur-text-image

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

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

Students also viewed these Databases questions

Question

Explain the multicultural organization development (MCOD) process.

Answered: 1 week ago