📄 structures.cpp
字号:
#include "stdafx.h"
#include "structures.h"
#include<fstream>
#include<iostream>
using namespace std;
DCDiffCount::DCDiffCount(){link=NULL;}
DCDiffCount::DCDiffCount(short int data1){
diffValue=data1;
count=0;
link=NULL;
}
void DCDiffCount::IncreCount(){++count;}
void DCDiffCount::InsertAfter(DCDiffCount *p){
p->link=link;
link=p;
}
DCDiffCount * DCDiffCount::RemoveAfter(){
DCDiffCount * tempP=link;
if(link==NULL)tempP=NULL;
else link=tempP->link;
return tempP;
}
List::List(){
head=tail=new DCDiffCount();
}
List::~List(){
MakeEmpty();
delete head;
}
void List::MakeEmpty(){
DCDiffCount *tempP;
while(head->link!=NULL){
tempP=head->link;
head->link=tempP->link;
delete tempP;
}
tail=head;
}
DCDiffCount *List::Find(short int data){
DCDiffCount *tempP=head->link;
while(tempP!=NULL&&tempP->diffValue!=data)tempP=tempP->link;
return tempP;
}
int List::Length(){
DCDiffCount *tempP=head->link;
int count1=0;
while(tempP!=NULL){
tempP=tempP->link;
count1++;
}
return count1;
}
void List::PrintList(){
DCDiffCount *tempP=head->link;
while(tempP!=NULL){
cout<<tempP->diffValue<<'\t';
cout<<tempP->count<<endl;
tempP=tempP->link;
}
cout<<endl;
}
void List::InsertRear(DCDiffCount *p){
p->link=tail->link;
tail->link=p;
tail=p;
}
DCDiffCount * List::CreatNode(short int data){
DCDiffCount *tempP=new DCDiffCount(data);
return tempP;
}
DCDiffCount * List::DeleteNode(DCDiffCount *p){
DCDiffCount *tempP=head;
while(tempP->link!=NULL&&tempP->link!=p)tempP=tempP->link;
if(tempP->link==tail)tail=tempP;
return tempP->RemoveAfter();
}
void BinaryTree::Destroy(Node* Current){
if(Current!=NULL){
Destroy(Current->lchild);
Destroy(Current->rchild);
delete Current;
}
}
void BinaryTree::CreateDC(dcCodeList* pdcCode,cltype dcCodeListlength){
for(cltype i=0;i<dcCodeListlength;i++){
unsigned short temp=unsigned short(strlen(pdcCode->code));
InsertDC(pdcCode,root,temp);
pdcCode=pdcCode->next;
}
}
void BinaryTree::CreateAC(decodeListStruct* pacCode,cltype acCodeListlength){
for(cltype i=0;i<acCodeListlength;i++){
unsigned short temp = unsigned short (strlen(pacCode->code));
InsertAC(pacCode,root, temp);
pacCode=pacCode->next;
}
}
void BinaryTree::InsertDC(dcCodeList* pdcCode,Node* &b,unsigned short codeLen){
if(b==NULL){
if(codeLen==0){ //叶节点
b=new Node(0,0,pdcCode->diffValue);
if(b==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
}
else{
b=new Node(0,0,0); //非叶节点
if(b==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
unsigned short index=unsigned short(strlen(pdcCode->code))-codeLen;
if(pdcCode->code[index]=='0')InsertDC(pdcCode,b->lchild,codeLen-1);
if(pdcCode->code[index]=='1')InsertDC(pdcCode,b->rchild,codeLen-1);
}
}
else{
unsigned short index=unsigned short(strlen(pdcCode->code))-codeLen;
if(pdcCode->code[index]=='0')InsertDC(pdcCode,b->lchild,codeLen-1);
if(pdcCode->code[index]=='1')InsertDC(pdcCode,b->rchild,codeLen-1);
}
}
void BinaryTree::InsertAC(decodeListStruct* pacCode,Node* &b,unsigned short codeLen){
if(b==NULL){
if(codeLen==0){ //叶节点
b=new Node(pacCode->ep.run,pacCode->ep.level,0);
if(b==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
}
else{ //非叶节点
b=new Node(0,0,0);
if(b==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
int index=unsigned short(strlen(pacCode->code))-codeLen;
if(pacCode->code[index]=='0')InsertAC(pacCode,b->lchild,codeLen-1);
if(pacCode->code[index]=='1')InsertAC(pacCode,b->rchild,codeLen-1);
}
}
else{
unsigned short index=unsigned short(strlen(pacCode->code))-codeLen;
if(pacCode->code[index]=='0')InsertAC(pacCode,b->lchild,codeLen-1);
if(pacCode->code[index]=='1')InsertAC(pacCode,b->rchild,codeLen-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -