1. 简介
栈(stack)是限定仅在表尾进行插入或删除操作的线性表。因此,对栈来说,表尾端有其特殊含义,称为
栈顶(top),相应地,表头端称为
栈底(bottom)。不含元素的空表称为空栈。
假设栈S=(a1, a2, ..., an),则称为a1为栈底元素,an为栈顶元素,栈的修改是按后进先出的原则进行的。因此,栈又称为后进先出(last in first out)的线性表。
2. 栈的实现
下面是一个栈的简单实现:
/*********************************************************************************
*Copyright(C),Your Company
*FileName: stack.h
*Author: Huangjh
*Version:
*Date: 2018-01-18
*Description: 栈(C语言)实现
*Others:
**********************************************************************************/
#ifndef _STACK_H
#define _STACK_H
#define STACK_TRUE 0
#define STACK_FALSE -1
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
typedef int SElemType;
typedef struct
{
SElemType *pBase;
SElemType *pTop;
int iStackSize;
}Stack;
int InitStack(Stack *pStack);
void DestroyStack(Stack *pStack);
void ClreaStack(Stack *pStack);
int StackEmpty(Stack *pStack);
int StackLength(Stack *pStack);
int GetTop(Stack *pStack, SElemType *pElem);
int Put(Stack *pStack, SElemType elem);
int Pop(Stack *pStack, SElemType *pElem);
#endif //#ifndef _STACK_H
/*********************************************************************************
*Copyright(C),Your Company
*FileName: stack.h
*Author: Huangjh
*Version:
*Date: 2018-01-18
*Description: 栈(C语言)实现
*Others:
**********************************************************************************/
#include <stdlib.h>
#include <assert.h>
#include "stack.h"
//构造一个空栈
int InitStack(Stack *pStack)
{
assert(pStack != NULL);
pStack->pBase = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (pStack->pBase == NULL)
{
abort();
}
pStack->pTop = pStack->pBase;
pStack->iStackSize = STACK_INIT_SIZE;
return STACK_TRUE;
}
//销毁栈
void DestroyStack(Stack *pStack)
{
assert(pStack != NULL);
if (pStack->pBase != NULL)
free(pStack->pBase);
pStack->iStackSize = 0;
pStack->pBase = NULL;
pStack->pTop = NULL;
}
//清空栈元素
void ClreaStack(Stack *pStack)
{
assert(pStack != NULL);
pStack->iStackSize = 0;
pStack->pBase = NULL;
pStack->pTop = NULL;
}
// 判断栈是否为空
int StackEmpty(Stack *pStack)
{
assert(pStack != NULL);
return pStack->pBase == pStack->pTop;
}
// 返回栈的长度
int StackLength(Stack *pStack)
{
assert(pStack != NULL);
return pStack->pTop - pStack->pBase;
}
// 获取栈顶的元素
int GetTop(Stack *pStack, SElemType *pElem)
{
assert(pStack != NULL);
if (pElem == NULL || StackEmpty(pStack))
return STACK_FALSE;
*pElem = *(pStack->pTop - 1);
return STACK_TRUE;
}
// 压栈
int Put(Stack *pStack, SElemType elem)
{
assert(pStack != NULL);
if (pStack->pTop - pStack->pBase >= pStack->iStackSize)
{
pStack->pBase = (SElemType *)realloc(pStack->pBase, (pStack->iStackSize + STACK_INCREMENT) * sizeof(SElemType));
if (pStack->pBase == NULL)
{
abort();
}
pStack->pTop = pStack->pBase + pStack->iStackSize;
pStack->iStackSize += STACK_INCREMENT;
}
*(pStack->pTop++) = elem;
return STACK_TRUE;
}
// 出栈
int Pop(Stack *pStack, SElemType *pElem)
{
assert(pStack != NULL);
if (StackEmpty(pStack))
{
return STACK_FALSE;
}
*pElem = *(--pStack->pTop);
return STACK_TRUE;
}
/*********************************************************************************
*Copyright(C),Your Company
*FileName: main.h
*Author: Huangjh
*Version:
*Date: 2018-01-18
*Description: 栈(C语言)测试用例
*Others:
**********************************************************************************/
#include <stdio.h>
#include "stack.h"
int main(void)
{
//初始化栈
Stack stStack;
InitStack(&stStack);
for (int i = 9; i >= 0; i--)
{
Put(&stStack, i);
}
printf("栈的长度为:%d.\r\n", StackLength(&stStack));
for (int i = 9; i >= 0; i--)
{
SElemType temp;
Pop(&stStack, &temp);
printf("出栈:%d.\r\n", temp);
}
if (StackEmpty(&stStack))
{
printf("栈为空.\r\n");
}
else
{
printf("栈不为空.\r\n");
}
DestroyStack(&stStack);
return 0;
}
运行结果如下所示:
栈的长度为:10.
出栈:0.
出栈:1.
出栈:2.
出栈:3.
出栈:4.
出栈:5.
出栈:6.
出栈:7.
出栈:8.
出栈:9.
栈为空.