栈的基本概念以及C语言操作
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。不含任何元素的栈称为空栈。
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
数据结构中的栈(Stack)是限定只能在表的一端进行插入和删除操作的线性表。
程序中的栈存放局部变量的区域,函数中的局部变量都是存放在栈中。
栈的C语言实现
函数声明
#pragma oncetypedef int STDataType;//静态栈
//#define N 10
//typedef struct Stack {
// STDataType _a[N];
// int _top; // 栈顶
//}Stack;//动态栈
typedef struct Stack
{STDataType* _array;int _capacity;int _top; // 标记栈顶位置
}Stack;// 初始化栈
void StackInit(Stack* ps);// 入栈
void StackPush(Stack* ps, STDataType data);// 出栈
void StackPop(Stack* ps);// 获取栈顶元素
STDataType StackTop(Stack* ps);// 获取栈中有效元素个数
int StackSize(Stack* ps);// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);// 销毁栈
void StackDestroy(Stack* ps);
函数定义
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <assert.h>// 初始化栈
void StackInit(Stack* ps)
{assert(ps);ps->_capacity = 10;ps->_top = 0;ps->_array = (STDataType)malloc(sizeof(STDataType)*ps->_capacity);
}// 入栈
void StackPush(Stack* ps, STDataType data)
{assert(ps);ps->_array[ps->_top] = data;++ps->_top;
}// 出栈
void StackPop(Stack* ps)
{assert(ps);ps->_array[ps->_top];--ps->_top;
}// 获取栈顶元素
STDataType StackTop(Stack* ps)
{assert(ps);if (ps->_top == 0){return;}STDataType a = ps->_array[ps->_top-1];return a;
}// 获取栈中有效元素个数
int StackSize(Stack* ps)
{assert(ps);int count = 0;for (int i = 0; i < ps->_top; ++i){++count;}return count;
}// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{assert(ps);if (ps->_top == 0){return 1;}return 0;
}// 销毁栈
void StackDestroy(Stack* ps)
{assert(ps);if(ps->_array == NULL){return;}ps->_capacity = 0;ps->_top = 0;free(ps->_array);ps->_array = NULL;ps = NULL;
}//打印
void Pri(Stack* ps)
{assert(ps);//栈为空if (ps->_top == 0){return;}for (int i = 0; i < ps->_top; ++i){printf("%d ", ps->_array[i]);}printf("\n");
}
测试函数
int main()
{Stack s;//初始化栈StackInit(&s);//入栈StackPush(&s, 1);StackPush(&s, 2);StackPush(&s, 3);StackPush(&s, 4);StackPush(&s, 5);// 1 2 3 4 5printf("入栈后:\n");Pri(&s);//出栈StackPop(&s);// 1 2 3 4printf("出栈后:\n");Pri(&s);// 获取栈顶元素 printf("栈顶元素为:%d\n", StackTop(&s));printf("\n");// 获取栈中有效元素个数 printf("栈中有效元素个数为:%d\n", StackSize(&s));printf("\n");// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 int ret = StackEmpty(&s);if (ret == 0){printf("此栈不为空\n");}elseprintf("此栈为空\n");printf("\n");// 销毁栈 StackDestroy(&s);ret = StackEmpty(&s);if (ret == 0){printf("此栈不为空\n");}elseprintf("此栈为空\n");printf("\n");system("pause");return 0;
}
栈的基本概念以及C语言操作
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。不含任何元素的栈称为空栈。
栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
数据结构中的栈(Stack)是限定只能在表的一端进行插入和删除操作的线性表。
程序中的栈存放局部变量的区域,函数中的局部变量都是存放在栈中。
栈的C语言实现
函数声明
#pragma oncetypedef int STDataType;//静态栈
//#define N 10
//typedef struct Stack {
// STDataType _a[N];
// int _top; // 栈顶
//}Stack;//动态栈
typedef struct Stack
{STDataType* _array;int _capacity;int _top; // 标记栈顶位置
}Stack;// 初始化栈
void StackInit(Stack* ps);// 入栈
void StackPush(Stack* ps, STDataType data);// 出栈
void StackPop(Stack* ps);// 获取栈顶元素
STDataType StackTop(Stack* ps);// 获取栈中有效元素个数
int StackSize(Stack* ps);// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);// 销毁栈
void StackDestroy(Stack* ps);
函数定义
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <assert.h>// 初始化栈
void StackInit(Stack* ps)
{assert(ps);ps->_capacity = 10;ps->_top = 0;ps->_array = (STDataType)malloc(sizeof(STDataType)*ps->_capacity);
}// 入栈
void StackPush(Stack* ps, STDataType data)
{assert(ps);ps->_array[ps->_top] = data;++ps->_top;
}// 出栈
void StackPop(Stack* ps)
{assert(ps);ps->_array[ps->_top];--ps->_top;
}// 获取栈顶元素
STDataType StackTop(Stack* ps)
{assert(ps);if (ps->_top == 0){return;}STDataType a = ps->_array[ps->_top-1];return a;
}// 获取栈中有效元素个数
int StackSize(Stack* ps)
{assert(ps);int count = 0;for (int i = 0; i < ps->_top; ++i){++count;}return count;
}// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{assert(ps);if (ps->_top == 0){return 1;}return 0;
}// 销毁栈
void StackDestroy(Stack* ps)
{assert(ps);if(ps->_array == NULL){return;}ps->_capacity = 0;ps->_top = 0;free(ps->_array);ps->_array = NULL;ps = NULL;
}//打印
void Pri(Stack* ps)
{assert(ps);//栈为空if (ps->_top == 0){return;}for (int i = 0; i < ps->_top; ++i){printf("%d ", ps->_array[i]);}printf("\n");
}
测试函数
int main()
{Stack s;//初始化栈StackInit(&s);//入栈StackPush(&s, 1);StackPush(&s, 2);StackPush(&s, 3);StackPush(&s, 4);StackPush(&s, 5);// 1 2 3 4 5printf("入栈后:\n");Pri(&s);//出栈StackPop(&s);// 1 2 3 4printf("出栈后:\n");Pri(&s);// 获取栈顶元素 printf("栈顶元素为:%d\n", StackTop(&s));printf("\n");// 获取栈中有效元素个数 printf("栈中有效元素个数为:%d\n", StackSize(&s));printf("\n");// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 int ret = StackEmpty(&s);if (ret == 0){printf("此栈不为空\n");}elseprintf("此栈为空\n");printf("\n");// 销毁栈 StackDestroy(&s);ret = StackEmpty(&s);if (ret == 0){printf("此栈不为空\n");}elseprintf("此栈为空\n");printf("\n");system("pause");return 0;
}