<分区>
我编写了一个程序来只输出一个链表,它工作得很好,但是它输出最后一个字符两次(例如,如果要输出的单词是 DAD,它会输出 DADD)
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
struct nodeType
{
char num;
nodeType *next;
};
int main()
{
infile.open("TextFile2.txt");
if (!infile)
cout << "Cannot open the file." << endl;
char digit;
nodeType *head = NULL, *trail = NULL, *current = NULL;
while (!infile.eof())
{
infile >> digit;
if (head == NULL)
{
head = new nodeType;
head->num = digit;
head->next = NULL;
trail = head;
}
else
{
current = new nodeType;
current->num = digit;
current->next = NULL;
trail->next = current;
trail = current;
}
}
current = head;
while (current != NULL)
{
cout << current->num;
current = current->next;
}
}