Hello
I have to do a sorted list code according to some instructions. a friend and I worked on this, and we pretty much ended up with the same code, his worked, but mine isnt, even though its exactly same!
I have been trying for hours figuring how to make a file and why the 5.5 in main() in x.retrieve keeps resulting in errors. we are supposed to read a list from a file, (named float.txt, or can be IN visual studio which i forgot how to to do, and searched the internet for an hour and nothing good comes up)and insert, delete, and retrieve an item. the program did that, but only to my friend. I have no idea why isnt this working out for me!
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#define MAX_ITEMS 10
using namespace std;
typedef float ItemType;
float list[10];
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
struct Node
{ItemType data;
Node* next;
}typedef *nodeptr;
nodeptr location;
nodeptr trailing;
nodeptr head;
Node* listdata;
int currentPos;
public:
SortedList(){ length = 0; currentPos=-1; location = NULL; head = NULL; }; // default constructor
bool IsFull() // test if the list is full
{
if (length < 9)
return false;
else
return true;
}
void MakeEmpty()
{
nodeptr z;
while (listdata != NULL)
{
z = listdata;
listdata = listdata->next;
delete z;
}
length = 0; // let length=0
}
void InsertItem(ItemType x) // insert x into the list
{
bool moreToSearch; moreToSearch = (location != 0);
nodeptr z = new Node;
z->next = NULL;
z->data = x;
if (head != NULL)
{
location = head;
while (location->next != NULL)
{
location = location->next;
}
location->next = z; length++;
}
else
head = z;
}
void DeleteItem(ItemType x) // delete x from the list
{nodeptr y = NULL;
location = head;
trailing = head;
while (location != NULL&& location->data != x)
{
trailing = location;
location = location->next;
}
if (location == NULL)
{
cout << "ERROR! Deletion value nonexistant." << endl;
delete y;
}
else
{
y = location;
location = location->next;
trailing->next = location;
delete y; length--;
cout << x << " was DELETED." << endl << endl;
}
}
/*bool IsFull() // test if the list is full
{
nodeptr location;
try
{
location = new Node;
delete location;
return false;
}
catch (bad_alloc exception)
{
return true;
}
}*/
void Lengthls(){ cout << length << endl; } // return length
void RetrieveItem(ItemType &x, bool &found) // retrieve x from the list, the boolean result is stored in found
{
location = 0;
nodeptr y = NULL;
location = head;
trailing = head;
while (location != NULL&& location->data != x)
{
trailing = location;
location = location->next;
}
if (location == NULL)
{
cout << "ERROR!" << x << " NOT FOUND." << endl;
}
else
{
y = location;
location = location->next;
trailing->next = location;
cout << x << " was FOUND." << endl;
}
}
void ResetList() // currentPos=-1
{
currentPos = -1;
}
float GetNextItem() // get the next element from the list with respect to the currentPos
{
currentPos++;
float y = list[currentPos];
return y;
}
void printlist()
{
location = head;
while (location != NULL)
{
cout << location->data << endl;
location = location->next;
}
}
};
/*void SortedList::InsertItem(ItemType x)
{
bool moreToSearch; moreToSearch = (location != 0);
nodeptr z = new Node;
z->next = NULL;
z->data = x;
if (head != NULL)
{
location = head;
while (location->next != NULL)
{
location = location->next;
}
location->next = z;
}
else
head = z;
}*/
/*void SortedList::DeleteItem(ItemType x)
{
nodeptr y = NULL;
location = head;
trailing = head;
while (location != NULL&& location->data != x)
{
trailing = location;
location = location->next;
}
if (location == NULL)
{
cout << "ERROR! Deletion value nonexistant." << endl;
delete y;
}
else
{
y = location;
location = location->next;
trailing->next = location;
delete y;
cout << x << " was DELETED." << endl << endl;
}
}*/
/*void SortedList::RetrieveItem(ItemType &x, bool &found)
{
location = 0;
nodeptr y = NULL;
location = head;
trailing = head;
while (location != NULL&& location->data != x)
{
trailing = location;
location = location->next;
}
if (location == NULL)
{
cout << "ERROR! NOT FOUND." << endl;
}
else
{
y = location;
location = location->next;
trailing->next = location;
cout << x << " was FOUND" << endl;
}
}*/
int main()
{
SortedList x;
ifstream inFile; ofstream output;
inFile.open("C:\\Users\\name\\Documents\\Visual Studio 2013\\Projects\\CIS 200 project 2\\CIS 200 project 2 myname q2\\CIS 200 project 2 Ques.2\\float.txt");
string line;
int i;
for (i = 0; i < 10; i++)
{
getline(inFile, line);
list[i] =::atof(line.c_str());
x.InsertItem(list[i]);
}
x.ResetList();
x.InsertItem(100);
x.printlist();
cout << endl << "The length is: "; x.Lengthls();
cout << endl;
cout << "The list that is made is: " << endl << endl;
x.DeleteItem(2);
x.printlist();
cout << endl;
bool allAboutLists;
x.RetrieveItem(5.5, allAboutLists);
cout << endl;
cout << "Is the List FULL?: " << boolalpha << x.IsFull() << endl;
cout << "The next item is: " << x.GetNextItem() << endl;
x.ResetList();
output.open("C:\\Users\\name\\Documents\\Visual Studio 2013\\Projects\\CIS 200 project 2\\CIS 200 project 2 myname q2\\CIS 200 project 2 Ques.2\\output.txt");
for (int file = 0; file<10; file++)
{
output << x.GetNextItem() << endl;
}
system("pause");
return 0;
}