当前位置: 代码网 > it编程>编程语言>C/C++ > C语言实现数组栈的代码示例

C语言实现数组栈的代码示例

2024年07月28日 C/C++ 我要评论
栈的概念及结构栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出lifo(last in first o

栈的概念及结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出lifo(last in first out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。

栈的定义

typedef int stdatatype;
typedef struct stack
{
	stdatatype* _a;//数组
	int _top; // 栈顶,类似顺序表中的_size
	int _capacity; // 容量
}stack;

对栈的操作

栈初始化

_top可以初始化为0,此时栈顶元素是_top-1的位置

_top也可以初始化为1,此时栈顶元素就是_top的位置

初始化为0

初始化为1

// 初始化栈
void stackinit(stack* ps)
{
	assert(ps);
	ps->_a = null;
	ps->_capacity = 0;
	ps->_top = 0;
}

压栈(入栈)

// 入栈
void stackpush(stack* ps, stdatatype data)
{
	assert(ps);
	//扩容
	if (ps->_capacity == ps->_top)
	{
		int newcapacity = ps->_capacity == 0 ? 4 : 2 * (ps->_capacity);
		stdatatype* tmp = (stdatatype*)realloc(ps->_a, newcapacity * sizeof(stdatatype));
		if (tmp == null)
		{
			perror("realloc fail");
			return;
		}
		ps->_a = tmp;
		ps->_capacity = newcapacity;
	}
	ps->_a[ps->_top++] = data;
}

出栈

void stackpop(stack* ps)
{
	assert(ps);
	assert(!stackempty(ps));
	
	ps->_top--;
}

取栈顶元素

stdatatype stacktop(stack* ps)
{
	assert(ps);
	return ps->_a[ps->_top-1];
}

判断栈是否为空

bool stackempty(stack* ps)
{
	assert(ps);
	return ps->_top == 0;
}

栈的长度

_top初始化为0,此时的_top的大小刚好就是栈的长度

int stacksize(stack* ps)
{
	assert(ps);
	return ps->_top;
}

栈销毁

void stackdestroy(stack* ps)
{
	assert(ps);
	ps->_capacity = ps->_top = 0;
	free(ps->_a);
	ps->_a = null;
}

完整总代码

头文件

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int stdatatype;
typedef struct stack
{
	stdatatype* _a;//数组
	int _top; // 栈顶,类似顺序表中的_size
	int _capacity; // 容量
}stack;
// 初始化栈
void stackinit(stack* ps);
// 入栈
void stackpush(stack* ps, stdatatype data);
// 出栈
void stackpop(stack* ps);
// 获取栈顶元素
stdatatype stacktop(stack* ps);
// 获取栈中有效元素个数
int stacksize(stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool stackempty(stack* ps);
// 销毁栈
void stackdestroy(stack* ps);

函数定义

#include"stack.h"
// 初始化栈
void stackinit(stack* ps)
{
	assert(ps);
	ps->_a = null;
	ps->_capacity = 0;
	ps->_top = 0;
}
// 入栈
void stackpush(stack* ps, stdatatype data)
{
	assert(ps);
	//扩容
	if (ps->_capacity == ps->_top)
	{
		int newcapacity = ps->_capacity == 0 ? 4 : 2 * (ps->_capacity);
		stdatatype* tmp = (stdatatype*)realloc(ps->_a, newcapacity * sizeof(stdatatype));
		if (tmp == null)
		{
			perror("realloc fail");
			return;
		}
		ps->_a = tmp;
		ps->_capacity = newcapacity;
	}
	ps->_a[ps->_top++] = data;
}
// 出栈
void stackpop(stack* ps)
{
	assert(ps);
	assert(!stackempty(ps));
	
	ps->_top--;
}
// 获取栈顶元素
stdatatype stacktop(stack* ps)
{
	assert(ps);
	return ps->_a[ps->_top-1];
}
// 获取栈中有效元素个数
int stacksize(stack* ps)
{
	assert(ps);
	return ps->_top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool stackempty(stack* ps)
{
	assert(ps);
	return ps->_top == 0;
}
// 销毁栈
void stackdestroy(stack* ps)
{
	assert(ps);
	ps->_capacity = ps->_top = 0;
	free(ps->_a);
	ps->_a = null;
}

测试

#include"stack.h"
void test()
{
	stack s;
	stackinit(&s);
	stackpush(&s, 1);
	stackpush(&s, 2);
	stackpush(&s, 3);
	stackpush(&s, 4);
 
	while (stacksize(&s)>0)
	{
		printf("%d ", stacktop(&s));
		stackpop(&s);
	}
	stackdestroy(&s);
}
int main()
{
	test();
	return 0;
}

到此这篇关于c语言实现数组栈的代码示例的文章就介绍到这了,更多相关c语言数组栈内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com