ShadowedKitty
Coder
I am in a C++ object oriented programming class and we were assigned a program that stores a singly linked list with "Fruit" as the data. Each fruit has a *char name and a char code[4]. No matter what I try, when I try to insert a new Fruit into a list that already has one or more fruits in it, the list is completely taken over by the new Fruit that is being added. That is, the list has the correct number of elements, but they are all the most recently added element. We are limited to the exact function that are there for the linked list (type and parameters) but I am allowed to change the inside.
[CODE lang="cpp" title="LList.cpp"]#include "LList.h"
#include "LeakWatcher.h"
using namespace std;
typedef Fruit InfoType;
LList::~LList() {
while (list) {
Node* n = new Node(list->infoPtr, list->next);
if (n->next)
{
list = list->next;
delete n;
}
else
delete list;
}
}
LList::LList(const LList& copyFrom)
{
Node* i = copyFrom.list;
while (i)
{
Node* n = new Node(i->infoPtr, i->next);
if(!this->list)
{
this->list = n;
}
else
{
list->next = n;
list = n;
}
i = i->next;
}
}
bool LList::IsEmpty() const {
return (list == NULL);
}
bool LList::Insert(InfoType* x_ptr)
{
if (IsEmpty())
{
Node* n = new Node(x_ptr, NULL);
list = n;
//cout << "list is: " << list->infoPtr << endl;
return true;
}
else if (list->next == NULL)
{
if (x_ptr < list->infoPtr)
{
Node* n = new Node(x_ptr, NULL);
}
}
else
{
Node *current = list;
while (current->next != NULL && current->next->infoPtr < x_ptr)
current = current->next;
if (current->infoPtr != x_ptr)
{
x_ptr->~Fruit();
return false;
}
else
{
Node* aft = current->next;
Node* w = new Node(x_ptr, NULL);
w->next = aft;
current->next = w;
//cout << "list is: " << list->infoPtr << endl;
return true;
}
}
}
bool LList:😀elete(const InfoType& x)
{
if (list == NULL)
return false;
else
{
Node* toDelete = new Node(list->infoPtr, list->next);
Node* prev = new Node(list->infoPtr, list->next);
while (*toDelete->infoPtr != x || toDelete == NULL)
{
prev = toDelete;
toDelete = toDelete->next;
}
if (toDelete == list)
{
list = list->next;
toDelete->~Node();
return true;
}
else if (toDelete)
{
prev->next = toDelete->next;
toDelete->next = NULL;
toDelete->~Node();
return true;
}
else
{
x.~Fruit();
return false;
}
}
}
void LList:😀isplay(ostream& out_stream) const
{
out_stream << "Below are the fruits currently in the list" << endl;
Node *ptr = list;
while (ptr != NULL)
{
out_stream << ptr->infoPtr << endl;
ptr = ptr->next;
}
out_stream << endl;
}
LList& LList:😱perator=(const LList& assignFrom)
{
this->list = assignFrom.list;
return *this;
}
[/CODE]
[CODE title="Fruit.cpp"]#include "Fruit.h";
#include <stdio.h>
#include <string>
#include "LeakWatcher.h"
using namespace std;
Fruit::Fruit() {
name = NULL;
*code = { 0 };
}
Fruit::~Fruit() {
delete name;
}
Fruit::Fruit(const Fruit& copyfrom)
{
Fruit* temp = new Fruit();
this->name = copyfrom.name;
*this->code = *copyfrom.code;
*this = *temp;
}
bool Fruit:😱perator<(Fruit const& obj)
{
return(strcmp(name, obj.name) < 0);
}
bool Fruit:😱perator==(Fruit const& obj)
{
return(strcmp(name, obj.name) == 0);
}
bool Fruit:😱perator!=(Fruit const& obj)
{
return (strcmp(name, obj.name) != 0);
}
Fruit Fruit:😱perator=(Fruit const& obj)
{
Fruit n = Fruit();
n.name = obj.name;
for (int i = 0; i < CODE_LEN; i++)
n.code = obj.code;
return n;
}
ostream& operator<<(std:😱stream& out, Fruit* f)
{
out << setiosflags(ios::left) << setw(MAX_NAME_LEN) << f->name;
for (int i = 0; i < CODE_LEN; i++)
out << f->code;
return out;
}
istream& operator>>(std::istream& in, Fruit* f)
{
char* temp = new char[MAX_NAME_LEN];
in >> temp;
for (int i = 0; i < CODE_LEN; i++)
in >> f->code;
f->name = temp;
return in;
}[/CODE]
[CODE lang="cpp" title="LList.cpp"]#include "LList.h"
#include "LeakWatcher.h"
using namespace std;
typedef Fruit InfoType;
LList::~LList() {
while (list) {
Node* n = new Node(list->infoPtr, list->next);
if (n->next)
{
list = list->next;
delete n;
}
else
delete list;
}
}
LList::LList(const LList& copyFrom)
{
Node* i = copyFrom.list;
while (i)
{
Node* n = new Node(i->infoPtr, i->next);
if(!this->list)
{
this->list = n;
}
else
{
list->next = n;
list = n;
}
i = i->next;
}
}
bool LList::IsEmpty() const {
return (list == NULL);
}
bool LList::Insert(InfoType* x_ptr)
{
if (IsEmpty())
{
Node* n = new Node(x_ptr, NULL);
list = n;
//cout << "list is: " << list->infoPtr << endl;
return true;
}
else if (list->next == NULL)
{
if (x_ptr < list->infoPtr)
{
Node* n = new Node(x_ptr, NULL);
}
}
else
{
Node *current = list;
while (current->next != NULL && current->next->infoPtr < x_ptr)
current = current->next;
if (current->infoPtr != x_ptr)
{
x_ptr->~Fruit();
return false;
}
else
{
Node* aft = current->next;
Node* w = new Node(x_ptr, NULL);
w->next = aft;
current->next = w;
//cout << "list is: " << list->infoPtr << endl;
return true;
}
}
}
bool LList:😀elete(const InfoType& x)
{
if (list == NULL)
return false;
else
{
Node* toDelete = new Node(list->infoPtr, list->next);
Node* prev = new Node(list->infoPtr, list->next);
while (*toDelete->infoPtr != x || toDelete == NULL)
{
prev = toDelete;
toDelete = toDelete->next;
}
if (toDelete == list)
{
list = list->next;
toDelete->~Node();
return true;
}
else if (toDelete)
{
prev->next = toDelete->next;
toDelete->next = NULL;
toDelete->~Node();
return true;
}
else
{
x.~Fruit();
return false;
}
}
}
void LList:😀isplay(ostream& out_stream) const
{
out_stream << "Below are the fruits currently in the list" << endl;
Node *ptr = list;
while (ptr != NULL)
{
out_stream << ptr->infoPtr << endl;
ptr = ptr->next;
}
out_stream << endl;
}
LList& LList:😱perator=(const LList& assignFrom)
{
this->list = assignFrom.list;
return *this;
}
[/CODE]
[CODE title="Fruit.cpp"]#include "Fruit.h";
#include <stdio.h>
#include <string>
#include "LeakWatcher.h"
using namespace std;
Fruit::Fruit() {
name = NULL;
*code = { 0 };
}
Fruit::~Fruit() {
delete name;
}
Fruit::Fruit(const Fruit& copyfrom)
{
Fruit* temp = new Fruit();
this->name = copyfrom.name;
*this->code = *copyfrom.code;
*this = *temp;
}
bool Fruit:😱perator<(Fruit const& obj)
{
return(strcmp(name, obj.name) < 0);
}
bool Fruit:😱perator==(Fruit const& obj)
{
return(strcmp(name, obj.name) == 0);
}
bool Fruit:😱perator!=(Fruit const& obj)
{
return (strcmp(name, obj.name) != 0);
}
Fruit Fruit:😱perator=(Fruit const& obj)
{
Fruit n = Fruit();
n.name = obj.name;
for (int i = 0; i < CODE_LEN; i++)
n.code = obj.code;
return n;
}
ostream& operator<<(std:😱stream& out, Fruit* f)
{
out << setiosflags(ios::left) << setw(MAX_NAME_LEN) << f->name;
for (int i = 0; i < CODE_LEN; i++)
out << f->code;
return out;
}
istream& operator>>(std::istream& in, Fruit* f)
{
char* temp = new char[MAX_NAME_LEN];
in >> temp;
for (int i = 0; i < CODE_LEN; i++)
in >> f->code;
f->name = temp;
return in;
}[/CODE]
.