typedef struct node {
int x;
int y;
} MyStructure;
如同:
struct node {
int x;
int y;
};
typedef struct node MyStructure;
堆栈实现示例
//definitions
//C99 has #include <stdbool.h> for this
typedef short boolean;
#define true 1
#define false 0
//You may #define YOUR_APIENTRY APIENTRY (from a system header)
#define YOUR_APIENTRY
#define YOUR_APIENTRYP YOUR_APIENTRY*
//predeclarations
struct _Stack;
typedef struct _Stack Stack;
struct _StackImpl;
typedef struct _StackImpl StackImpl;
struct _Element;
typedef struct _Element Element;
//stack implementation function type definitions
typedef void (YOUR_APIENTRYP pfnPush) (Stack*, Element);
typedef Element (YOUR_APIENTRYP pfnPop) (Stack*);
typedef Element (YOUR_APIENTRYP pfnPeek) (Stack*);
typedef boolean (YOUR_APIENTRYP pfnIsEmpty) (Stack*);
typedef boolean (YOUR_APIENTRYP pfnIsFull) (Stack*);
//funct ptr table
struct _StackImpl{
pfnPush push;
pfnPop pop;
pfnPeek peek;
pfnIsEmpty isEmpty;
pfnIsFull isFull;
};
//stack
typedef struct _Stack{
Element* elems; //any appropriate container
size_t elemCount;
//if you want to replace the implementation using
//different func tables (polymorphic)
//StackImpl* funcPtrs;
} Stack;
//stack element
struct _Element{
int value;
};
//default implementation /replace NULL's with actual function pointers)
StackImpl defaultStackImpl =
{
NULL,
NULL,
NULL,
NULL,
NULL
};
//function wrappers
void push(Stack* stack, Element elem)
{
//if you use a polymorphic implementation
//stack->funcPtrs->push(stack,elem);
defaultStackImpl.push(stack,elem);
}