Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct TList
- {
- string word;
- int counter;
- TList* next;
- };
- bool isEmpty(TList* lst)
- {
- return lst==NULL;
- }
- int addElemToDict(TList*& lst, string newValue)
- {
- if(!isEmpty(lst))
- {
- TList* temp;
- TList* prev = NULL;
- temp = lst;
- while((temp!=NULL)&&(temp->word<newValue)&&(temp->word!=newValue))
- {
- prev = temp;
- temp = temp->next;
- }
- if(temp==NULL)
- {
- TList * newNode = new TList;
- newNode->word = newValue;
- newNode->counter = 1;
- prev->next = newNode;
- newNode->next = NULL;
- return 0;
- }
- if(temp->word==newValue)
- {
- temp->counter++;
- return 0;
- }
- if(temp->word>newValue)
- {
- TList* newNode = new TList;
- newNode->word = newValue;
- newNode->counter = 1;
- if(prev!=NULL)
- {
- prev->next = newNode;
- newNode->next = temp;
- return 0;
- }
- else
- {
- newNode->next = lst;
- lst = newNode;
- return 0;
- }
- }
- }
- else
- {
- TList * newNode = new TList;
- newNode->word = newValue;
- newNode->counter = 1;
- newNode->next = NULL;
- lst = newNode;
- }
- return 0;
- }
- int printList(TList* lst)
- {
- while(lst!=NULL)
- {
- cout<<lst->word<<" "<<lst->counter<<endl;
- lst = lst->next;
- }
- return 0;
- }
- int clearList(TList* lst)
- {
- TList* tmp = NULL;
- while(lst!=NULL)
- {
- tmp = lst;
- lst = lst->next;
- delete tmp;
- }
- return 0;
- }
- int createDict(TList*& lst, ifstream& inpf)
- {
- string word;
- while(!inpf.eof())
- {
- inpf>>word;
- addElemToDict(lst, word);
- }
- return 0;
- }
- int main()
- {
- setlocale(0,"Russian");
- TList* List = NULL;
- ifstream inputFile("input.txt");
- if(inputFile)
- {
- createDict(List,inputFile);
- printList(List);
- clearList(List);
- return 0;
- }
- else
- {
- cout<<"Не удалось открыть файл для чтения"<<endl;
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement