一.顺序栈
1.1 概念
栈是一种 先进后出 的数据结构 FILO(first in last out)
1.2栈的逻辑结构
线性结构
1.3顺序栈的存储结构
顺序存储的栈,我们称之为顺序栈,
是基于数组实现的,一般会配合一个 "栈顶指针" top 来使用
顺序栈本身相当于对顺序表操作的一个约束:只允许在同一端插入和删除元素。
顺序栈的操作:
创建栈
清空栈
销毁栈
向栈中插入元素:入栈/压栈(push)
判断栈是否为满
获取栈中的元素:出栈/弹栈(pop)
判断栈是否为空
1.4顺序栈代码实现:
1.4.1头文件:
#ifndef _SEQ_STACK_H_
#define _SEQ_STQACK_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
typedef struct __STACK{
int s[N];
int top;
}stack_t;
int create_stack(stack_t **p);
int push_stack(stack_t* my_stack, int value);
int is_full(stack_t *my_stack);
int pop_stack(stack_t *my_stack, int *num);
int print_stack(stack_t *my_stack);
int is_empty(stack_t* my_stack);
int clean_stack(stack_t *my_stack);
int destroy_stack(stack_t **my_stack);
#endif
1.4.2函数文件:
#include "seq_stack.h"
//创建栈空间
int create_stack(stack_t **p)
{
if (NULL == p){
printf("入参为NULL,请检查\n");
return -1;
}
*p = (stack_t*)malloc(sizeof(stack_t));
if (NULL == *p){
printf("内存分配失败\n");
exit(-1);
}
(*p)->top = 0;
return 0;
}
//出栈
int push_stack(stack_t* my_stack, int value)
{
if (NULL == my_stack){
printf("入参为NULL,请检查\n");
return -1;
}
if (is_full(my_stack)){
printf("栈已经满了\n");
return -1;
}
my_stack->s[my_stack->top] = value;
my_stack->top++;
return 0;
}
//判断是否为满
int is_full(stack_t *my_stack)
{
if (NULL == my_stack){
printf("入参为NULL 请检查\n");
return -1;
}
return my_stack->top==N?1:0;
}
//判断是否为空
int is_empty(stack_t* my_stack)
{
if (NULL == my_stack){
printf("入参为NULL 请检查\n");
return -1;
}
return my_stack->top == 0?1:0;
}
//出栈
int pop_stack(stack_t *my_stack, int *num)
{
if (NULL == my_stack){
printf("入参为NULL 请检查\n");
return -1;
}
if (is_empty(my_stack)){
printf("栈已经空了,出栈失败\n");
return -1;
}
my_stack->top--;
*num = my_stack->s[my_stack->top];
return 0;
}
//打印
int print_stack(stack_t *my_stack)
{
if (NULL == my_stack){
printf("入参为NULL 请检查\n");
return -1;
}
int i = 0;
for (int i =0; i < my_stack->top; i++){
printf("%d ",my_stack->s[i]);
}
printf("\n");
return 0;
}
int clean_stack(stack_t *my_stack)
{
if (NULL == my_stack){
printf("入参为NULL 请检查\n");
return -1;
}
my_stack->top = 0;
}
int destroy_stack(stack_t **my_stack)
{
if (NULL == my_stack || *my_stack == NULL){
printf("入参为NULL 请检查\n");
return -1;
}
free(*my_stack);
*my_stack = NULL;
return 0;
}
二链式栈:
2.1概念:
链式存储的栈,我们称之为链式栈,
是基于链表实现的
链式栈本身相当于对链表表操作的一个约束:只允许在同一端插入和删除元素。
如果采用链表尾作为入栈和出栈的端点,那么入栈和出栈都需要遍历,时间复杂度都是O(n)
所以一般链式栈都是 使用链表头部进行 入栈和出栈 时间复杂度是 O(1)
2.2顺序栈的操作:
创建栈
清空栈
销毁栈
向栈中插入元素:入栈/压栈(push)
获取栈中的元素:出栈/弹栈(pop)
判断栈是否为空
打印栈中所有元素--学习阶段看现象用的
2.3代码实现:
2.3.1头文件:
#ifndef _LINK_STACK_H_
#define _LINK_STACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Node{
int data;
struct _Node *next;
}node_t;
typedef struct _Stack{
unsigned int count;
node_t* top;
}stack_t;
int create_stack(stack_t **my_stack);
int push_stack(stack_t *my_stack, int value);
int is_empty(stack_t *my_stack);
int pop_stack(stack_t *my_stack, int *num);
int print_stack(stack_t *my_stack);
int clean_stack(stack_t *my_stack);
int destroy_stack(stack_t **my_stack);
#endif
2.3.2函数文件:
#include "link_stack.h"
int create_stack(stack_t **my_stack)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
*my_stack = (stack_t*)malloc(sizeof(my_stack));
(*my_stack)->top = NULL;
(*my_stack)->count = 0;
return 0;
}
int push_stack(stack_t *my_stack, int value)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
node_t* pnew = (node_t*)malloc(sizeof(node_t));
pnew->next = NULL;
pnew->data = value;
pnew->next = my_stack->top;
my_stack->top = pnew;
my_stack->count++;
return 0;
}
int pop_stack(stack_t *my_stack, int *num)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
if (is_empty(my_stack)){
printf("栈为空,出栈失败\n");
}
*num = my_stack->top->data;
node_t* temp;
temp = my_stack->top;
my_stack->top = temp->next;
free(temp);
temp = NULL;
my_stack->count--;
return 0;
}
int is_empty(stack_t *my_stack)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
return my_stack->count == 0 ? 1 : 0;
}
int clean_stack(stack_t *my_stack)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
node_t* pdel = NULL;
while (my_stack->top != NULL){
pdel = my_stack->top;
my_stack->top = pdel->next;
free(pdel);
}
pdel = NULL;
my_stack->count = 0;
return 0;
}
int destroy_stack(stack_t **my_stack)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
clean_stack(*my_stack);
free(*my_stack);
*my_stack = NULL;
return 0;
}
int print_stack(stack_t *my_stack)
{
if (my_stack == NULL){
printf("入参错误,请检测\n");
}
node_t* ptemp = my_stack->top;
while(NULL != ptemp){
printf("%d ", ptemp->data);
ptemp = ptemp->next;
}
printf("\n");
}