C语言数组方式实现 栈结构
#include <stdio.h>
#include <stdlib.h>
const long MaxSizeOfStack = 999;
struct Stack {
int arr[MaxSizeOfStack];
int * bottom;
int * top;
};
void InitStack(struct Stack * stack) {
stack->top = stack->arr;
stack->bottom = stack->arr;
}
int IsEmpty(struct Stack * stack) {
return stack->top == stack->bottom;
}
int Top(struct Stack * stack) {
return *(stack->top-1);
}
void Push(struct Stack * stack, int n) {
*(stack->top) = n;
++stack->top;
}
void Pop(struct Stack * stack) {
if(IsEmpty(stack)) return;
--stack->top;
}
void print(struct Stack * stack) {
for(int * p =stack->bottom; p != stack->top; p++)
printf("%d ", *p);
printf("\n");
}
int main() {
struct Stack stack;
InitStack(&stack);
Push(&stack, 1);
Push(&stack, 2);
Push(&stack, 3);
Pop(&stack);
print(&stack);
}