顺序栈

本文详细介绍了如何使用C语言实现一个顺序栈,包括初始化、入栈、获取栈顶元素、出栈、判断栈是否为空、是否已满、获取元素个数、清空和销毁栈等操作。顺序栈具有先进后出的特点,通过动态内存分配实现栈的扩容。提供的代码示例展示了栈的基本操作,并进行了实际测试。
摘要由CSDN通过智能技术生成

栈:一种先进后出,后进先出的数据结构

栈分为顺序栈和链栈,本文实现的是不定长的顺序栈

顺序栈的插入和删除都在尾部,因为入栈和出栈的时间复杂度为O(1)

顺序栈怎么设计:

typedef struct Stack
{
	int* base;   //用来申请动态内存
	int stack_Size;  //栈总体个数
	int top;   //栈顶指针,当前栈存放的有效元素个数指向下一个存放数据的位置
	           //这里和书上不一致,因为实现的是不定长顺序栈,所以用
}Stack,*PStack;

顺序栈可以进行的操作

//增删改查

//初始化
void InitStack(PStack ps);

//入栈操作(尾插)
bool push(PStack ps,int val);

// 获取栈顶元素的值,但不删除
bool Get_top(PStack ps,int *rtval);

//获取栈顶元素的值,但删除,出栈操作(尾删)
bool Pop(PStack ps,int* rtval);

//判空
bool IsEmpty(PStack ps);

//判满
bool IsFull(PStack ps);

//获取有效元素个数
int Get_length(PStack ps);

//清空
void Clear(PStack ps);

//销毁
void Destory(PStack ps);

//扩容
void Inc(PStack ps);

具体代码实现:

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"stack.h"
#define INIT_SIZE 20
//初始化
void InitStack(PStack ps)
{
	assert(ps != NULL);
	if (ps == NULL)
		return;

	ps->base = (int*)malloc(sizeof(int) * INIT_SIZE);
	assert(ps->base != NULL);

	ps->top = 0;
	ps->stack_Size = INIT_SIZE;
}

//入栈操作(尾插)
bool push(PStack ps, int val)
{
	assert(ps != NULL);         //断言
	if (ps == NULL)
		return false;           //判满

	if (IsFull(ps))              //如果满了,扩容
	{
		Inc(ps);
	}
	ps->base[ps->top++] = val;    //插入
	return true;                  //返回值
}

// 获取栈顶元素的值,但不删除
bool Get_top(PStack ps,int *rtval)
{
	assert(ps != NULL);
	if (ps == NULL)
		return false;           //   \0只有字符串认识它

	if (IsEmpty(ps))
	{
		return false;
	}

	*rtval = ps->base[ps->top - 1];
	return true;
}

//获取栈顶元素的值,但删除,出栈操作(尾删)
bool Pop(PStack ps, int* rtval)
{
	assert(ps != NULL && !IsEmpty(ps));       //ps不能为空且栈不能是空栈
	if (ps == NULL || IsEmpty(ps))
		return false;

	*rtval = ps->base[--ps->top];
	return true;
}

//判空
bool IsEmpty(PStack ps)
{
	return ps->top == 0;
}

//判满
bool IsFull(PStack ps)
{
	return ps->top==ps->stack_Size;
}

//获取有效元素个数
int Get_length(PStack ps)
{
	return ps->top;
}

//清空
void Clear(PStack ps)
{
	ps->top = 0;
}

//销毁
void Destory(PStack ps)
{
	assert(ps != NULL);
	if (ps == NULL)
		return;

	free(ps->base);          //释放malloc申请的动态内存

	ps->base = NULL;         //将指针赋值为NULL,防止出现野指针
	ps->top = 0;
	ps->stack_Size = 0;
}

//扩容
void Inc(PStack ps)            //如果按2倍去扩容    1.5
{
	assert(ps != NULL);
	if (ps == NULL)
		return;

	//给栈空间最大容量重新赋值
	ps->stack_Size *= 2;

	//从堆内申请更高大的空间给我们
	ps->base = (int*)realloc(ps->base, sizeof(int) * ps->stack_Size);
}

代码实现:

//顺序栈实现代码
int main()
{
	Stack head;
	InitStack(&head);

	for (int i = 0; i < 15; i++)
	{
		push(&head, i);
	}
	int rtval = 0;
	printf("%-5d\n", Get_length(&head));

	while (!IsEmpty(&head))
	{
		Get_top(&head, &rtval);
		printf("%-5d", rtval);
		Pop(&head, &rtval);
	}

	printf("\n");
	printf("%-5d\n", Get_length(&head));

	Clear(&head);
	printf("%-5d\n", Get_length(&head));

	Destory(&head);
	Destory(&head);

	return 0;
}

运行结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值