The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 4 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Can you help explain to me what is wrong with my code? The member functions that are const are required to be const and i cannot change that.//
// PeopleList.cpp
// Project 3 Cs32
//
// Created by Merton Ung on 7/24/17.
// Copyright © 2017 Merton Ung. All rights reserved.
//
#include "PeopleList.h"
#include <iostream>
using namespace std;
PeopleList::PeopleList() // Create an empty In (i.e., one with no InfoType values)
{
head = NULL;
tail = NULL;
tail->next = NULL;
n = NULL;
length=0;
}
// return true if b is greater than a
bool PeopleList::compare(string a, string b)
{
for(int i=0; i<a.length(); i++)
{
if(b[i] > a[i])
{
return true;
}
}
return false;
}
//places tail
void PeopleList::setTail()
{
Node* temp1 = head;
for(int i=0; i<length; i++)
{
temp1 = temp1->next;
}
tail = temp1;
tail->next=NULL;
return;
}
bool PeopleList::empty() const // Return true if the list is empty, otherwise false.
{
if ( head== NULL)
{
return true;
}
else{
return false;
}
}
int PeopleList::size() const
{
return length;
}
// If the full name (both the first and last name) is not equal to any full name currently
// in the list then add it and return true. Elements should be added according to
// their last name. Elements with the same last name should be added according to
// their first names. Otherwise, make no change to the list and return false
// (indicating that the name is already in the list).
bool PeopleList::add(const string& firstName, const string& lastName, const InfoType& value)
{
Node* n;
n->num = value;
n->first = firstName;
n->last = lastName;
Node* temp1 = head;
Node* temp2 = NULL;
Node* temp3 = NULL;
if(head == NULL)
{
head->num = value;
head->first = firstName;
head->last = lastName;
head->prev = NULL;
length++;
setTail();
return true;
}
if(!compare(head->first,firstName) && !compare(head->last,lastName))
{
head = n->next;
n = head;
length++;
setTail();
}
for (int i = 0; i < length; i++)
{
if((temp1->first != firstName && temp1->last != lastName))
{
if((!compare(temp1->last,lastName)) && !compare(temp1->first,firstName))
{
temp3->next = temp2;
n = temp3->next;
temp3 = n->prev;
temp2 = n->next;
n = temp2->prev;
length++;
setTail();
return true;
}
}
temp3 = temp1;
temp1 = temp1->next;
}
return false;
}
// If the full name is equal to a full name currently in the list, then make that full
// name no longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// Otherwise, make no change to the list and return false.
bool PeopleList::change(const string& firstName, const string& lastName, const InfoType& value)
{
Node* temp1 = head;
for(int i =0; i<length; i++)
{
if(temp1->first == firstName && temp1->last == lastName)
{
temp1->num = value;
setTail();
return true;
}
temp1 = temp1->next;
}
return false;
}
// If full name is equal to a name currently in the list, then make that full name no
// longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// If the full name is not equal to any full name currently in the list then add it
// and return true. In fact this function always returns true.
bool PeopleList::addOrChange(const string& firstName, const string& lastName, const InfoType& value)
{
Node* temp = head;
for(int i =0; i<length; i++)
{
if(temp->first == firstName && temp->last == lastName)
{
temp->num = value;
return true;
}
temp = temp->next;
}
Node* n;
n->num = value;
n->first = firstName;
n->last = lastName;
Node* temp1 = head;
Node* temp2=NULL;
Node* temp3=NULL;
if(head == NULL)
{
head->num = value;
head->first = firstName;
head->last = lastName;
length++;
setTail();
}
if(!compare(head->first,firstName) && !compare(head->last,lastName))
{
head = n->next;
n = head;
length++;
setTail();
}
for (int i = 0; i < length; i++)
{
if((!compare(temp1->last,lastName)) && !compare(temp1->first,firstName))
{
temp3->next = temp2;
n = temp3->next;
temp3 = n->prev;
temp2 = n->next;
n = temp2->prev;
length++;
setTail();
return true;
}
temp3 = temp1;
temp1 = temp1->next;
}
return true;
}
// If the full name is equal to a full name currently in the list, remove the
// full name and value from the list and return true. Otherwise, make
// no change to the list and return false.
bool PeopleList::remove(const string& firstName, const string& lastName)
{
Node* temp1 = head;
if(head == NULL)
{
return false;
}
for(int i=0; i<length; i++)
{
if(temp1->first == firstName && temp1->last == lastName)
{
if(length==1)
{
head = NULL;
length--;
setTail();
return true;
}
else
{
if(temp1->first== head->first && temp1->last == head->last && temp1->num == head->num)
{
head=head->next;
head->prev = NULL;
length--;
setTail();
return true;
}
else if(temp1->next ==NULL)
{
temp1->prev->next=NULL;
length--;
setTail();
return true;
}
else
{
temp1->prev->next = temp1->next;
temp1->next->prev = temp1->prev;
length--;
setTail();
return true;
}
}
}
}
return false;
}
// Return true if the full name is equal to a full name currently in the list, otherwise
// false.
bool PeopleList::contains(const string& firstName, const string& lastName) const
{
Node* temp1 = head;
for(int i =0; i<length; i++)
{
if(temp1->first == firstName && temp1->last == lastName)
{
return true;
}
temp1 = temp1->next;
}
return false;
}
// If the full name is equal to a full name currently in the list, set value to the
// value in the list that that full name maps to, and return true. Otherwise,
// make no change to the value parameter of this function and return
// false.
bool PeopleList::lookup(const string& firstName, const string& lastName, InfoType& value) const
{
Node* temp1 = head;
for(int i =0; i<length; i++)
{
if(temp1->first == firstName && temp1->last == lastName)
{
value = temp1->num;
return true;
}
temp1 = temp1->next;
}
return false;
}
// If 0 <= i < size(), copy into firstName, lastName and value parameters the
// corresponding information of the element at position i in the list and return
// true. Otherwise, leave the parameters unchanged and return false.
// (See below for details about this function.)
bool PeopleList::get(int i, string& firstName, string& lastName, InfoType& value) const
{
Node* temp1 = head;
if(0 <= i < length)
{
return false;
}
else
{
for(int a =0; a<i; i++)
{
temp1 = temp1->next;
}
firstName = temp1->first;
lastName = temp1->last;
value = temp1->num;
return true;
}
}
void PeopleList::swap(PeopleList& other)
{
Node* p = other.head;
other.head = head;
head = p;
// Swap sizes
int s = other.length;
other.length = length;
length = s;
};
int main()
{
PeopleList m;
m.add("Skyler", "White", "i");
m.add("James", "McGill", "am");
m.add("Charles", "McGill", "a");
m.add("Walter", "White", "bad");
m.add("Jesse", "Pinkman", "bitch");
for (int n = 0; n < m.size(); n++)
{
string f;
string l;
int v;
m.get(n, f, l, v);
cout << f << " " << l << " " << v << endl;
}
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly