1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
//class definition
#include "concordance.h"
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
concordance::concordance()
{
first = NULL;
}
concordance::~concordance()
{
Node *temp;
while(first != NULL)
{
temp = first;
first = first -> next;
delete temp;
}
}
void concordance::insert(Word& word, int& count)
{
Node *prev;
if(first == NULL || strcmp(first -> wd, word) > 0)
first = get_node(word, count, first);
else
{
prev = first;
while(prev -> next != NULL && strcmp(prev -> next -> wd, word) > 0)
prev = prev -> next;
prev -> next = get_node(word, count, prev -> next);
}
}
void concordance::remove(Word& word)
{
Node *prev, *temp;
prev = temp;
if(prev -> wd == word)
{
first = first -> next;
delete prev;
}
else
{
while(strcmp(prev -> next -> wd, word) > 0)
prev = prev -> next;
temp = prev -> next;
prev -> next = temp -> next;
delete temp;
}
}
int concordance::get_count(Word& word)
{
while(strcmp(first -> wd, word) != 0)
first = first -> next;
return first -> count;
}
int concordance::length() const
{
Node *cursor;
int len;
len = 0;
for(cursor = first; cursor != NULL; cursor = cursor -> next )
len++;
return len;
}
concordance::Node* concordance::get_node (Word& word, int& count, Node* link)
{
Node *temp;
temp = new Node;
strcpy(temp-> wd, word);
temp-> next = link;
temp -> count = count+1;
return temp;
}
ostream& operator << (ostream& out_s, concordance& c)
{
concordance::Node *cursor;
out_s << "Word" << setw(10) << " " << "Count" << endl;
out_s << "--------------------" << endl;
for(cursor = c.first; cursor != NULL && cursor->next != NULL; cursor = cursor-> next )
out_s << cursor-> wd << setw(10) << " " << cursor -> count << endl;
if(cursor != NULL)
out_s << cursor-> wd << setw(10) << " " << cursor -> count << endl;
out_s << "--------------------" << endl;
// out_s << "The file contains " << c.length << " distinct words. ";
return out_s;
}
|