栈的实现:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
typedef int Element;
#define MAX_STACK_LENGTH 10
struct Stack {
Element stack[MAX_STACK_LENGTH];
int length;
};
void Init(struct Stack* stack)
{
memset(stack->stack, 0, sizeof(Element) * MAX_STACK_LENGTH);
stack->length = 0;
}
int Empty(struct Stack* stack)
{
return stack->length == 0;
}
int Length(struct Stack* stack)
{
return stack->length;
}
void Push(struct Stack* stack, Element elem)
{
if (stack->length == MAX_STACK_LENGTH) {
return;
} else {
stack->stack[stack->length++] = elem;
}
}
void Pop(struct Stack* stack)
{
if (stack->length == 0) {
return;
} else {
stack->length--;
}
}
Element Top(struct Stack* stack)
{
return stack->stack[stack->length - 1];
}
// following is test code, you can use like this:
int main()
{
struct Stack stack;
Init(&stack);
for (int i = 0; i < 120; i++) {
Push(&stack, i);
}
while (!Empty(&stack)) {
int t = Top(&stack);
printf("%d
", t);
Pop(&stack);
}
return 0;
}