Writing a generic stack with C

xiaoxiao2021-03-06  100

The following, the stuff is I used to compile the pass in DEV C 4.9.9.0. This is the header file "stack.h"

============================================================================================================================================================================================================= ====================

#ifndef AVALON_STACK_H #define AVALON_STACK_H #include #include #include #ifndef AVALON_BOOL #define AVALON_BOOL #define TRUE 1 #define FALSE 0 typedef int BOOL; / * custom Bool / #ndif / * Defines a stack structure * / #define declare_node (type, name) / type / typefedef struct type ## node / {/ type data; / struct type ## node * prior; /} node ## name ; #define declare_stack (type, name) / Struct Type ## stack / {/ int size; / Struct Type ## node * base; / struct type ## node * top; /} stack ## name; / * Pan type Stack definition * / #define declare (type, name) / declare_node (type, name); / declare_stack (type, name); / ********************* ******************************* / * initialization stack, name is the address of the newly declared stack name * / #define Init (name) / do {/ stack ## name.base = stack ## Name.top = null; / stack ## name.size = 0; /} while (0) / ********* ** Do not destroy the top elements of the stack value ************************************* / #Define get (name, elem) / do {/ if (stack ## Name.size! = 0) {/ elem = stack ## Name.top-> data; /} /} while (0) / ************** Press Elem ELEM *********************************** / #Define push (name, elem) / do {/ node ## name * Temp = (Node ## Name *) Malloc (SIDE ## Name)); / assert (temp); / temp-> data = elem; / (stack ## name.size) ; / if (1! = stack) # Name.size) {/ TEMP-> PRIOR = stack ## name.top; / stack ## name.top = Temp; /} / else {/ TEMP-> prior = null; / stack ## name.top = Stack ## name.base = Temp; /} /} while (0) / ********** Stack top element assigns ELEM, and pops up ********** / # Define Pop (Name, Elem) / Do {/ Node ## Name * Temp = stack ## name.top; / if (stack ## name.size! = 0) {/ * elem = stack ## name.top- > data; / if (stack ## name.size! = 1) {/ stack ## name.top = Temp-> prior; / free (temp); /} / else / stack ## name.top = stack # # Name.base = null; / (stack ## name.size) -;

/} /} while (0) / ************* Clear stack *********************** / #define clear (name) / do {/ node ## name * Temp; / while (stack ## name.size-! = 0) {/ TEMP = stack ## name.top; / stack ## name.top = Stack ## name.top-> prior; / free (temp); /} / stack ## name.size = 0; / stack ## name.base = stack ## name.top = null; /} while 0) / **************** Stack empty ???? *********************** / #define Empty (Name) (stack ## name.size == 0) / **************** ************** ********** / #Define length (name) (1? stack ## name.size: 0) #ENDIF This is a test program: ============== ============================================================================================================================================================================================================= =======

#include "stack.h" #include #include int main (void) {declare (int, a); declare (double, b); double b; int A; init (a ); Init (b); Printf ("% D", length (a)); for (a = 1; a <= 10000; A ) {push (a, a); push (a, a); push (PUSH) A, a);} Printf ("% D", length (a)); for (b = 1; b <= 10000; b ) {push (b, b);} while (! EMPTY (a)) { POP (A, & A); if (! EMPTY (B)) {POP (B, & B);}} Clear (a); system ("pause"); return 0;}

It used to write with void *, considering that the efficiency is quite low, it is written.

转载请注明原文地址:https://www.9cbs.com/read-105583.html

New Post(0)