This article is for the data structure Basic Series Network course (3): Stack and queue implementation project.
The "project-number Conversion" Converts a decimal integer to any of the binary outputs. Please use the stack design algorithm and implement the program.
Tip: To convert to R, the original number is divided by the cardinality R (after the end of the business), until the quotient is 0, the inverse of a series of remainder is the conversion result. The "reverse order" here means that the remainder will be output first, LIFO, and the stack of opportunities ...
[Reference Solution]
Solution: Header file sqstack.h see [Sequential Stack Algorithm library], using a chain stack is also possible.
#include <stdio.h>#include "sqstack.h"voidMultibaseoutput (intNumberint Base){//Assume number is a non-negative decimal integer that outputs the base binary of the equivalent intI Sqstack *s; Initstack (S); while(number)//Right-to-left generation of the base binary numbers, and put it into the stack{Push (s,number%Base);//The remainder into the stackNumber/=Base; } while(! Stackempty (S))//stack non-empty time back stack output{Pop (S, i); printf"%d", i); }}intMain () {Multibaseoutput (Ten,2);return 0;}
Note: In order to achieve this project descurainiae, change the elemtype in sqstack.h from char to int, i.e.
typedefchar ElemType;
Switch
typedefint ElemType;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure Practice--numeral conversion (stack)