I am using struct to create a node and a linked list. But I got an error about the tail of the linked list and I don't know why. The purpose of the task is to load the data in the "items.txt" into the linked list I created. But it seems that the error occurs in the tail of the linked list. But I don't know how to fix this.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct item
{
string itemId, title, type, loan, copy, fee, genre;
};
typedef struct item Item;
struct node
{
Item data;
node* next;
};
typedef struct node Node;
struct linkedList
{
Node* head;
Node* tail;
};
typedef struct linkedList list;
void createList(list& list)
{
list.head = NULL;
list.tail = NULL;
}
Node* createNode(item newData)
{
Node* point = new Node;
if (point == NULL)
{
cout << "Failed." << endl;
return NULL;
}
point->data = newData;
point->next = NULL;
return point;
}
void insertHead(list& list, Node* point)
{
if (list.head == NULL)
{
list.head = list.tail = point;
}
else
{
point->next = list.head;
list.head = point;
}
}
void appenTail(list& list, Node* point)
{
if (list.head == NULL)
{
list.head = point;
list.tail = point;
}
else
{
list.tail->next = point;
list.tail = point;
}
}
void readFirstLine(ifstream& file, Item& item)
{
getline(file, item.itemId, ',');
getline(file, item.title, ',');
getline(file, item.loan, ',');
getline(file, item.copy, ',');
getline(file, item.fee, ',');
getline(file, item.genre, '
');
}
void readAllFile(ifstream& file, list& list)
{
while (!file.eof())
{
Item item;
readFirstLine(file, item);
Node* point = createNode(item);
appenTail(list, point);
}
}
void printLine(item item)
{
cout << item.itemId << endl;
cout << item.title << endl;
cout << item.loan << endl;
cout << item.copy << endl;
cout << item.fee << endl;
cout << item.genre << endl;
}
void printAll(list list)
{
int count = 1;
for (Node* point = list.head; point != NULL; point = point->next)
{
cout << "Item number " << count;
printLine(point->data);
}
}
int main(int argc, char* argv[])
{
Item item;
list list;
ifstream file("items.txt", ios::in);
string line;
if (!file)
{
cerr << "Error in opening file.";
exit(EXIT_FAILURE);
}
readAllFile(file, list);
printAll(list);
file.close();
return 0;
}
The problem i got is in the line 70.
Thanks in advance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…