顺序栈与链式栈

栈(逻辑结构)
限制为只有一个端口进出元素,就导致先进后出和特性。
顺序栈(容量在限)、链式栈(可以无限容量)。
一般常用于,表达式解析,内存管理(函数的调用提供支持)。
顺序栈:

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值