Trying to Implement a Self-Organizing List Using the Transpose Method

122 views Asked by At

I'm trying to implement a self-organizing list using the transpose method, but I'm struggling to get the correct logic.

I'm reading in some words from a textfile and reorganizing them into pairs. So like in a sentence, the first word gets paired with the second word and then the second word becomes the first word for the next pair. After this gets set up, I want to try and search through the list of pairs and use the transpose method for self-organizing lists to bring the most frequent pairs to the front. This is the implementation of the word-pairing:

ifstream textfile; 
textfile.open("thecat.txt"); 
string word, first, second; 
LinkedList *list = new LinkedList(); 
int flag = 0; 

while(!textfile.eof()){

  getline(textfile, line); 
  //cout << line << endl; 

  //remove punctuation, remove if from algorithm library 
  line.erase(remove_if(line.begin(), line.end(), ::ispunct), line.end()); 

  //make everything lowercase 
  transform(line.begin(), line.end(), line.begin(), ::tolower); 
  //cout << line << endl; 

  //break line into words 
  istringstream iss(line);


  while(iss >> word){

     // cout << word << endl; 

      if(!isStopWord(word, stopwords)){

          if(flag == 0){
  
            first = word; 
            //cout << first; 
          }else{

            
            second = word; 
            
            //create a new node with first and second word for search 
           
            
             Node *current = new Node(first, second); 
            
             list->insert(current);  
            
            first = word; //assign next word as first word for next node 

            
          }
  
          flag = 1;              
        
      } 
   
  }

 list->print();

}


textfile.close();

Here is my transpose method:

bool transpose(Node *newNode){

//pointer for current node
 Node *current = head; 
  //pointer for previous node
 Node *prev = NULL; 
 //pointer for node before previous node
Node *prev_prev = NULL; 

//while there are nodes left to search through 
while(current != NULL){

//If the current node is equivalent to the new node being read in 
if(current->first == newNode->first && current->second == newNode->second){

  //if the node two behind the current node is not empty 
  if(prev_prev != NULL){

    //re-arrange elements
    prev_prev->next = current; 
    prev->next = current->next; 
    current->next = prev; 
  }else if(prev != NULL){

    prev->next = current->next; 
    current->next = prev; 
    head = current; 
}
    return true; 
}

  prev_prev = prev; 
  prev = current; 
  current = current->next; 
}

return false; 
 }

I was trying to do something like if transpose returns false, then insert the searched node to the end of the list, but I think my logic is wrong somewhere. When I try to include this function, one of three things happen depending on where I place it:

Innermost while loop, under the first insert statement: List cuts short after like 5 insertions Outer while loop, before flag = 1: Every so often a repeated first and second word appears Outmost while loop, above or below list->print(): Nothing.

I'm struggling to understand the logic behind this.

0

There are 0 answers