目录
一、栈的概念
先进后出,后进先出
二、基础栈
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
typedef int DataType;
typedef struct
{
DataType data[MAXSIZE];
int top;
}SqStack;
int main()
{
DataType result;
SqStack* stack;
// 栈初始化
stack = (SqStack*)malloc(sizeof(stack));
stack->top = -1;
// 入栈
// 若栈满
if (stack->top == MAXSIZE - 1)
{
printf("栈满\n");
}
else
{
stack->data[++stack->top] = 300;
}
// 出栈
// 若栈空
if (stack->top == -1)
{
printf("栈空\n");
}
else
{
result = stack->data[stack->top--];
printf("%d\n", result);
}
}
三、栈 -- 函数
stack.h
#ifndef __STACK_H__
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 100
typedef int DataType;
typedef struct
{
DataType data[MAXSIZE];
int top;
}SqStack;
SqStack* InitStack(void);
int PushStack(SqStack* stack, DataType e);
int PopStack(SqStack* stack, DataType* e);
int ClearStack(SqStack* stack);
int StackIsEmpty(SqStack* stack);
int StackIsFull(SqStack* stack);
int StackLength(SqStack* stack);
int StackGetTop(SqStack* stack, DataType* e);
int StackTraverse(SqStack* stack);
#endif // !__STACK_H
stack.c
#include "stack.h"
/// <summary>
/// 栈初始化
/// </summary>
/// <param name=""></param>
/// <returns></returns>
SqStack* InitStack(void)
{
SqStack* stack;
stack = (SqStack*)malloc(sizeof(stack));
stack->top = -1;
return stack;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="stack"></param>
/// <param name="e"></param>
/// <returns></returns>
int PushStack(SqStack* stack, DataType e)
{
// 若栈满
if (stack->top == MAXSIZE - 1)
{
return 0;
}
// 数据添加
stack->data[++stack->top] = e;
}
/// <summary>
/// 出栈
/// </summary>
/// <param name="stack"></param>
/// <param name="e"></param>
/// <returns></returns>
int PopStack(SqStack* stack, DataType* e)
{
if (stack->top == -1)
{
return 0;
}
//获取数据
*e = stack->data[stack->top--];
return 1;
}
/// <summary>
/// 清空栈
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
int ClearStack(SqStack* stack)
{
stack->top = -1;
return 1;
}
/// <summary>
/// 判断栈是否为空
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
int StackIsEmpty(SqStack* stack)
{
return stack->top == -1 ? 1 : 0;
}
/// <summary>
/// 判断栈是否已满
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
int StackIsFull(SqStack* stack)
{
return stack->top == MAXSIZE - 1 ? 1 : 0;
}
/// <summary>
/// 获取当前栈的个数
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
int StackLength(SqStack* stack)
{
return stack->top + 1;
}
/// <summary>
/// 获取当前栈顶元素
/// </summary>
/// <param name="stack"></param>
/// <param name="e"></param>
/// <returns></returns>
int StackGetTop(SqStack* stack, DataType* e)
{
// 若栈为空
if (StackIsEmpty(stack))
{
return 0;
}
// 获取栈顶元素
*e = stack->data[stack->top];
return 1;
}
/// <summary>
/// 栈的遍历
/// </summary>
/// <param name="stack"></param>
/// <returns></returns>
int StackTraverse(SqStack* stack)
{
int i = 0;
// 若栈为空
if (StackIsEmpty(stack))
{
printf("栈空\n");
}
while (i <= stack->top)
{
printf("%d ", stack->data[i++]);
}
printf("\n");
return 1;
}
main.c
#include <stdio.h>
#include "stack.h"
int main()
{
DataType result;
SqStack* stack;
// 栈初始化
stack = InitStack();
// 入栈
PushStack(stack, 100);
PushStack(stack, 200);
PushStack(stack, 300);
// 出栈
PopStack(stack, &result);
printf("%d\n", result);
}
如有错误,烦请批评指正