//Written by Morteza Kashi
#include<iostream>
#include<fstream>
#include<string>

ofstream fout;
ifstream fin;
class node;
typedef node* nodeptr;
nodeptr top;
nodeptr newnode;
int counter=0;

class node{
public:
// node();
// ~node();
 nodeptr next;
 int number;

 string word;
 void insert(nodeptr&,string);
 void destroy(nodeptr&);
 void print(nodeptr top);
 string findmax(nodeptr top);
};

// node::node(){
//   cout<<"constructer called"<<endl;
//   top=NULL;
// }
// node::~node(){
 //  cout<<"Destructor called"<<endl;
//   destroy(top);

//}


  void node::insert(nodeptr& top,string word1){
 //   number_of_words++;
 //   cout <<"I am in insert"<<endl;
 //   cout<<word1<<endl;
    nodeptr curr=top,prev=NULL;
    newnode=new node;
    bool found=false;
    if(top==NULL){
    cout<<"null is processed"<<endl;
       newnode->word=word1;
       newnode->number=1;
       top=newnode;
       newnode->next=NULL;
     }//if
    else{

        while((curr!=NULL)&&(!found)){
           if(word1!=curr->word){
              prev=curr;
              curr=curr->next;
            }//if
           else found=true;
          }//while
        if(curr==NULL){
           newnode->word=word1;
           newnode->number=1;
           newnode->next=curr;
           prev->next=newnode;
         }//if

        else{
            if(word1==curr->word)
            curr->number=curr->number+1;
            else{
               if(prev==NULL){
                  newnode->word=word1;
                  newnode->number=1;
                  newnode->next=curr;
                  top=newnode;
                 }//if
               else{
                  newnode->word=word1;
                  newnode->number=1;
                  newnode->next=curr;
                  prev->next=newnode;
                  }//else
             }//else
         }//else
     }//else
  }//insert

void node::destroy(nodeptr& top){

    nodeptr curr,temp;
    curr=top;
    while(curr!=NULL){
       temp=curr;
       curr=curr->next;
       delete temp;
      }//while
    top=NULL;

 }//destroy

 void node::print(nodeptr top){

    nodeptr curr=top;
    while(curr!=NULL){
      counter++;
      cout<<curr->word<<"     "<<curr->number<<endl;
      fout<<curr->word<<"     "<<curr->number<<endl;
      curr=curr->next ;
    }//while
  }

 string node::findmax(nodeptr top){
    nodeptr curr=top;
    int max=-1;
    while(curr!=NULL){
    if(curr->number>max)
      max=curr->number;
    curr=curr->next;
    }//while
    cout<<"Max is:  "<<max<<endl;
    fout<<"Max is:  "<<max<<endl;
    curr=top;
    bool found=false;
    while(!found){
     if(curr->number==max){
        found=true;
        return curr->word;
      }//if
     else
       curr=curr->next;
     }//while

 }//findmax

 void main(){

  node list;
  string line;
  string word;
  fin.open("c:\\Cipher.txt");
  fout.open("c:\\Result.txt");
  fin>>line;

  while(fin){

//    for(int i=0;i<line.size();i++){
//     word=line[i];
 //    list.insert(top,word);
 //   }//for
  int k=1;
  int l=k-1;
 for (int i=0;i<line.size()-l;i++)
  list.insert(top,line.substr(i,k));

    fin>>line;
  }//while


  list.print(top);
  cout<<"Letter with max occurance:  "<<list.findmax(top)<<endl;
  fout<<"Letter with max occurance:  "<<list.findmax(top)<<endl;
  int freeze;

  cout<<"Enter an integer to exit:"<<endl;
  cin>>freeze;

 }//main()