# Include <stdio. h> # include <string. h> # include <stdlib. h> # define maxsize 8 typedef int datatype; typedef struct stack {datatype date [maxsize]; int top ;}stack; void initstack (stack * s) // initialize the stack function {S-> Top =-1; // initialize top pointing to the bottom memset of the stack (S-> date, 0, sizeof (datatype) * maxsize ); // initialize the stack space to 0 (this step can be omitted)} void push (stack * s, int e) {If (S-> Top = MAXSIZE-1) {return ;} s-> top ++; s-> date [S-> top] = E;} void POP (stack * s, int * E) {If (S-> Top =-1) {return;} * E = s-> date [S-> top]; // obtain the top-level data S-> top --;} int main (void) {stack mytest; int temp; initstack (& mytest ); printf ("Push order: \ n"); For (INT I = 0; I <8; I ++) {printf ("% d", I ); push (& mytest, I) ;}printf ("\ n"); printf ("Pop order: \ n"); For (INT I = 0; I <8; I ++) {Pop (& mytest, & temp); printf ("% d", temp);} printf ("\ n"); Return 0 ;}
This article from the "Network Learning summary" blog, please be sure to keep this source http://8947509.blog.51cto.com/8937509/1550922
C array simulation Stack