common.h
#ifndef __COMMON_H__
#define __COMMON_H__
#define STACK_SIZE_MAX 10
typedef int SElemType;
typedef struct stack_s
{
SElemType *base; // stack bottom
SElemType *top; // stack top
int stack_size; // size of stack
}STACK_S, *PSTACK_S;
int stack_init(STACK_S *s);
void stack_deinit(STACK_S *s);
int stack_push(STACK_S *s, SElemType e);
int stack_pop(STACK_S *s, SElemType *e);
void stack_dump(STACK_S *s);
typedef struct linkstack_s
{
SElemType data;
struct linkstack_s *next;
}LINKSTACK_S, *PLINKSTACK_S;
int linkstack_init(LINKSTACK_S **s);
void linkstack_deinit(LINKSTACK_S **s);
int linkstack_push(LINKSTACK_S **s, SElemType e);
int linkstack_pop(LINKSTACK_S **s, SElemType *e);
void linkstack_dump(LINKSTACK_S **s);
#endif
顺序栈stack.c
//顺序栈
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
int stack_init(STACK_S *s)
{
if(!s)
{
printf("ERR: NULL pointer.\n");
return -1;
}
s->base = malloc(STACK_SIZE_MAX * sizeof(SElemType));
if(!(s->base))
{
printf("ERR: malloc fail\n");
return -1;
}
s->top = s->base;
s->stack_size = STACK_SIZE_MAX;
return 0;
}
void stack_deinit(STACK_S *s)
{
if(!s)
{
printf("ERR: NULL pointer.\n");
return;
}
if(!(s->base))
{
printf("ERR: stack is null.\n");
return;
}
free(s->base);
s->base = NULL;
}
int stack_push(STACK_S *s, SElemType e)
{
if(!s)
{
printf("ERR: NULL pointer.\n");
return -1;
}
/* full? */
if((s->top - s->base) == s->stack_size * sizeof(SElemType))
{
printf("ERR: stack is full.\n");
return -1;
}
*(s->top) = e;
(s->top) += sizeof(SElemType);
return 0;
}
int stack_pop(STACK_S *s, SElemType *e)
{
if(!s)
{
printf("ERR: NULL pointer.\n");
return -1;
}
/* empty? */
if(s->top == s->base)
{
printf("ERR: stack is empty.\n");
return -1;
}
(s->top) -= sizeof(SElemType);
*e = *(s->top);
return 0;
}
void stack_dump(STACK_S *s)
{
int i;
SElemType e;
int used_size;
if(!s)
{
printf("ERR: NULL pointer.\n");
return;
}
printf("======================================\n");
printf(" stack dump \n");
printf("======================================\n");
used_size = (s->top - s->base) / sizeof(SElemType);
printf(" Used / Total : %d / %d.\n", used_size, s->stack_size);
for(i = 0; i < used_size; i++)
{
e = *(s->base + i * sizeof(SElemType));
printf(" %02d: %d.\n", i, e);
}
}
链栈linkstack.c
//链栈
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
int linkstack_init(LINKSTACK_S **s)
{
if(*s)
{
printf("ERR: Already exist.\n");
return -1;
}
*s = NULL;
return 0;
}
void linkstack_deinit(LINKSTACK_S **s)
{
PLINKSTACK_S ptr;
if(!(*s))
{
printf("ERR: %s--> link stack not exist.\n", __func__);
return;
}
do
{
ptr = (*s)->next;
free(*s);
*s = ptr;
}while(ptr != NULL);
*s = NULL;
}
int linkstack_push(LINKSTACK_S **s, SElemType e)
{
PLINKSTACK_S ptr = malloc(sizeof(LINKSTACK_S));
ptr->data = e;
ptr->next = *s;
*s = ptr;
return 0;
}
int linkstack_pop(LINKSTACK_S **s, SElemType *e)
{
PLINKSTACK_S ptr;
if(!(*s))
{
printf("ERR: %s--> link stack not exist.\n", __func__);
return -1;
}
*e = (*s)->data;
ptr = (*s)->next;
free(*s);
*s = ptr;
return 0;
}
void linkstack_dump(LINKSTACK_S **s)
{
int i = 0;
PLINKSTACK_S ptr = *s;
if(!(*s))
{
printf("ERR: %s--> link stack not exist.\n", __func__);
return;
}
printf("======================================\n");
printf(" linkt stack dump \n");
printf("======================================\n");
while(ptr != NULL)
{
printf("top - %d : %d.\n", i, ptr->data);
i++;
ptr = ptr->next;
}
printf("==========dump=end====================\n");
}
一个进制转换的例子,使用栈:
/* 进制转换 */
int conversion(int src, int *out, int base)
{
SElemType e;
int n = src;
STACK_S stStack;
if(!out)
{
printf("null pointer.\n");
return -1;
}
stack_init(&stStack);
while(n)
{
stack_push(&stStack, n%base);
n = n/base;
}
stack_dump(&stStack);
n = 0;
while(0 == stack_pop(&stStack, &e))
{
n = n*10 + e;
}
*out = n;
return 0;
}
Makefile文件:
#最终的可执行文件名
TARGET = out
#可执行文件依赖的所有.o文件
SRCS := $(wildcard *.c)
OBJECTS := $(patsubst %.c,%.o,$(SRCS))
$(warning $(SRCS))
$(warning $(OBJECTS))
export CC = gcc
export AR := ar
export AS := as
CFLAGS += -g -O2 -Wall
CFLAGS += $(INCLUDE_PATH)
.PHNOY: all clean
all: ${TARGET}
${TARGET}: ${OBJECTS}
${CC} -o ${TARGET} ${OBJECTS} ${LIB_PATH} ${LIBS}
%.o:%.c
${CC} ${CFLAGS} -c $< ${INCLUDE_PATH}
clean:
-rm -rf ${TARGET} ${OBJECTS}