A simple C stack implementation, stack implementation
A simple stack is implemented in C language. The basic idea is to define a stack structure with two pointers and an int representing the stack size. The two pointers point to the bottom and top of the stack respectively. When the bottom pointer and top pointer of the stack are repeated, the stack is empty; when the value of the top pointer minus the bottom pointer of the stack is greater than or equal to the size of the stack, the stack is full.
// Mystack. h # ifndef mystack_H # define mystack_H # include <stdio. h> # include <stdlib. h> # define MYSTACK_INCREASE_NUM 2 typedef int ElementType; // tentatively set to inttypedef int Bool; typedef struct {ElementType * bottom; // The bottom pointer of the stack: ElementType * top; // stack top pointer int size; // stack size} MyStack; MyStack * initStack (int size); // initialize Bool freeStack (MyStack * stack ); // release the memory Bool push (MyStack * stack, ElementType data); Bool pop (MyStack * stack, ElementType * outputData); Bool isEmpty (MyStack * stack ); void makeStackEmpty (MyStack * stack); void printStack (MyStack * stack); # endif
// Mystack. C # include "mystack. h "MyStack * initStack (int size) {MyStack * stack = (MyStack *) malloc (sizeof (MyStack); // allocate memory if (stack = NULL) return NULL; stack-> bottom = (ElementType *) malloc (sizeof (ElementType) * size); if (stack-> bottom = NULL) return NULL; stack-> top = stack-> bottom; // The top pointer and bottom pointer of the empty stack point to the same position as the stack-> size = size; // initialization size return stack;} Bool freeStack (MyStack * stack) {if (stack! = NULL) {free (stack-> bottom); free (stack); return 1 ;}else {return 0 ;}} void makeStackEmpty (MyStack * stack) {if (stack! = NULL) stack-> top = stack-> bottom;} Bool isEmpty (MyStack * stack) {if (stack! = NULL) {if (stack-> bottom = stack-> top) return 1; else return 0;} else return 0;} Bool push (MyStack * stack, ElementType data) {if (stack-> top-stack-> bottom> = stack-> size) // stack full {stack-> bottom = (ElementType *) realloc (stack-> bottom, // allocate memory, increase the stack size sizeof (ElementType *) * (stack-> size + MYSTACK_INCREASE_NUM); if (stack-> bottom = NULL) return 0; stack> top = stack> bottom + stack> size; // initialize the stac K-> size + = MYSTACK_INCREASE_NUM;} * (stack-> top) = data; // first save the value stack-> top ++; // Add 1 to the pointer, pointing to the next memory unit return 1;} Bool pop (MyStack * stack, ElementType * outputData) {if (isEmpty (stack) {printf ("stack is empty \ n "); return 0;} stack-> top --; // the pointer minus 1 and points to the top element in the stack * outputData = * (stack-> top); // The value is return 1 ;} void printStack (MyStack * stack) {if (stack! = NULL) {if (stack-> bottom = stack-> top) {printf ("stack is empty \ n ");} else {/* for (int I = 0; I <stack-> top-stack-> bottom; I ++) printf ("% d \ n ", stack-> bottom [I]); */ElementType * element = stack-> bottom; for (; element! = Stack-> top; element ++) printf ("% d \ n", * element );}}}
// Stacktest. c # include "mystack. h "# include <stdio. h> int main (int argc, char const * argv []) {/* code */int a, B, c; MyStack * stack = initStack (2 ); push (stack, 1); push (stack, 2); printStack (stack); // expected output 1, 2 push (stack, 3); printStack (stack ); // expected output 1, 2, and 3 pop (stack, & a); pop (stack, & B); push (stack, 4); pop (stack, & c ); printf ("a = % d B = % d c = % d \ n", a, B, c ); // expected output a = 3 B = 2 c = 4 makeStackEmpty (stack); pop (stack, & ); // expected output stack is empty printf ("a = % d \ n", a); // expected output a = 3 freeStack (stack); return 0 ;}
Reference: Baidu Library
Http://blog.csdn.net/mci2004/article/details/7532205