C++, Hi, my code is not reading in the \"int.dat\" file at all. Here is what I h
ID: 3882065 • Letter: C
Question
C++, Hi, my code is not reading in the "int.dat" file at all. Here is what I have
#include <iostream>
#include <fstream>
using namespace std;
class ItemList
{
private:
struct ListNode
{
int value;
ListNode * next;
};
ListNode * head;
public:
ItemList();
// Post: List is the empty list.
bool IsThere(int item) const;
// Post: If item is in the list IsThere is
// True; False, otherwise.
void Insert(int item);
// Pre: item is not already in the list.
// Post: item is in the list.
void Delete(int item);
// Pre: item is in the list.
// Post: item is no longer in the list.
void Print() const;
// Post: Items on the list are printed on the screen.
int GetLength() const;
// Post: Length is equal to the number of items in the
// list.
~ItemList();
// Post: List has been destroyed.
};
int main()
{
ItemList obj;
ifstream inFile("int.dat");
if (inFile.is_open())
{
while (!inFile.eof())
{
int value;
inFile >> value;
obj.Insert(value);
cout << value << endl;
}
inFile.close();
}
obj.Print();
cout << "There are " << obj.GetLength() << " items in the list" << endl;
obj.Delete(-47);
obj.Delete(1926);
obj.Delete(2000);
cout << "After deletion, there are " << obj.GetLength() << " items in the list" << endl;
obj.Print();
system("pause");
return 0;
}
ItemList::ItemList()
{
head = NULL;
}
bool ItemList::IsThere(int item) const
{
struct ListNode * current = head;
while (current != NULL)
{
if (current->value == item)
{
return true;
current = current->next;
}
else
{
return false;
}
}
}
void ItemList::Insert(int item)
{
struct ListNode * tempNode = head;
struct ListNode * newNode = new ListNode;
if (!IsThere(item))
{
newNode->value = item;
newNode->next = NULL;
if (tempNode != NULL)
{
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
tempNode->next = newNode;
}
else
{
head = newNode;
}
}
}
void ItemList::Delete(int item)
{
ListNode * nodePtr;
ListNode * previousNode;
if (IsThere(item))
{
nodePtr = head;
previousNode = NULL;
while (nodePtr != NULL)
{
if (nodePtr->value == item)
{
if (nodePtr == head)
{
head = head->next;
}
else
{
previousNode->next = nodePtr->next;
}
free(nodePtr);
break;
}
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
}
}
void ItemList::Print() const
{
ListNode * p = head;
if (p == NULL)
{
cout << "The list is empty " << endl;
return;
}
if (p->next == NULL)
{
cout << p->value;
cout << endl;
}
else
{
do
{
cout << p->value;
p = p->next;
cout << endl;
} while (p != NULL);
}
}
int ItemList::GetLength() const
{
int index = 0;
ListNode * current = head;
while (current)
{
index++;
current = current->next;
}
return index;
}
ItemList::~ItemList()
{
ListNode * nodePtr;
ListNode * nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
Here is what my output looks like
Here is what it is supposed to look like
1492 1776 1860 23 1698 1935 1926 1953 1960 1492 1776 1860 23 1698 1935 1926 1953 1960 47 The list is empty There are e items in the list After deletion, there are e items in the 1ist The list is empty Press any key to continue . . .Explanation / Answer
Fixed your code. Also insertion of items is done such that the list is always in sorted order. The screen shot shown by you indicates that sorted list should be maintained. So fixed code for that. Other functions as well are fixed. Shown below is the output. Hope it helped. If it did, please do rate the answer. Thank you.
#include <iostream>
#include <fstream>
using namespace std;
class ItemList
{
private:
struct ListNode
{
int value;
ListNode * next;
};
ListNode * head;
public:
ItemList();
// Post: List is the empty list.
bool IsThere(int item) const;
// Post: If item is in the list IsThere is
// True; False, otherwise.
void Insert(int item);
// Pre: item is not already in the list.
// Post: item is in the list.
void Delete(int item);
// Pre: item is in the list.
// Post: item is no longer in the list.
void Print() const;
// Post: Items on the list are printed on the screen.
int GetLength() const;
// Post: Length is equal to the number of items in the
// list.
~ItemList();
// Post: List has been destroyed.
};
int main()
{
ItemList obj;
ifstream inFile("int.dat");
if (inFile.is_open())
{
int value;
while (inFile >> value)
{
obj.Insert(value);
}
inFile.close();
}
cout << "There are " << obj.GetLength() << " items in the list" << endl;
obj.Print();
obj.Delete(-47);
obj.Delete(1926);
obj.Delete(2000);
cout << "After deletion, there are " << obj.GetLength() << " items in the list" << endl;
obj.Print();
system("pause");
return 0;
}
ItemList::ItemList()
{
head = NULL;
}
bool ItemList::IsThere(int item) const
{
struct ListNode * current = head;
while (current != NULL)
{
if (current->value == item)
{
return true;
}
current = current->next;
}
return false;
}
void ItemList::Insert(int item)
{
struct ListNode * prev = NULL;
struct ListNode * curr = head;
struct ListNode * newNode = new ListNode;
if (!IsThere(item))
{
cout << item << " is not in the list" << endl;
newNode->value = item;
newNode->next = NULL;
if(head == NULL)
head = newNode;
else
{
//insert in a sorted order, find the place where to insert
while(curr != NULL && item > curr->value)
{
prev = curr;
curr = curr->next;
}
newNode->next = curr;
if(curr == head) //insert as head node?
head = newNode;
else
prev->next = newNode;
}
}
else
{
cout << item << " is already in the list" << endl;
}
}
void ItemList::Delete(int item)
{
ListNode * nodePtr;
ListNode * previousNode;
if (IsThere(item))
{
nodePtr = head;
previousNode = NULL;
while (nodePtr != NULL)
{
if (nodePtr->value == item)
{
if (nodePtr == head)
{
head = head->next;
}
else
{
previousNode->next = nodePtr->next;
}
free(nodePtr);
break;
}
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
}
}
void ItemList::Print() const
{
ListNode * p = head;
if (p == NULL)
{
cout << "The list is empty " << endl;
return;
}
while(p != NULL)
{
cout << p->value;
p = p->next;
cout << endl;
}
}
int ItemList::GetLength() const
{
int index = 0;
ListNode * current = head;
while (current)
{
index++;
current = current->next;
}
return index;
}
ItemList::~ItemList()
{
ListNode * nodePtr;
ListNode * nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
1492 is not in the list
1776 is not in the list
1860 is not in the list
-23 is not in the list
1698 is not in the list
1935 is not in the list
1926 is not in the list
1953 is not in the list
1960 is not in the list
1492 is already in the list
1776 is already in the list
1860 is already in the list
-23 is already in the list
1698 is already in the list
2000 is not in the list
1935 is already in the list
1926 is already in the list
1953 is already in the list
1960 is already in the list
-47 is not in the list
There are 11 items in the list
-47
-23
1492
1698
1776
1860
1926
1935
1953
1960
2000
After deletion, there are 8 items in the list
-23
1492
1698
1776
1860
1935
1953
1960
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.