Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class LinkedList
{
public:
LinkedList();
~LinkedList();
void push_back(string word);
void pop_front();
bool empty() const;
void save();
private:
struct Node
{
string item;
Node* next;
};
int Size;
Node *head, *tail, *cur, *prev;
};
LinkedList::LinkedList():Size(0)
{
head = tail = cur = prev = NULL;
}
LinkedList::~LinkedList()
{
while(!empty())
pop_front();
}
void LinkedList::push_back(string word)
{
if(empty())
{
head = new Node;
head->item = word;
head->next = NULL;
tail = head;
}
else
{
Node* slot = new Node;
slot->item = word;
slot->next = NULL;
tail->next = slot;
tail = slot;
}
Size++;
}
void LinkedList::pop_front()
{
if(empty())
{
cout << "Error: Attempt to pop empty list" << endl;
system("pause");
exit(1);
}
Node* oldhead = head;
head = head->next;
delete oldhead;
Size--;
}
bool LinkedList::empty() const
{
if(head == NULL)
return true;
else
return false;
}
void LinkedList::save()
{
ofstream fout("index.txt");
if(fout.is_open())
{
cur=head;
while(cur != NULL)
{
fout << cur->item << endl;
cur = cur->next;
}
}
}
int main()
{
LinkedList index;
ifstream fin_noindex;
ifstream fin_f1;
string s, temp;
int found;
fin_f1.open("file2.txt");
while(fin_f1.is_open())
{
fin_f1 >> s;
fin_noindex.open("noindex.txt");
found = 0;
while(fin_noindex.is_open())
{
fin_noindex >> temp;
if(s==temp)
{
found++;
}
}
fin_noindex.close();
if(found==0)
{
index.push_back(s);
}
}
fin_f1.close();
index.save();
return 0;
}
i want to write a program that read file2.txt and compare each words in file2.txt with noindex.txt. If there are any words which is in file2.txt but not in noindex.txt, i am suppose to put it in a linked list, then later save the linked list in index.txt.
the problem now is: why the program went to an infinite loop ?
actually i am not so sure wether my implementation or way is correct. Please correct and comment on my code. Thanks in advance.