栈(逻辑结构)
限制为只有一个端口进出元素,就导致先进后出和特性。
顺序栈(容量在限)、链式栈(可以无限容量)。
一般常用于,表达式解析,内存管理(函数的调用提供支持)。
顺序栈:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TYPE int
typedef struct Stack
{
TYPE* arr; // 内存首地址
int len; // 栈的容量 typedef unsigned int size_t;
int top; // 栈顶下标
}Stack;
// 创建
Stack* creat_stack(size_t cal);
// 销毁
void destory_stack(Stack* stack);
// 入栈
bool push_stack(Stack* stack,TYPE data);
// 出栈
bool pop_stack(Stack* stack);
// 栈空
bool empty_stack(Stack* stack);
// 栈满
bool full_stack(Stack* stack);
// 栈顶
TYPE* top_stack(Stack* stack);
int main()
{
Stack* stack = creat_stack(15);
printf("开始入栈:");
for(int i=0; i<20; i++)
{
printf("入栈%s,",push_stack(stack,i)?"成功":"失败");
printf("栈顶:%d\n",*top_stack(stack));
}
printf("------------------------\n开始出栈:\n");
for(int i=0; i<15; i++)
{
printf("栈顶:%2d,",*top_stack(stack));
printf("出栈:%s\n",pop_stack(stack)?"成功":"失败");
}
}
// 创建
Stack* creat_stack(size_t cal)
{
Stack* stack = malloc(sizeof(Stack));
stack->arr = malloc(sizeof(TYPE)*cal);
stack->len = cal;
stack->top = -1;
return stack;
}
// 销毁
void destory_stack(Stack* stack)
{
free(stack->arr);
free(stack);
}
// 入栈
bool push_stack(Stack* stack,TYPE data)
{
if(full_stack(stack)) return false;
stack->arr[++stack->top] = data;
return true;
}
// 出栈
bool pop_stack(Stack* stack)
{
if(empty_stack(stack)) return false;
stack->top--;
return true;
}
// 栈空
bool empty_stack(Stack* stack)
{
return -1 == stack->top;
}
// 栈满
bool full_stack(Stack* stack)
{
return stack->top+1 >= stack->len;
}
// 栈顶
TYPE* top_stack(Stack* stack)
{
if(empty_stack(stack)) return NULL;
return stack->arr + stack->top;
}
链式栈:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TYPE int
typedef struct Node
{
TYPE data;
struct Node* next;
}Node;
Node* creat_node(TYPE data)
{
Node* node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
typedef struct Stack
{
Node* top;
size_t len;
}Stack;
// 创建
Stack* creat_stack(void);
// 销毁
void destory_stack(Stack* stack);
// 栈空
bool empty_stack(Stack* stack);
// 入栈
void push_stack(Stack* stack,TYPE data);
// 出栈
bool pop_stack(Stack* stack);
// 栈顶
TYPE* top_stack(Stack* stack);
int main()
{
Stack* stack = creat_stack();
for(int i=0; i<10; i++)
{
push_stack(stack,i);
printf("top:%d\n",*top_stack(stack));
}
printf("-----------------\n");
for(int i=0; i<10; i++)
{
printf("top:%d,",*top_stack(stack));
printf("出栈%s\n",pop_stack(stack)?"成功":"失败");
}
destory_stack(stack);
}
// 创建
Stack* creat_stack(void)
{
Stack* stack = malloc(sizeof(Stack));
stack->top = NULL;
stack->len = 0;
return stack;
}
// 销毁
void destory_stack(Stack* stack)
{
while(pop_stack(stack));
free(stack);
}
// 栈空
bool empty_stack(Stack* stack)
{
return NULL == stack->top;
}
// 入栈
void push_stack(Stack* stack,TYPE data)
{
Node* node = creat_node(data);
node->next = stack->top;
stack->top = node;
stack->len++;
}
// 出栈
bool pop_stack(Stack* stack)
{
if(empty_stack(stack)) return false;
Node* node = stack->top;//是top与node的地址相同
stack->top = node->next;
free(node);//删除地址释放内存
stack->len--;
return true;
}
// 栈顶
TYPE* top_stack(Stack* stack)
{
if(empty_stack(stack)) return NULL;
return &stack->top->data;
}