Stack Header File in C: Complete Guide
In C language, it is common to declare a stack header file in the following way:
#ifndef STACK_H
#define STACK_H
// 定义数据类型
typedef struct {
int data;
struct Node* next;
} Node;
// 定义堆栈结构
typedef struct {
Node* top;
} Stack;
// 函数声明
void push(Stack* stack, int data);
int pop(Stack* stack);
int peek(Stack* stack);
int isEmpty(Stack* stack);
#endif // STACK_H
In the header file, first use #ifndef and #define to prevent duplicate inclusion, then define the necessary data structures (Node struct and Stack struct), and finally declare the related operation functions. This allows the header file to be included in other source files, and then use the defined data structures and functions to operate the stack.