c++ - Adding Too Many Nodes -
i created data bag structure, text files consist of short stories , poems. first time instance of string inserted, datavalue contains string , datacount set one. if instance of same string inserted, datacount incremented. reason keeps outputting same word on , on , of strings contain count of 1 reason. there suggestions on adjust?
struct bagnode { string datavalue; string datacopy; int datacount; bagnode * next; }; class bag{ private: bagnode * head; public: bag() { head = null; } void insert(string v) { if(head == null){ //empty list head = new bagnode; removepunct(v); head->datavalue = v; transform(v.begin(), v.end(), v.begin(), ::tolower); head->datacopy = v; head->next = null; } else { bagnode * n = new bagnode; // new node removepunct(v); n->datavalue = v; transform(v.begin(), v.end(), v.begin(), ::tolower); n->datacopy = v; bagnode * current = head; //for traversal n->datacount = 1; if(current->datacopy > v) { n->next = head; head = n; } else{ //mid , tail insert while(current->next && current->next->datacopy < v){ current = current->next; } //check identical strings if(current->datacopy != v) { n->next = current->next; current->next = n; } else { current->datacount++; } } } }
output: 10annette (1) 1805 (1) 7 (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) , (1) , (1) , (1) , (1) , (1) , (1) , (4) anna (1) anna (1) antichristi (1) antichristi (1) (1) (1) arrive (1) (1) (1) (1) @ (1) attack (1) (1) being (1) believe (1) better (1) between (1) buonapartes (1) (1) (1) (1) (1) (1) call (1)
Comments
Post a Comment