Stack of array pointer storage form

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define ERROR 0#define OK 1#define stack_int _size 10/* Storage space initial allocation */typedef int elemtype;    /* Define the type of element */typedef struct{Elemtype *base; Elemtype *top;}   Sqstack;int Initstack (Sqstack *s); /* Construct empty stack */int push (Sqstack *s,elemtype e);  /* Enter stack */int Pop (sqstack *s,elemtype *e);     /* Out Stack */int createstack (sqstack *s);   /* Create stack */void printstack (sqstack *s);    /* out stack and output stack elements */int initstack (Sqstack *s) {s->base= (Elemtype *) malloc (stack_int_size *sizeof (elemtype)); if (!    S->base) return ERROR;    s->top=s->base; return OK;} /*initstack*/int Push (Sqstack *s,elemtype e) {if (s->top-s->base==stack_int_size)//Judgment stack full return error;* (S->t OP) =e;//s->top=&e is wrong S->top++;return OK;} /*push*/int Pop (sqstack *s,elemtype *e) {if (s->base==s->top) return error;*e=* (s->top-1);//e=s-> Why is top-1 wrong? S->top--;return OK;    }/*pop*/int Createstack (Sqstack *s) {int E; if (Initstack (S)) printf ("Init success!\n ");        else{printf ("Init fail!\n");    return ERROR;    } printf ("Input data: (Terminated by inputing a character) \ n");    while (scanf ("%d", &e)) Push (s,e); return OK;}    /*createstack*/void Printstack (Sqstack *s) {elemtype e; while (Pop (s,&e)) printf ("%3d", e);}    /*pop_and_print*/int Main () {Sqstack ss;    printf ("\n1-createstack\n");    Createstack (&AMP;SS);    printf ("\n2-pop&print\n");    Printstack (&AMP;SS);p rintf ("\ n");    return 0;}

about the e=s->top-1 in the stack, theoretically e holds the s->top-1 address, and the address holds the value stored s->top-1, so e should also point to that value,
At the same time, although the s->top--out of the stack, but the original value is deleted, if the stack is used to store the array rather than the array pointer?




Stack of array pointer storage form

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.