/ * Name: stack.h Copyright: 1.0 Author: avalon date: 02-10-04 19:48 Description: Pan design of the stack * / # ifndef avalon_stack_h # deflude
#ifndef avalon_bool # Define avalon_bool # define true 1 # define false 0typedef int buol; / * Custom Bool * / # Endif
Typedef struct stack * stackhandle; / * stack handle * /
/ ******* Interface ******* / stackhandle initstack; / * Constructs an empty stack * / boore gettop (StackHandle S, Void * ELEM); / * If the stack is not available, Then use ELEM to return the top elements of S and return to True * / Bool Push (StackHandle S, Void * ELEM); / * Insert element, ELEM is the new stack top element * / bool Pop (StackHandle S, Void * ELEM) ; / * If the stack is not empty, delete the stack top element of S and return it with ELEM, and return to true.elem, you can also get a NULL value, only pop-up action * / bool clear clear clear (stackhandle s); / * Put S Set to empty stack * / bool destroystack (StackHandle * s); / * Destroy stack s * / bool stackempty (stackhandle s); / * Stack empty? * / Int StackLength (StackHandle S); / * Stack length * /
#ENDIF
/ * Name: stack.c Copyright: 1.0 Author: avalon date: 02-10-04 19:48 Description: Pan design of the stack of generics * / # include
#ifndef avalon_bool # Define avalon_bool # define true 1 #define false 0typedef int buol; #ENDIF
/ ******* Data structure ******* / TypeDef struct node {struct node * prior; / * Front position * / void * data; / ** /} node, * nodehandle
TypeDef struct stack {nodehandle base; / * stack * / nodeHandle Top; / * Stack top * / int size_of_stack; / * Stack length * / size_t size_of_data; / ** /} stack, * stackhandle;
/ ****** Internal functions ****** / NodeHandle AllocNode (StackHandle S, Void * ELEM) {/ * only for internal use for allocation space * / nodeHandle Temp; size_t size; assert (null! = S && NULL! = ELEM); / ** / size = S-> size_of_data; temp = (nodeHandle); assert (null! = Temp); (TEMP-> DATA) = (void * ) Malloc (size); Memcpy (Temp-> Data, ELEM, SIZE); Return Temp;} Bool Freenode (NodeHandle * n) {/ * Delete Node * / assert (null! = * n && null! = n) ; / ** / free ((* n) -> data); free (* n); return true;}
/ ******* External interface ******* / stackhandle initstack (size_t size) {/ * constructs an empty stack * / stackhandle s = (STACKHANDLE) malloc (SIZEOF (STACK); Assert (null ! = S); / ** / S-> TOP = S-> Base = NULL; S-> size_of_stack = 0; s-> size_of_data = size; return s;} BOOL Gettop (StackHandle S, Void * ELEM) { / * If the stack is not available, use ELEM to return the top of the top of S and return true * / if (s == null || S-> Base == S-> TOP) Return False; Memcpy (ELEM, S- > TOP-> DATA, S-> Size_OF_DATA); RETURN TRUE;} Bool Push (StackHandle S, Void * ELEM) {/ * Insert Element, ELEM is the new stack of top elements * / nodehandle temp; assert (null! = s ); Assert (null! = ELEM); Temp = allocnode (s, elem); / * Assign a node * / (S-> size_of_stack) ; Temp-> PRIOR = S-> Top; S-> TOP = Temp; Return True;} Bool Pop (StackHandle S, VOID * ELEM) {/ * If the stack is not empty, delete the top elements of S and return it with ELEM and return to True ELEM, but only NULL value, only do Pop-up action * / nodehandle temp; if (0 == S-> size_of_stack) / * stack empty * / return false; Temp = S-> TOP-> PRIOR; (S-> Size_Of_stack) -; if (null! = ELEM) / * If the parameter is NULL, then pop up * / gettop (s, elem); FreeNode & (S-> TOP)); S-> TOP = Temp; Return True;} Bool ClearsTack (StackHandle S) {/ * Set S is empty * / assert (null! = S); while (0! = S-> size_of_stack) {POP (s, null);} reburn true;} Bool Destroystack (stackHandle * s) {/ * Destruction Stack S * / assert (null! = S); assert (null! = * S); IF (0! = (* s) -> size_of_stack) ClearsTack (* s); free (* s); * s = null; return true;} Bool StackMPTY (StackHandle S) {/ * stack empty? * / assert NULL! = S); if (0 == S-> size_of_stack) return true;} int stacklength (stacklacele s) {/ * stack length * / assert (NULL! =