#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 (&SS); printf ("\n2-pop&print\n"); Printstack (&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