I see a runtime error wz message "Unhandled exception at 0x009f5900 in trial4.exe: 0xC0000005: Access violation reading location 0xcdcdcdd5." each time I run the following code.
// trial4.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define NULL 0
struct node{
char letter;
int frequency;
node* next;
};
struct treeq{
char letter;
int frequency;
treeq* leftch;
treeq* rightch;
treeq* parent;
char code;
treeq* next;
};
struct lut{
string code;
char letter;
lut* next;};
int freCount(string c, node* start)
{
node* ptr=start;
ptr->letter=NULL;
ptr->frequency=0;
ptr->next=NULL;
node* temp=ptr;
node* ptr1;
int i=0,k=0,len=0;
bool found=false;
const int leng=c.length();
while(i<leng)
{
k=0;
ptr1=temp;
while(k<=i&&ptr1!=ptr)
{
if(ptr1->letter==c[i])
{
ptr1->frequency++;
found=true;
break;
}
ptr1=ptr1->next;
k++;
}
if(!found)
{ ptr->letter=c[i];
ptr->frequency=1;
ptr->next=new node;
ptr=ptr->next;
len++;
}
i++;
found=false;
}
return len;
}
void enque(treeq* item, treeq* start){
treeq* ptrt=start;
if(ptrt!=NULL){
while((ptrt->next!=NULL))
{if(ptrt->next->frequency>item->frequency)
break;
ptrt=ptrt->next;}}
treeq* temp=new treeq;
temp->letter=item->letter;
//temp->next=NULL;
temp->frequency=item->frequency;
if(ptrt==NULL)
ptrt=temp;
else
{temp->next=ptrt->next;
ptrt->next=temp;}
}
treeq* deque(treeq* head){
treeq* item;
item=head;
head=head->next;
return item;}
treeq* add(treeq* item1, treeq* item2,treeq* parent){
parent->frequency=item1->frequency+item2->frequency;
item1->code='0';
item2->code='1';
item1->parent=parent;
item2->parent=parent;
parent->leftch=item1;
parent->rightch=item2;
return parent;}
treeq* huffman(treeq* start){
treeq* parent;
while(start!=NULL){
parent=new treeq;
treeq* item1=new treeq;
treeq* item2=new treeq;
item1->frequency=start->frequency;
item1->letter=start->letter;
start=start->next;
item2->frequency=start->frequency;
item2->letter=start->letter;
start=start->next;
//cout<<item2->letter<<"-> "<<item2->frequency<<endl;
parent=add(item1, item2,parent);
enque(parent, start);
cout<<parent->letter<<"->"<<parent->frequency<<endl;
}
return parent;
}
lut* inorder(treeq* root,string code){
lut* start=new lut;
lut* ptr=start;
if(root->leftch!=NULL){
inorder(root->leftch,code);
code+=root->leftch->code;
cout<<endl<<code;
}
else
{lut* k=new lut;
k->code=code;
k->letter=root->letter;
ptr->next=k;
ptr=ptr->next;
if(root->parent->rightch!=NULL){
code="";
inorder(root->parent->rightch,code);
}
}
return start;}
int main () {
int x;
ifstream myfile ("C:\\Users\\Sara\\Desktop\\trial.txt");
string copy;
char c;
while(myfile.get(c))
copy+=c;
myfile.close();
cout<<copy<<endl;
node* start=new node;
int len=freCount(copy, start);
node* ptr=start;
int i=0;
while(i<len)
{cout<<ptr->letter<<" "<<ptr->frequency<<endl;
ptr=ptr->next;
i++;}
treeq* star=new treeq;
star->frequency=0;
star->letter=NULL;
star->next=NULL;
treeq* item=NULL;
treeq* ptrt;
ptr=start;
ptrt=star;
treeq* ptrt1=star;
/*ptrt1->leftch=NULL;
ptrt1->rightch=NULL;*/
i=0;
while(i<len)
{ptrt1->frequency=ptr->frequency;
ptrt1->letter=ptr->letter;
enque(ptrt1,star);
ptr=ptr->next;
i++;}
ptrt=star->next;
i=0;
while(ptrt!=NULL)
{cout<<ptrt->letter<<" "<<ptrt->frequency<<endl;
ptrt=ptrt->next;
i++;}
star=star->next;
treeq* root=huffman(star);
string code="";
lut* begin=inorder(root,code);
cin>>x;
return 0;
}