my code stock.com

Array and linked list storage

Snippet options

Download: Download snippet as assignment2.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstring>
#include<cstdlib>
#include <algorithm>
using namespace std;
struct wordItem{
  string word;
  int count;
};
struct Node{
  wordItem WordItem;
  Node *next;
  
};
class LinkedList {  
public:
  Node *head = new Node();
  // Node *tail= NULL;
    
  LinkedList(){};

  LinkedList( wordItem key) {
    head=NULL;
    head->WordItem = key;
    head->next = NULL;
  }
    
};
Node * searchNode(LinkedList *linkedList, string value){
 
  Node *tmp = linkedList->head;
  while(tmp != NULL)
    {
      if (tmp->WordItem.word == value)
	{
	  return tmp;
	}
      tmp = tmp->next;
    }
  return NULL;
}
  
void getStopWords(char *ignoreWordFileName, string ignoreWords[]){
  ifstream myfile2;
  string data;
  int tmp=0;
  myfile2.open(ignoreWordFileName); 
  if(myfile2.is_open()){
    while(myfile2>>data){
      ignoreWords[tmp]=data;      
      tmp++;
    } 
    myfile2.close();
  }
  else cout<<"error"<<endl;
  return;
}
bool isStopWord(string word, string ignoreWords[]){
  for(int i=0;i<50;i++){
    if(word==ignoreWords[i])   return true;
  }
  
  return false;
}
bool uniWord(string words, wordItem* &array,int n){
  for(int i=0;i<n;i++){
    if(array[i].word==words) return true;
    // else return false;
  }
  return false;
}
int getTotalNumberNonStopWords(wordItem list[],int length){
  return length;
    
}

//  if(isStopWord(list, 
// }
/*int uniq(int *f,int count){
  int t=0;
  for(int i=0;i<count;i++){
  if(f[i]==1) t++;
  }
  return t;
  }*/
void double_arr(struct wordItem* &word, int n,int max){
  wordItem* tmp=new wordItem[max];
  for(int i=0;i<n;i++){
    tmp[i]=word[i];
  }
  delete[] word;
  word=tmp;
}
void printTopN(wordItem WordItemList[],int topN){

  for (int i = 0; i< topN; i++)
    {
      cout << WordItemList[i].count<< " - " << WordItemList[i].word << endl;
    }
}
/*Node* creatList(wordItem* array,int size){
  Node *next=NULL;
  Node *rootptr=NULL;
  Node *lastptr=NULL;
  for(int i=0; i<size;i++){
  if(!ptr){
  next=new Node(*(i));
  if(!rootptr){
  rootprt=ptr;
  }
  if(lastptr) last->next=next;
  }
  lastptr=next;
  next=next->next;
  }
  return rootptr;
  
  }*/
void arraySort (wordItem array[], int numUniqueWords){
  int i, j, k = 1;
  wordItem tem;
  int numLength = numUniqueWords;
  for (i = 1; (i <= numLength) && k; i++)
    {
      k = 0;
      for (j=0; j < (numLength-1); j++)
        {
	  if (array[j+1].count>array[j].count)
            {
	      tem = array[j];
	      array[j] = array[j+1];
	      array[j+1]=tem;
	      k = 1;
            }
        }
    }
  return;
}

void linkedlist(char * file, string words[]){
  LinkedList *linkedList = new LinkedList();
  // int countNonStop++;
  string line;
  ifstream myfile1;
  myfile1.open(file);
  if(myfile1.fail()){
    cout << "failed to open 2" << endl;
  }
    
  while(myfile1>>line){
    if((isStopWord(line,words)==false)&&(linkedList->head==NULL)){
      //seg fault here
      linkedList->head->WordItem.word=line;
      linkedList->head->WordItem.count=1;
      linkedList->head->next = NULL;
     
    }
    else{
      //cout<<"else"<<endl;
      Node *tmpNode=searchNode(linkedList,line);
      if((isStopWord(line,words)==false)&&(tmpNode==NULL)){  
	Node *newNode= new Node;
	newNode->WordItem.word = line;
	newNode->WordItem.count = 1;
	newNode->next=NULL;
        linkedList->head->next = newNode;
        linkedList->head = newNode;

      }       
      else if((isStopWord(line,words)==false)&& (tmpNode!=NULL)){
	//countNonStop++;
	tmpNode->WordItem.count++;      
      }
              }   // count++;   
  }
  //cout<<linkedList<<endl;
  myfile1.close();  
}


int main(int argc, char* argv[]){
  string word2[50];
  ifstream myfile1(argv[2]);
  getStopWords(argv[3],word2);
  string line;
  int maxArraySize = 100;
  int numUniqueWords = 0;
  int numDoublings = 0;
  int countNonStop = 0;
  //int count=0;
  int numWords=atoi(argv[1]);
  wordItem *arrayUniqueWords=new wordItem[maxArraySize];
  myfile1.is_open();
  if( myfile1.fail() || argc<4){
    cout << "failed to open 1" << endl;
  }
  float time2 = clock();
  while(myfile1>>line){
    // count++;
    if((isStopWord(line,word2)==false) && (uniWord(line,arrayUniqueWords,numUniqueWords)==false)){
      countNonStop++;
      if (maxArraySize > numUniqueWords){
	arrayUniqueWords[numUniqueWords].word=line;
	arrayUniqueWords[numUniqueWords].count=1;
      }
      else{
	maxArraySize = 2*maxArraySize ;
	numDoublings++;
	double_arr(arrayUniqueWords, numUniqueWords, maxArraySize);
	arrayUniqueWords[numUniqueWords].word=line;
	//   cout<<"!!!!"<<endl;
	arrayUniqueWords[numUniqueWords].count=1;
      }
      numUniqueWords++;
    }
         
    else if((isStopWord(line,word2)==false) && (uniWord(line,arrayUniqueWords,maxArraySize)==true)){
      countNonStop++;
      for(int i=0;i<numUniqueWords;i++){
	if(line==arrayUniqueWords[i].word){
	  arrayUniqueWords[i].count++;
	}
      }
    }
  }
  // cout<<arrayUniqueWords<<endl;
  myfile1.close();
  time2 = (clock() - time2) / CLOCKS_PER_SEC;

  arraySort(arrayUniqueWords, numUniqueWords);
  printTopN(arrayUniqueWords,numWords);
  cout<<"#"<<endl;
  cout<<"Array Double: "<<numDoublings<<"," << "Time taken to store in array: "<<time2<<"s"<<endl;
  cout<<"#"<<endl;
    float time=clock();     
  linkedlist(argv[2],word2);
  time = (clock() - time) / CLOCKS_PER_SEC;
  cout<<"Time taken to store in linked list: "<<time<<endl;
  cout<<"#"<<endl;
  cout<<"Unique non-common words: "<<numUniqueWords<<endl;
  cout<<"#"<<endl;
  cout<< "Total non-common words "<<getTotalNumberNonStopWords(arrayUniqueWords ,countNonStop)<<endl;
  return 0;
}


Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.