1. 程式人生 > >求助!結構體的二級指標陣列給一級指標初始化遇見的異常

求助!結構體的二級指標陣列給一級指標初始化遇見的異常

百度也看了很多部落格都沒解決

主要程式碼如下

typedef struct HTNode {
int weight;
char c;//存這個字元,單個字元,符號都是葉子節點
int code;
HTNode *lchild, rchild;
}HuffmanTree;
/

資料字典,順序線性表
*/
typedef struct {
int *weight;
char c;//字元陣列
int length;//儲存資料的長度
int listsize;
HuffmanTree t;//二維指標,存放指標的指標陣列,因為節點是指標,所以需要存放指標的陣列
}DATA;
DATA D;
HuffmanTree T = (HuffmanTree)malloc(sizeof(HTNode));//根
/


字典初始化
*/
void InitList(DATA &D) {
D.weight = (int *)malloc(DEFAULTSIZE * sizeof(int));
D.c = (char *)malloc(DEFAULTSIZE * sizeof(char));
D.t = (HuffmanTree *)malloc(DEFAULTSIZE * sizeof(HuffmanTree));
if (!D.weight || !D.c || !D.t)
return;
D.length = 0;
D.listsize = DEFAULTSIZE;
D.t = null;
for (int i = 0; i < D.listsize; i++) {
D.t[i] = (HuffmanTree)malloc(sizeof(HTNode));
//[^2]錯誤在這,引發了異常: 寫入訪問許可權衝突。
D.t 是 0x1110112。
D.t[i]->lchild = NULL;
D.t[i]->rchild = NULL;
D.t[i]->weight = 0;
D.t[i]->c = NULL;
}
}

還在除錯的原始碼,不確定還有沒有其他錯誤

#include
#include
#define null NULL
#define DEFAULTSIZE 50
#define ADDSIZE 10
using namespace std;
typedef struct HTNode {
int weight;
char c;//存這個字元,單個字元,符號都是葉子節點
int code;
HTNode *lchild, rchild;
}HuffmanTree;
/


資料字典,順序線性表
*/
typedef struct {
int *weight;
char c;//字元陣列
int length;//儲存資料的長度
int listsize;
HuffmanTree t;//二維指標,存放指標的指標陣列,因為節點是指標,所以需要存放指標的陣列
}DATA;
DATA D;
HuffmanTree T = (HuffmanTree)malloc(sizeof(HTNode));//根
/

字典初始化
*/
void InitList(DATA &D) {
D.weight = (int *)malloc(DEFAULTSIZE * sizeof(int));
D.c = (char *)malloc(DEFAULTSIZE * sizeof(char));
D.t = (HuffmanTree *)malloc(DEFAULTSIZE * sizeof(HuffmanTree));
if (!D.weight || !D.c || !D.t)
return;
D.length = 0;
D.listsize = DEFAULTSIZE;
D.t = null;
for (int i = 0; i < D.listsize; i++) {
D.t[i] = (HuffmanTree)malloc(sizeof(HTNode));
D.t[i]->lchild = NULL;
D.t[i]->rchild = NULL;
D.t[i]->weight = 0;
D.t[i]->c = NULL;
}
}
void deleteChar(DATA &D) {
D.c[D.length - 1] = null;
D.weight[D.length - 1] = 0;
D.c[D.length - 2] = null;
D.weight[D.length - 2] = 0;
D.length -= 2;
}
void Initdata(DATA &D,char c) {
if (D.length + 1 >= D.listsize) {
D.weight = (int *)realloc(D.weight, (D.listsize + ADDSIZE) * sizeof(int));
D.c = (char *)realloc(D.c, (D.listsize + ADDSIZE) * sizeof(char));
D.t = (HuffmanTree *)realloc(D.t, (D.listsize + ADDSIZE) * sizeof(HTNode *));
D.listsize += ADDSIZE;

}
D.weight[D.length] = 1;//初始化為1
D.c[D.length] = c;
D.length++;
//order(D);//重新 排序

}
/**
根據weight排序,對應位置的字元也要改變位置,大的放前面
/
void order(DATA &D) {
for (int j = D.length; j > 0; j–) {
int min = 0;
HuffmanTree a;
for (int i = 0; i < j; i++) {
if (D.weight[i] < D.weight[min]) {
D.weight[i] += D.weight[min];
D.weight[min] = D.weight[i] - D.weight[min];
D.weight[i] = D.weight[i] - D.weight[min];
D.c[i] += D.c[min];
D.c[min] = D.c[i] - D.c[min];
D.c[i] = D.c[i] - D.c[min];
a = D.t[i];
D.t[i] = D.t[min];
D.t[min] = a;
min = i;
}
}
}
}

/
*
兩個最小的構造樹,權重之和還有參加排序,根節點
*/
void addNode(DATA &D, int w) {
if (D.length + 1 >= D.listsize) {
D.weight = (int *)realloc(D.weight, (D.listsize + ADDSIZE) * sizeof(int));
D.c = (char *)realloc(D.c, (D.listsize + ADDSIZE) * sizeof(char));
D.t = (HuffmanTree )realloc(D.t, (D.listsize + ADDSIZE) * sizeof(HTNode ));
D.listsize += ADDSIZE;
}
D.weight[D.length] = w;
D.c[D.length] = NULL;
D.length++;
order(D);
}
/

計算出現的概率
*/
int check(DATA D, char ch) {//不是指標可以直接用==判斷
for (int i = 0;i <= D.length;i++) {
if (D.c[i] == ch)
return i;
}
return -1;
}

int countWeight(istream& input,DATA &D) {//檔案中的字元出現個數
char ch;
while (input.get(ch)) {
//遍歷字典,有則加1,沒有則建立
int j = check(D, ch);
if (j != -1) {
D.weight[j]++;
}
else {
Initdata(D, ch);
}
}
return D.length;//字元種類數(葉子節點數)
}
void InitLink(HuffmanTree &N,int w,char ch) {//對節點初始化
if (N) {
N->weight = w;
N->c = ch;
}
}

int createTree(DATA &D,HuffmanTree &T) {//構造赫夫曼樹,用遞迴或者棧?迴圈,根接點地址賦值給T無論怎麼變最終都會到達根
int newadd = 0;//記錄非葉子節點的數
while (D.weight[0] != 0) {
int newweight = D.weight[D.length - 1] + D.weight[D.length - 2];
addNode(D, newweight);
newadd++;
HuffmanTree H = (HuffmanTree)malloc(sizeof(HTNode));
H = D.t[D.length];
InitLink(D.t[D.length],newweight,’ ');
T = D.t[D.length];
D.t[D.length]->lchild =D.t[D.length - 1];
D.t[D.length - 1]->code = 0;//左枝為0
//D.t[D.length - 1]->flag = true;
D.t[D.length]->rchild = D.t[D.length - 2];
D.t[D.length - 1]->code = 1;//右1
//D.t[D.length - 1]->flag = true;
InitLink(D.t[D.length - 1],D.weight[D.length - 1],D.c[D.length - 1]);
InitLink(D.t[D.length - 2],D.weight[D.length - 2], D.c[D.length - 2]);
deleteChar(D);
}
return newadd;
}
//bool visit(ostream& output, HuffmanTree T) {
// if (!T->lchild && !T->rchild) {
// output << “符號:” << T->c << “編碼:”;
// return true;
// }
// return true;
//}
void showdata(DATA D,int a) {
ofstream output(“result.txt”);
for (int i = 0; i < a; i++) {
output << “符號:” << D.c[i] << “權重:” << D.weight[i];
}
}
void HuffCode(HuffmanTree T,int len,DATA D) {
ofstream output(“result.txt”);
if (T) {
output << “編碼:”;
if (T->lchild) {
output << 0;
if (!T->lchild->lchild)
output << “符號:” << T->lchild->c << “\n”;
HuffCode(T->lchild,len,D);
}
if (T->rchild) {
output << 1;
if (!T->rchild->rchild)
output << “符號:” << T->rchild->c << “\n”;
HuffCode(T->rchild, len, D);
}
}
}
int main()
{
InitList(D);
const char* fileName = “G:\資料結構C語言\赫夫曼樹對文字檔案編碼與譯碼\test.txt”;
fstream obIn(fileName);
if (obIn.is_open()) {
int b = countWeight(obIn, D);
order(D);
int a = createTree(D, T);
int c = a + b;
showdata(D, b);
//HuffCode(T, c, D);
}
else
cout << “open " << fileName << " is fail!” << endl;
return 0;
}