Data structure Practice--numeral conversion (stack)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.