PS: Application of 48-page data structure Stack
Decimal conversion octal
Write it by yourself. For your reference, you can further learn the data structure through the source code.
This book provides all the algorithm ideas
You have to think more about the code by yourself. You can't just read it. It's useless. You can't write your ideas as well.
To be a qualified programmer, write code. Reflect your capabilities through the amount of code
// Use the stack to implement the conversion between systems
# Include <stdio. h>
# Include <stdlib. h>
// # Include <malloc. h>
# Include <conio. h>
# Define stack_init_size 100
# Define stackincrement 10
Typedef struct {
Int * base;
Int * top;
Int stacksize;
} Sqstack;
Int initstack (sqstack & S) // construct an empty Stack
{
S. base = (int *) malloc (sizeof (INT) * (stack_init_size ));
If (! S. Base) Exit (-1 );
S. Top = S. base;
S. stacksize = stack_init_size;
Return 1;
}
Int push (sqstack & S, int e) // press the stack
{
If (S. Top-S. Base> = S. stacksize)
{
S. base = (int *) realloc (S. Base, sizeof (INT) * (stack_init_size + stackincrement ));
If (! S. Base) Exit (-1 );
S. Top = S. Base + S. stacksize;
S. stacksize + = stackincrement;
}
* S. Top ++ = E;
Return 1;
}
Int stackempty (sqstack s) // check whether the stack is empty
{
If (S. base = S. Top) return 1;
Else return 0;
}
Int POP (sqstack & S, Int & E) // output Stack
{
If (S. Top = S. Base) return 0;
E = * -- S. Top;
Return 1;
}
Void conversion () // convert decimal to octal
{
Sqstack S;
Int N;
Int E;
Initstack (s );
Printf ("input N value :");
Scanf ("% d", & N );
Printf ("converted value :");
While (N)
{
Push (S, N % 8 );
N = N/8;
}
While (! Stackempty (s ))
{
Pop (S, e );
Printf ("% d", e );
}
}
Void main ()
{
Conversion ();
Getch ();
}