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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
class LList
{
private:
//a Food struct stores info for a food
struct Food
{
string name, group;
float cal, per;
Food* next;
Food()
{
name="nothing";
group="nothing";
cal=0;
per=0;
next=NULL;
}
};
public:
typedef Food* FoodPtr;
FoodPtr head, tail, index;
//function prototypes go here
void AddNode(FoodPtr head, FoodPtr tail, FoodPtr index);
FoodPtr PrevNode(FoodPtr index);
void Forward();
void Backward();
void DelNode(FoodPtr index);
FoodPtr NextNode();
FoodPtr PrevNode();
FoodPtr ReadFood(ifstream &inFile);
//constructor
LList();
//destructor
~LList();
//function prototypes end here
};
//////////////////////// FUNCTION DEFINITIONS /////////////////////////
//function definitions go here
void LList::AddNode(FoodPtr head, FoodPtr tail, FoodPtr index)
{
// original
// tail->next= new Food;
// tail=tail->next;
//testing new code
ifstream inFile;
inFile.open("file.txt");
FoodPtr temp;
while ((temp=ReadFood(inFile)))
{
//add Food to list
if (head==NULL)
{
//creates head of list
head=temp;
//node is only one in the list, is both head and tail right now
tail=temp;
}
else
{
//current Food, tail, now references new Food
tail->next=temp;
//new food becomes tail, referenced by previous Food
tail=temp;
}
}
//end of list
index=tail;
tail->next=NULL;
//always close the stream
inFile.close();
}
FoodPtr LList::PrevNode(FoodPtr index)
{
FoodPtr temp=head;
if (index==head) //no previous node, return head
{
return head;
}
while (temp->next!=index)
{
temp=temp->next;
}
return temp;
}
void LList::Forward()
{
if (index->next!=NULL)
{
index=index->next;
}
}
void LList::Backward()
{
if (index!=head)
{
index=PrevNode(index);
}
}
void LList::DelNode(FoodPtr index)
{
FoodPtr deltor=index;
if (index==head) //delete the head
{
head=head->next;
delete deltor;
}
else if (index==tail) //delete the tail
{
tail=PrevNode(index);
tail->next=NULL;
delete deltor;
}
else //delete a middle node
{
index=PrevNode(index);
index->next=deltor->next;
delete deltor;
}
index=head;
}
FoodPtr ReadFood(ifstream &inFile)
{
FoodPtr temp=NULL;
//temp values
string tName, tGroup;
float tCal, tPer;
if (inFile >> tName >> tGroup >> tCal >> tPer)
{
temp=new Food;
//store infor in created object
temp->name=tName;
temp->group=tGroup;
temp->cal=tCal;
temp->per=tPer;
}
return temp;
}
LList::LList()
{
FoodPtr temp;
head=new Food;
tail=head;
temp=head;
}
LList::~LList()
{
FoodPtr temp, deltor;
temp=deltor=head;
while (temp!=NULL)
{
temp=temp->next;
delete deltor;
deltor=temp;
}
}
|