0
我寫了一個程序,只輸出單鏈表,它工作得很好,但它是兩次輸出的最後一個字符(例如,如果要輸出的字是它輸出DADD DAD)單鏈表輸出額外的字符
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
struct nodeType
{
\t char num;
\t nodeType *next;
};
int main()
{
\t infile.open("TextFile2.txt");
\t if (!infile)
\t \t cout << "Cannot open the file." << endl;
\t char digit;
\t nodeType *head = NULL, *trail = NULL, *current = NULL;
\t while (!infile.eof())
\t {
\t \t infile >> digit;
\t \t if (head == NULL)
\t \t {
\t \t \t head = new nodeType;
\t \t \t head->num = digit;
\t \t \t head->next = NULL;
\t \t \t trail = head;
\t \t }
\t \t else
\t \t {
\t \t \t current = new nodeType;
\t \t \t current->num = digit;
\t \t \t current->next = NULL;
\t \t \t trail->next = current;
\t \t \t trail = current;
\t \t }
\t }
\t current = head;
\t while (current != NULL)
\t {
\t \t cout << current->num;
\t \t current = current->next;
\t }
}