array_stack.c 文件
#include "stack.h"
static STACK_TYPE stack[STACK_SIZE];
static int top_element = -1;
void push(STACK_TYPE value)
{
if (is_full())
{
printf("stack is full\n");
return;
}
top_element++;
stack[top_element] = value;
}
STACK_TYPE pop(void)
{
if (is_empty())
{
printf("stack is empty\n");
return -1;
}
return stack[top_element--];
}
STACK_TYPE top(void)
{
if (is_empty())
{
printf("stack is empty\n");
return -1;
}
return stack[top_element];
}
int is_full(void)
{
return top_element == STACK_SIZE - 1;
}
int is_empty(void)
{
return top_element == -1;
}
stack.h 文件
#pragma once
#include <stdio.h>
#include <assert.h>
#define STACK_TYPE int
#define STACK_SIZE 100
void push(STACK_TYPE value);
STACK_TYPE pop(void);
STACK_TYPE top(void);
int is_full(void);
int is_empty(void);
main.c 文件
#include "stack.h"
int main(int argc, char *argv[])
{
printf("hello world\r\n");
push(1);
push(2);
printf("%d\r\n", pop());
printf("%d\r\n", pop());
printf("%d\r\n", pop());
}
makefile 文件
objects = main.o array_stack.o
main:$(objects)
gcc -o main $(objects)
main.o:main.c
gcc -c main.c
array_stack.o:array_stack.c
gcc -c array_stack.c
.PHONY:clean
clean:
-rm main $(objects)