数据结构之C语言栈的实现

一、栈模型

  • 栈是限制插入和删除只能在一个位置上进行的表,该位置是表的末端,叫做栈的顶(top)。栈有时又称为LIFO(后进先出)表。
    在这里插入图片描述
  • 栈的应用
    Expression evaluation
    Backtracking
    Memory Management

二、栈的实现

1. 定义结构体

typedef struct {
	double *values;
	int top;
	int maxTop;
} Stack;

2. 栈的创建

  • 这里我们初始化top为-1, 则存第一个元素的时候 top 为0,相当于数组的A[0]
bool CreateStack(Stack *stack, int size){
	if (size < 1){
		return false;
	}
	stack->values = (double*)malloc(sizeof(double)*size);
	stack->top = -1;
	stack->maxTop = size - 1;
	return true;
}

3. 判断栈空和栈满

bool IsEmpty(Stack *stack){
	return stack->top == -1;
}

bool IsFull(Stack *stack){
	return stack->top == stack->maxTop;
}

4. 读取栈顶

bool Top(Stack *stack, double *x){
	if (IsEmpty(stack)){  // 判断栈空
		return false;
	}
	*x = stack->values[stack->top];
	return true;
}

5. 入栈

bool Push(Stack *stack, double x){
	if (IsFull(stack)){  // 判断栈满
		return false;
	}
	// add x to the stack, top must be increased
	stack->values[++stack->top] = x;
	return true;
}

6. 出栈

bool Pop(Stack *stack, double *x){
	if (IsEmpty(stack)){  // 判断栈空
		return false;
	}
	// pass the value of the top element to x
	// move the top value to the next
	*x = stack->values[stack->top--];
	return true;
}

7. 历遍打印

void DisplayStack(Stack *stack){
	printf("top --> ");
	for (int i=stack->top; i>=0; i--){
		printf("|%12f\t|\n\t", stack->values[i]);
	}
	printf("+---------------+");
}

8. 栈的销毁

void DestroyStack(Stack *stack){
	free(stack->values);
	stack->maxTop = -1;
	stack->top = -1;
	stack->values = NULL;
}

9. 完整代码

  • stack.h
#include <stdbool.h>

typedef struct {
  double *values;
  int top;
  int maxTop;
} Stack;

bool CreateStack(Stack *stack, int size);

bool IsEmpty(Stack *stack);

bool IsFull(Stack *stack);

bool Top(Stack *stack, double *x);

bool Push(Stack *stack, double x);

bool Pop(Stack *stack, double *x);

void DisplayStack(Stack *stack);

void DestroyStack(Stack *stack);
  • stack.c
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"

bool CreateStack(Stack *stack, int size){
	if (size < 1){
		return false;
	}
	stack->values = (double*)malloc(sizeof(double)*size);
	stack->top = -1;
	stack->maxTop = size - 1;
	return true;
}

bool IsEmpty(Stack *stack){
	return stack->top == -1;
}

bool IsFull(Stack *stack){
	return stack->top == stack->maxTop;
}

bool Top(Stack *stack, double *x){
	if (IsEmpty(stack)){  // 判断栈空
		return false;
	}
	*x = stack->values[stack->top];
	return true;
}

bool Push(Stack *stack, double x){
	if (IsFull(stack)){  // 判断栈满
		return false;
	}
	// add x to the stack, top must be increased
	stack->values[++stack->top] = x;
	return true;
}

bool Pop(Stack *stack, double *x){
	if (IsEmpty(stack)){  //判断栈空
		return false;
	}
	// pass the value of the top element to x
	// move the top value to the next
	*x = stack->values[stack->top--];
	return true;
}

void DisplayStack(Stack *stack){
	printf("top --> ");
	for (int i=stack->top; i>=0; i--){
		printf("|%12f\t|\n\t", stack->values[i]);  
	}
	printf("+---------------+");
}

void DestroyStack(Stack *stack){
	free(stack->values);
	stack->maxTop = -1;
	stack->top = -1;
	stack->values = NULL;
}

  • main.c
#include <stdio.h>
#include <stdbool.h>
#include "stack.h"
int main(void) {
	Stack stack;
	double val;
	CreateStack(&stack, 5);
	Push(&stack, 5.0);
	DisplayStack(&stack);
	printf("\n");
	Push(&stack, 6.5);
	DisplayStack(&stack);
	printf("\n");
	Push(&stack, -3.0);
	Push(&stack, -8.0);
	DisplayStack(&stack);
	if(Top(&stack, &val)){
		printf("Top: %f\n", val);
	}
	printf("\n");
	Pop(&stack, &val);
	DisplayStack(&stack);
	if(Top(&stack, &val)){
		printf("Top: %f\n", val);
	}
	while(!IsEmpty(&stack)){
		Pop(&stack, &val);
	}
	DisplayStack(&stack);
	DestroyStack(&stack);
	return 0;
}
  • 结果显示
    在这里插入图片描述

  • 以上均为个人学习数据结构的时候的笔记,如有错误请多多指教

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值