0
点赞
收藏
分享

微信扫一扫

<数据结构----栈(C语言版)>

GhostInMatrix 2022-05-03 阅读 103
数据结构

前言

本节内容

1. 栈的概念

2. 栈的实现

2.1 动态结构定义

2.2  栈的初始化

2.3 入栈

2.3 出栈(压栈)

2.4 获取栈顶元素

2.5 获取栈中有效元素个数

2.6 检测栈是否为空

2.7 栈的销毁

3. 栈的使用

4. 完整源码

Stack.h

Stack.c

test.c

5. 最终实现效果


1. 栈的概念

2. 栈的实现

2.1 动态结构定义

typedef int STDataType;// typedef定义STDataType类型

typedef struct Stack
{
	int* a;       // 定义动态存储数组
	int top;      // 记录栈顶    
	int capacity; // 记录容量
}ST;

2.2  栈的初始化

void StackInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

2.3 入栈

void StackPush(ST* ps, STDataType data)
{
	assert(ps);

	// 判断是否栈满,栈满则需要扩容
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
		if (ps->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newCapacity;
	}

	ps->a[ps->top++] = data;
}

2.3 出栈(压栈)

void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	
	ps->top--;
}

2.4 获取栈顶元素

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);

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

2.5 获取栈中有效元素个数

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

2.6 检测栈是否为空

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

	return ps->top == 0;
}

2.7 栈的销毁

void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

3. 栈的使用

void test()
{
	ST st;
	StackInit(&st);

    // 入栈
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);

    // 输出栈中的值,每输出一个,就出栈一个
	while (!StackEmpty(&st)) // 当栈为空时停止输出
 	{
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}

	StackDestroy(&st);
}

4. 完整源码

Stack.h

#pragma once

#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>

typedef int STDataType;

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

// 初始化栈
void StackInit(ST* ps);
// 入栈
void StackPush(ST* ps, STDataType data);
// 出栈
void StackPop(ST* ps);
// 获取栈顶元素
STDataType StackTop(ST* ps);
// 获取栈中有效元素个数
int StackSize(ST* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(ST* ps);
// 销毁栈
void StackDestroy(ST* ps);

Stack.c

#include "Stack.h"

void StackInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackPush(ST* ps, STDataType data)
{
	assert(ps);

	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
		if (ps->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newCapacity;
	}

	ps->a[ps->top++] = data;
}

void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	
	ps->top--;
}

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);

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

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

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

	return ps->top == 0;
}

void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

test.c

#include "Stack.h"

void test()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);

	printf("%d ", StackTop(&st));
	StackPop(&st);

	StackPush(&st, 4);
	StackPush(&st, 5);

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

	StackDestroy(&st);
}

int main()
{
	test();

	return 0;
}

5. 最终实现效果


举报

相关推荐

0 条评论