Realization of C language stack and the conversion of binary to octal using stack

Source: Internet
Author: User

Small dishes recently Review data structure ~ ~

It's a simple thing to knock on.


That's it, PO code.


#include <stdio.h> #include <stdlib.h> #include <math.h> #define STACK_INIT_SIZE #define STACK_INCR

easement typedef char Elemtype;
    typedef struct{ELEMTYPE *base;
    Elemtype *top;
int stacksize;

}sqstack;
    void Initstack (Sqstack *s) {s->base= (Elemtype *) malloc (stack_init_size*sizeof (elemtype));
    if (!s->base) exit (0);
    s->top=s->base;
s->stacksize=stack_init_size;

int Stacklen (Sqstack s) {return (s.top-s.base);} void push (Sqstack *s,elemtype e) {if (s->top-s->base>=s->stacksize) {s->base= (Elemtype *) realloc
        (S->base, (stack_increasement+s->stacksize) *sizeof (elemtype));
    if (!s->base) exit (0);
    } * (S->top) =e;
s->top++;
    } void Pop (Sqstack *s,elemtype *e) {if (s->top==s->base) {return;
    } s->top--;
*e=* (S->top);

int IsEmpty (Sqstack *s) {return s->top==s->base;}
    int main () {Sqstack s,t; EleMtype C;
    Initstack (&s);

    Initstack (&t);
    scanf ("%c", &c);
        while (c!= ' # ') {//When input # input ends with characters read into push (&AMP;S,C);
    scanf ("%c", &c);
    } getchar ();
    int sum;
    int i,j;
    int Slen=stacklen (s);
        for (i=0;i<slen;i+=3) {sum=0;
                for (j=0;j<3;j++) {if (!isempty (&s)) {pop (&s,&c);
	    sum+= (c-48) * (int) pow (2,j);
    } push (&AMP;T,SUM+48);
    int Tlen=stacklen (t);
        for (i=0;i<tlen;i++) {pop (&t,&c);
    printf ("%c", c);
return 0;
 }


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.