Home > Back-end >  Why am I the printf (" % c ", a [I] data) output is not ah
Why am I the printf (" % c ", a [I] data) output is not ah

Time:01-19

#include
#include
#include
# define MAX_SIZE 1024
Typedef struct {
Char data;
Int weight;
Int the parent;
Int lchild;
Int rchild;
} Huffnode, * Hufftree;
Typedef struct stack {
Int top;
Int elem [MAX_SIZE];
} the Stack;
Void Initstack (Stack * s)
{
S - & gt; Top=1;
}
Void push (Stack * s, int e)
{
S - & gt; Top++;
S - & gt; Elem [s - & gt; top]=e;
}
Pop (Stack is int * s)
{
The return of s - & gt; Elem [s - & gt; top --];
}
Boolean isempty (Stack * s)
{
If (s - & gt; Top==1)
return true;
The else
return false;
}
Void GetHuffmamCode (Hufftree a, int n)
{
Int I, j, s1, s2, Max, c, p;
Stack s;
Char ch;
Initstack (& amp; s);
Printf (" input character set: ");
for(i=1; I<=n; I++)
{
The scanf (" % c ", & amp; A [I]. Data);
The scanf (" % c ", & amp; Ch);
}
Printf (" input weight: ");
for(i=1; I<=n; I++)
{
The scanf (" % d ", & amp; A [I]. Weight);
The scanf (" % c ", & amp; Ch);
}
Int m=2 * n - 1;
for(i=1; I<=n; I++)
{
[I] a parent=0;
A [I] rchild=0;
A [I] lchild=0;
}
For (I=n + 1; I<=m; I++)
{
A [I]. data=https://bbs.csdn.net/topics/NULL;
A [I]. Weight=0;
[I] a parent=0;
A [I] lchild=0;
A [I] rchild=0;
}
For (I=n + 1; I<=m; I++)
{
Max=65535;
For (j=1; J<=I - 1; J++)
{
If (a [j]. Journal of parent==0 & amp; & A [j]. Journal of weight<=Max)
{
S1=j;
Max=a [j]. Journal of weight;
}
}
A (s1). The parent=I;
Max=65535;
For (j=1; J<=I - 1; J++)
{
If (a [j]. Journal of parent==0 & amp; & A [j]. Journal of weight<=Max)
{
S2=j;
Max=a [j]. Journal of weight;
}
}
A (s2). The parent=I;
A [I]. Weight=a (s1). The weight + a (s2). The weight;
A [I] lchild=s1;
A [I] rchild=s2;
}
for(i=1; I<=n; I++)
{
C=I;
P=a [I] the parent;
While (p!=0)
{
If (a [p]. Lchild==c)
Push (& amp; S, 0);
The else
Push (& amp; S, 1);
C=p;
P=a [p]. The parent;
}
Printf (" % c \ n ", a [I] data);
while(! Isempty (& amp; S))
Printf (" % d ", pop (& amp; S));
printf("\n");
}
}
Int main ()
{
Hufftree a;
int n;
int m;
Printf (" input character number: ");
The scanf (" % d ", & amp; n);
M=2 * n - 1;
A=(Hufftree) malloc (sizeof (Huffnode) * m);
GetHuffmamCode (a, n);
return 0;
}

CodePudding user response:

Too long didn't see
Your a, array subscript should from 0 to the m - 1, but do you handle is from 1 to m, cause memory of crossing the line?

CodePudding user response:

Printf (" % c \ n ", a [I] data); Just print a line in front of the string, look at the program go to the output statements

CodePudding user response:

For input % c, need to pay attention to the last '\ n' input may be received,

CodePudding user response:

 # include 
#include
#include
# define MAX_SIZE 1024
Typedef struct {
Char data;
Int weight;
Int the parent;
Int lchild;
Int rchild;
}
Huffnode, * Hufftree;
Typedef struct stack {
Int top;
Int elem [MAX_SIZE];
}
The Stack;
Void Initstack (Stack * s) {
S - & gt; Top=1;
}
Void push (Stack * s, int e) {
S - & gt; Top++;
S - & gt; Elem [s - & gt; top]=e;
}
Pop (Stack is int * s) {
The return of s - & gt; Elem [s - & gt; top --];
}
Boolean isempty (Stack * s) {
If (s - & gt; Top==1)
return true;
The else
return false;
}
Void GetHuffmamCode (Hufftree a, int n) {
Int I, j, s1, s2, Max, c, p;
Stack s;
Char ch;
Initstack (& amp; s);
Printf (" input character set: "); The fflush (stdout);
The rewind (stdin);
for(i=1; I<=n; I++) {
The scanf (" % c ", & amp; A [I]. Data);
}
for(i=1; I<=n; I++) {
Printf (" a [% d]. Data: % c \ n ", I, a [I] data);
}
printf("\n");
Printf (" input weight: "); The fflush (stdout);
The rewind (stdin);
for(i=1; I<=n; I++) {
The scanf (" % d ", & amp; A [I]. Weight);
}
for(i=1; I<=n; I++) {
Printf (" a [% d]. Weight: % d \ n ", I, a [I] weight);
}
printf("\n");
Int m=2 * n - 1;
for(i=1; I<=n; I++) {
[I] a parent=0;
A [I] rchild=0;
A [I] lchild=0;
}
For (I=n + 1; I<=m; I++) {
A [I]. data=https://bbs.csdn.net/topics/NULL;
A [I]. Weight=0;
[I] a parent=0;
A [I] lchild=0;
A [I] rchild=0;
}
For (I=n + 1; I<=m; I++) {
Max=65535;
For (j=1; J<=I - 1; J++) {
If (a [j]. Journal of parent==0 & amp; & A [j]. Journal of weight<=Max) {
S1=j;
Max=a [j]. Journal of weight;
}
}
A (s1). The parent=I;
Max=65535;
For (j=1; J<=I - 1; J++) {
If (a [j]. Journal of parent==0 & amp; & A [j]. Journal of weight<=Max) {
S2=j;
Max=a [j]. Journal of weight;
}
}
A (s2). The parent=I;
A [I]. Weight=a (s1). The weight + a (s2). The weight;
A [I] lchild=s1;
A [I] rchild=s2;
}
for(i=1; I<=n; I++) {
C=I;
P=a [I] the parent;
While (p!=0) {
If (a [p]. Lchild==c)
Push (& amp; S, 0);
The else
Push (& amp; S, 1);
C=p;
P=a [p]. The parent;
}
Printf (" I: % d \ n ", I);
Printf (" % c \ n ", a [I] data);
while(! Isempty (& amp; S))
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related