Data Structure-basic operations and Applications of stacks (digital conversion)

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <math. h> # define true 1 # define false 0 # define error 0 # define Infeasible-1 typedef int status; typedef int Boolean; typedef int selemtype; # define stack_init_size 10 # define stack_increment 2 struct sqstack {selemtype * base; selemtype * Top; selemtype stacksize;}; void initstack (sqstack & S) {If (! (S. base = (selemtype *) malloc (stack_init_size * sizeof (selemtype) Exit (overflow); // storage allocation failed S. top = S. base; S. stacksize = stack_init_size;} void destroystack (sqstack & S) {free (S. base); S. base = NULL; S. top = NULL; S. stacksize = 0;} void clearstack (sqstack & S) {S. top = S. base;} status stackempty (sqstack s) {If (S. top = S. base) return true; else return false;} int stacklength (sqstack s) {return S. top-S.base;} status Gettop (sqstack S, selemtype & E) {If (S. top> S. base) {e = * (S. top-1); Return OK;} else return error;} void push (sqstack & S, selemtype e) {If (S. top-S.base> = S. stacksize) {S. base = (selemtype *) realloc (S. base, (S. stacksize + stack_increment) * sizeof (selemtype); If (! S. base) Exit (overflow); S. top = S. base + S. stacksize; S. stacksize + = stack_increment;} * (S. top) ++ = E;} status POP (sqstack & S, selemtype & E) {If (S. top = S. base) Return Error; E = * -- S. top; Return OK;} void stacktraverse (sqstack S, void (* visit) (selemtype )) {// call the visit () while (S. top> S. base) visit (* s. base ++); printf ("\ n");} void print (selemtype c) {printf ("% d", c);} void main () {Int J; sqstack s; se Lemtype E; initstack (s); For (j = 1; j <= 12; j ++) Push (S, J); printf ("the elements in the stack are: "); stacktraverse (S, print); POP (S, e); printf (" pop-up top stack Element E = % d \ n ", e ); printf ("Stack null No: % d (1: NULL 0: No) \ n", stackempty (s); gettop (S, e ); printf ("Stack top Element E = % d stack length: % d \ n", E, stacklength (s); clearstack (s); printf ("after clearing the stack, stack empty no: % d (1: NULL 0: No) \ n ", stackempty (s); destroystack (s); printf (" after the stack is destroyed, S. top = % u s. base = % u s. stacksize = % d \ n ", S. top, S. base, S. stacksize );/ /Value conversion printf ("\ n ************************ number conversion ****** * ********************** \ n "); sqstack num; initstack (Num); int N; printf ("Enter N: \ n"); scanf ("% d", & N); While (N) {push (Num, N % 8); n = N/8;} while (! Stackempty (Num) {Pop (Num, e); printf ("% d", e );}}
Printf ("\ n ");

Data Structure-basic operations and Applications of stacks (digital conversion)

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.