1. 什么叫顺序栈
百度百科中的定义如下
顺序栈是栈的顺序实现。顺序栈是指利用顺序存储结构实现的栈。采用地址连续的存储空间(数组)依次存储栈中数据元素,由于人栈和出栈运算都是在栈顶进行,而栈底位置是固定不变的,可以将栈底位置设置在数组空间的起始处;栈顶位置是随入栈和出栈操作而变化的,故需用一个整型变量top来记录当前栈顶元素在数组中的位置
解释
栈的元素依次存放在一个一维数组中。下标小的一端作为栈底。用一个变量记录栈顶位置,称“栈顶指针”。
应用
进栈是把元素存放在栈顶后面一个位置,栈顶往后移;出栈是删除栈顶元素,栈顶往前移。适合栈元素数量比较确定的情况。
2. 栈的实现
stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include <stdbool.h>
/* 栈结构 */
struct stack_struct
{
int *top;
int *bottom;
int capacity;
};
/*
* 函数名称:creat_stack
* 功能描述:创建栈
* 输入参数:capacity-容量
* 返 回 值:栈结构体指针
*/
struct stack_struct *creat_stack(int capacity);
/*
* 函数名称:stack_is_empty
* 功能描述:判断栈是否为空
* 输入参数:stack_t-栈结构体指针
* 返 回 值:true-空,false-不为空
*/
bool stack_is_empty(struct stack_struct * stack_t);
/*
* 函数名称:stack_push
* 功能描述:入栈
* 输入参数:stack_t-栈结构体指针,data-入栈数据
* 返 回 值:true-入栈成功,false-入栈失败
*/
bool stack_push(struct stack_struct *stack_t, int data);
/*
* 函数名称:stack_pop
* 功能描述:出栈
* 输入参数:
*/
bool stack_pop(struct stack_struct *stack_t);
/*
* 函数名称:stack_print
* 功能描述:遍历栈数据
*
*/
void stack_print(struct stack_struct *stack_t);
/*
* 函数名称:stack_destroy
* 功能描述:销毁栈
* 输入参数:栈指针
*/
void stack_destroy(struct stack_struct *stack_t)
#endif
stack.c
#include "stack.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
/*
* 函数名称:creat_stack
* 功能描述:创建栈
* 输入参数:capacity-容量
* 返 回 值:栈结构体指针
*/
struct stack_struct *creat_stack(int capacity)
{
struct stack_struct *stack = (struct stack_struct *)malloc(sizeof(struct stack_struct));
if(stack == NULL)
return NULL;
if(stack->bottom == NULL)
{
stack->bottom = (int *)malloc(sizeof(int)*capacity);
if(stack->bottom == NULL)
return NULL;
}
stack->top = stack->bottom;
stack->capacity = capacity;
return stack;
}
/*
* 函数名称:stack_is_empty
* 功能描述:判断栈是否为空
* 输入参数:stack_t-栈结构体指针
* 返 回 值:true-空,false-不为空
*/
bool stack_is_empty(struct stack_struct * stack_t)
{
if(stack_t->top == stack_t->bottom)
{
return true;
}
return false;
}
/*
* 函数名称:stack_is_full
* 功能描述:判断栈是否已满
* 输入参数:stack_t-栈结构体指针
* 返 回 值:true-满,false-不满
*/
bool stack_is_full(struct stack_struct *stack_t)
{
if((stack_t->top - stack_t->bottom) >= stack_t->capacity)
{
printf("stack is full...\n");
return true;
}
return false;
}
/*
* 函数名称:stack_push
* 功能描述:入栈
* 输入参数:stack_t-栈结构体指针,data-入栈数据
* 返 回 值:true-入栈成功,false-入栈失败
*/
bool stack_push(struct stack_struct *stack_t, int data)
{
if(stack_is_full(stack_t) != true)
{
*stack_t->top = data;
stack_t->top++;
return true;
}
return false;
}
/*
* 函数名称:stack_pop
* 功能描述:出栈
* 输入参数:statck_t-栈指针
* 返 回 值:true-成功,false-失败
*/
bool stack_pop(struct stack_struct *stack_t)
{
if(stack_is_empty(stack_t) == true)
{
printf("stack is empty, pop failed...\n");
return false;
}
stack_t->top--;
return true;
}
/*
* 函数名称:stack_print
* 功能描述:遍历栈数据
* 输入参数:stack_t-栈指针
*/
void stack_print(struct stack_struct *stack_t)
{
printf("stack[%d]:", stack_t->capacity);
int *tmp_t = stack_t->top;
while(tmp_t != stack_t->bottom)
{
printf("%d ", *(--tmp_t));
}
printf("\r\n");
}
/*
* 函数名称:stack_destroy
* 功能描述:销毁栈
* 输入参数:栈指针
*/
void stack_destroy(struct stack_struct *stack_t)
{
free(stack_t);
}
main.c
#include "stack.h"
#include <stddef.h>
#include <stdio.h>
int main(void)
{
struct stack_struct *stack_t = creat_stack(10);
if(stack_t == NULL)
return -1;
int i;
for(i=0; i<10; i++)
{
stack_push(stack_t, i+1);
}
printf("push:\n");
stack_print(stack_t);
for(i=0; i<10; i++)
{
stack_pop(stack_t);
printf("pop: %d\n", i+1);
stack_print(stack_t);
}
stack_destroy(stack_t);
return 0;
}
运行结果:
root@book-virtual-machine:/mnt/hgfs/windows/c/data_struct/data_struct/stack# ./a.out
push:
stack[10]:10 9 8 7 6 5 4 3 2 1
pop: 1
stack[10]:9 8 7 6 5 4 3 2 1
pop: 2
stack[10]:8 7 6 5 4 3 2 1
pop: 3
stack[10]:7 6 5 4 3 2 1
pop: 4
stack[10]:6 5 4 3 2 1
pop: 5
stack[10]:5 4 3 2 1
pop: 6
stack[10]:4 3 2 1
pop: 7
stack[10]:3 2 1
pop: 8
stack[10]:2 1
pop: 9
stack[10]:1
pop: 10
stack[10]:
root@book-virtual-machine:/mnt/hgfs/windows/c/data_struct/data_struct/stack#
值得注意的是:
在 linux 下,申请完 栈结构体内存后,若不额外申请 top指针和bottom指针所指向的内存,top和bottom 为空,对 top 和 bottom 操作时会出现段错误