Trouble with Sorted Linked List
Feb 21, 2017 at 3:45am UTC
I'm converting a previous program that used a linked list to display data from a .csv file into a sorted linked list (eventually I will be introducing an additional .csv file that contains updates the file). However right now I just want I have to display. I'm getting a bunch of error C2784 on list.h line 157. I'm pretty sure the problem is in either my find function or getMember function. I having been staring at it too long and need fresh eyes. Any help would greatly be appreciated. If you need the .csv info I can post it.
Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
using namespace std;
#ifndef LIST_H
#define LIST_H
template <class List>
struct Node
{
List info;
Node<List> *next;
};
//********************************************************************
template <class List>
class Items
{
private :
Node<List> *start;
Node<List> *ptr;
Node<List> *current;
public :
Items();
~Items();
bool first(List&);
bool getNext(List&);
bool getMember(List&);
bool insert(List item);
bool erase(List item);
bool update(List item);
bool find(List item, Node<List>*& pPrev);
};
#endif
//********************************************************************
template <class List>
Items<List>::Items()
{
start = NULL;
current = start;
ptr = new Node<List>;
}
//********************************************************************
template <class List>
Items<List>::~Items()
{
while (start != NULL)
{
current = start;
start = start->next;
delete current;
}
current = NULL;
}
//********************************************************************
template <class List>
bool Items<List>::first(List& element)
{
if (start == NULL)
return false ;
current = start;
element = current->info;
return true ;
}
//********************************************************************
template <class List>
bool Items<List>::getNext(List& element)
{
current = current->next;
if (current == NULL)
return false ;
element = current->info;
return true ;
}
//********************************************************************
template <class List>
bool Items<List>::insert(List item) {
Node<List>* pPrev;
bool found = false ;
found = find(item, pPrev); // note the use of the find function
if (!found){
Node<List>* pNew = new Node<List>;
pNew->info = item;
if (pPrev != NULL){
pNew->next = pPrev->next;
pPrev->next = pNew;
}
else {
pNew->next = start;
start = pNew;
}
}
else
found = true ;
return found;
}
//********************************************************************
template <class List>
bool Items<List>::erase(List item) {
Node<List>* pPrev;
Node<List>* ptr;
bool found = false ;
found = find(item, pPrev); // note the use of the find function
if (found) {
if (pPrev != NULL) {
ptr = pPrev->next;
pPrev->next = ptr->next;
}
else {
ptr = start;
start = ptr->next;
}
delete ptr;
}
return found;
}
//********************************************************************
template <class List>
bool Items<List>::update(List item) {
Node<List>* pPrev;
bool found = false ;
found = find(item, pPrev); //note the use of find function
if (found) {
if (pPrev == NULL)
start->info = item;
else
pPrev->next->info = item;
}
return found;
}
//********************************************************************
template <class List>
bool Items<List>::find(List item, Node<List>*& pPrev) {
bool found = false ;
pPrev = NULL; // set the address of the previous node to NULL
Node<List>* ptr = start;
while (ptr != NULL && !found && item >= ptr->info) {
if (item == ptr->info)
found = true ;
else {
pPrev = ptr; // capture the address of the previous node
ptr = ptr->next;
}
}
return found;
}
//********************************************************************
template <class List>
bool Items<List>::getMember(List& item)
{
Node<List> *ptr = start;
Node<List>* pPrev;
bool found = false ;
found = find(item, pPrev);
while (ptr != NULL && !found)
{
if (ptr->info == item)
{
found = true ;
item = ptr->info;
}
if (!found)
ptr = ptr->next;
}
return found;
}
Source File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "List.h"
using namespace std;
struct Inventory
{
int code;
string description;
double cost;
double amount;
double value;
Inventory(){ code = 0; cost = 0.0; description = "Unassigned" ; }
bool operator == (const Inventory & s)
{
if (code == s.code) return true ; return false ;
}
};
//********************************************************************
void getData(Items<Inventory>&);
void displayList(Items<Inventory>&);
void displayInventory(Items<Inventory>&);
//********************************************************************
int main()
{
Items<Inventory> WebsterBakery;
getData(WebsterBakery);
displayInventory(WebsterBakery);
displayList(WebsterBakery);
return 0;
}
//********************************************************************
void getData(Items<Inventory>& data)
{
Inventory ingredient;
ifstream inFile("Inventory.csv" );
if (!inFile)
{
cout << "Problem opening Inventory.csv file" << endl;
exit(99);
}
inFile >> ingredient.code;
inFile.ignore();
getline(inFile, ingredient.description, ',' );
inFile >> ingredient.cost;
inFile.ignore();
inFile >> ingredient.amount;
ingredient.value = ingredient.cost * ingredient.amount;
inFile.ignore();
while (!inFile.eof())
{
data.insert(ingredient);
inFile >> ingredient.code;
inFile.ignore();
if (ingredient.code != 0)
{
getline(inFile, ingredient.description, ',' );
inFile >> ingredient.cost;
inFile.ignore();
inFile >> ingredient.amount;
ingredient.value = ingredient.cost * ingredient.amount;
inFile.ignore();
}
}
}
//********************************************************************
void displayList(Items<Inventory>& i)
{
Inventory ingredient;
int code;
bool found = false ;
cout << "Enter the code of the ingredient to display: " ;
cin >> code;
while (code != 0)
{
ingredient.code = code;
found = i.getMember(ingredient);
if (found)
cout << ingredient.code << '\t' << setw(20)
<< left << ingredient.description << '\t' << setw(4)
<< right << fixed << setprecision(2) << ingredient.cost
<< '\t' << setw(4) << right << fixed << setprecision(2)
<< ingredient.amount << '\t' << setw(4) << right << fixed
<< setprecision(2) << ingredient.value << endl;
else
cout << "Ingredient " << code << " not found in inventory\n" ;
cout << "Enter the code of the ingredient to display: " ;
cin >> code;
}
}
//********************************************************************
void displayInventory(Items<Inventory>& s)
{
Inventory ingredient;
double totalAmount = 0;
double totalValue = 0;
double totalCost = 0;
double avgCost = 0;
int count = 0;
bool found = false ;
cout << "Code " << setw(14) << right << "Ingredient "
<< setw(18) << right << "Cost " << setw(10) << right
<< "Amount " << setw(6) << right << "Value" << endl;
cout << endl;
cout << "-----------------------------------------"
<< "--------------------------------" << endl;
cout << endl;
found = s.first(ingredient);
if (found)
{
while (found)
{
cout << ingredient.code << '\t' << setw(20) << left
<< ingredient.description << '\t' << setw(4) << right
<< fixed << setprecision(2) << ingredient.cost << '\t'
<< setw(4) << right << fixed << setprecision(2)
<< ingredient.amount << '\t' << setw(4) << right
<< fixed << setprecision(2) << ingredient.value << endl;
totalAmount += ingredient.amount;
totalValue += ingredient.value;
totalCost += ingredient.cost;
found = s.getNext(ingredient);
count++;
}
cout << endl;
cout << "The total inventory amount is: "
<< totalAmount << endl;
cout << "The total inventory value is: "
<< totalValue << endl;
avgCost = totalCost / count;
cout << "The average ingredient cost is: "
<< avgCost << endl;
cout << endl;
}
else
cout << "The list is empty" << endl;
}
Feb 21, 2017 at 10:40am UTC
Perhaps someone will go through your 300 lines of code and give you a detailed reply but, if not, let's try and encapsulate the problem:
I'm converting a previous program that used a linked list to display data from a .csv file into a sorted linked list (eventually I will be introducing an additional .csv file that contains updates the file). However right now I just want I have to display .
So it appears that you have the linked list up and running and the problem is in the display i.e. printing the list? In that case you check if the start node is valid (i.e. the list exists) and then while(start){ print start; start = start -> next;} through the list though this is a fairly basic exercise and therefore I suspect that you might be having other problems
So if the problem is at an earlier stage then be specific where exactly it is and what is it that is going wrong
Topic archived. No new replies allowed.