栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

栈的基本操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

栈的基本操作

目录
  • 一、pandas是什么?栈的概念及结构
  • 二、栈的框架定义
  • 三、栈的功能实现
    • 1.栈的初始化
    • 2.栈的销毁
    • 3.入栈
    • 4.出栈
    • 5.取栈顶元素
    • 6.栈的判空
    • 7.计算栈的大小
  • 四.总代码
    • 1.Stack.h
    • 2.Stack.c
    • 3.Test.c

一、pandas是什么?栈的概念及结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

二、栈的框架定义


实现栈有两种结构:分别为数组结构和链式结构。相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。

三、栈的功能实现 1.栈的初始化
//初始化
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
2.栈的销毁
//销毁
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
3.入栈
//入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top==ps->capacity)
	{
		//刚开始是0,给四个容量,不是的话,扩展2倍
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
		
	}
	ps->a[ps->top] = x;
	ps->top++;
}
4.出栈
//出栈
void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	//不用释放空间
	//free(ps->a[ps->top]);
	ps->top--;
}
5.取栈顶元素
// 获取栈顶元素  
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}
6.栈的判空
bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}
7.计算栈的大小
//获取栈中有效元素个数
int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}
四.总代码 1.Stack.h
#pragma once
#include
#include
#include
#include


typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化
void StackInit(ST* ps);

//销毁
void StackDestroy(ST* ps);

//入栈
void StackPush(ST* ps,STDataType x);

//出栈
void StackPop(ST* ps);

//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);

// 获取栈顶元素
STDataType StackTop(ST* ps);

//获取栈中有效元素个数
int StackSize(ST* ps);
2.Stack.c
#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

//初始化
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

//销毁
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

//入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top==ps->capacity)
	{
		//刚开始是0,给四个容量,不是的话,扩展2倍
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
		
	}
	ps->a[ps->top] = x;
	ps->top++;
}

//出栈
void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->top--;
}


bool StackEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

// 获取栈顶元素  
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

//获取栈顶号数
int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}
3.Test.c
#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h" 

void TestStack()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);

	StackPush(&st, 2);
	StackPush(&st, 3);
	printf("%d ", StackTop(&st));
	StackPop(&st);
	printf("%d ", StackTop(&st));
	StackPop(&st);
	
	StackPush(&st, 4);
	StackPush(&st, 5);

	while (!StackEmpty(&st))
	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	printf("n");

}

int main()
{
	TestStack();

	return 0;
}
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1037263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号