栈和队列的模拟实现(C语言版)

栈的模拟实现

cpp 复制代码
Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//栈的实现,底层逻辑是数组
//动态开辟空间
typedef int DataType;
typedef struct Stack {
	DataType* arr;//定义动态数组
	int capacity;//有效空间
	int top;//栈顶元素
}St;
// 初始化栈 
void StackInit(St* ps);
// 入栈 
void StackPush(St* ps, DataType data);
// 出栈 
void StackPop(St* ps);
// 获取栈顶元素 
DataType StackTop(St* ps);
// 获取栈中有效元素个数 
int StackSize(St* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(St* ps);
// 销毁栈 
void StackDestroy(St* ps);
cpp 复制代码
Stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
// 初始化栈 
void StackInit(St* ps) {
	assert(ps);
	ps->arr = NULL;
	ps->capacity = 0;
	ps->top = 0;
}
// 入栈 
void StackPush(St* ps, DataType data) {
	assert(ps);
	//判断是否要开辟空间
	if (ps->capacity == ps->top) {
		//开辟空间
		int Newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		St* tem = (St*)realloc(ps->arr, sizeof(St) * Newcapacity);
		if (tem == NULL) {
			perror("malloc");
			exit(-1);
		}
		ps->arr = tem;
		ps->capacity = Newcapacity;
	}
	ps->arr[ps->top] = data;
	ps->top++;
}
// 出栈 
void StackPop(St* ps) {
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
// 获取栈顶元素 
DataType StackTop(St* ps) {
	assert(ps);
	assert(ps->top > 0);
	return  ps->arr[ps->top-1];
}
// 获取栈中有效元素个数 
int StackSize(St* ps) {
	assert(ps);
	assert(ps->top);
	return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(St* ps) {
	assert(ps);
	return ps->top == 0;
}
// 销毁栈 
void StackDestroy(St* ps) {
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
}
cpp 复制代码
test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void test01() {
	St st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPop(&st);
	DataType ret = StackTop(&st);
	int con = StackSize(&st);
	int ret1 = StackEmpty(&st);
	while (!StackEmpty(&st)) {
		printf("%d ", StackTop(&st));
		StackPop(&st);
	}
	StackDestroy(&st);
}
int main() {
	test01();
	return 0;
}

队列的模拟实现

cpp 复制代码
Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int DataType;
typedef struct QueueNode {
	DataType val;
	struct QueueNode* pnext;
}QNode;
typedef struct Queue {
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;
//初始化头尾结点
void QueueInit(Queue* q1);

//队尾插入
void QueuePush(Queue* q1,DataType x);
//出队列
void QueuePop(Queue* q1);
//取出队列头元素
DataType QueueGettop(Queue* q1);
//计算队列元素大小
int QueueSize(Queue* q1);
//判空
bool QueEmpty(Queue* q1);
//摧毁
void QueueDesert(Queue* q1);
cpp 复制代码
Queue.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
//初始化头尾结点
void QueueInit(Queue* q1) {
	assert(q1);
	q1->phead = NULL;
	q1->ptail = NULL;
	q1->size = 0;
}
//队尾插入
void QueuePush(Queue* q1, DataType x) {
	assert(q1);
	//申请空间
	QNode* NewNode= (QNode*)malloc(sizeof(QNode));
	if (NewNode == NULL) {
		perror("malloc");
		exit(-1);
	}
	NewNode->pnext = NULL;
	NewNode->val = x;
	if (q1->ptail == NULL) {
		q1->phead = NewNode;
		q1->ptail = NewNode;
	}
	else {
		q1->ptail->pnext = NewNode;
		q1->ptail = NewNode;
	}
	q1->size++;
}
//出队列
void QueuePop(Queue* q1) {
	assert(q1);
	assert(q1->size != 0);
	if (q1->phead->pnext == NULL) {
		free(q1->phead);
		q1->phead = q1->ptail = NULL;
	}
	else {
		Queue* tem = q1->phead->pnext;
		free(q1->phead);
		q1->phead = tem;
	}
	q1->size--;
}
//取出队列头元素
DataType QueueGettop(Queue* q1) {
	assert(q1);
	assert(q1->phead);
	return (q1->phead->val);
}
//取对尾
DataType QueueGetback(Queue* q1) {
	assert(q1);
	assert(q1->ptail);
	return (q1->ptail->val);
}
//计算队列元素大小
int QueueSize(Queue* q1) {
	assert(q1);
	return q1->size;
}
//判空
bool QueEmpty(Queue* q1) {
	assert(q1);
	return q1->size == 0;
}  
//摧毁
void QueueDesert(Queue* q1) {
	assert(q1);
	Queue* cur = q1->phead;
	while (cur) {  
		Queue* next =q1->phead->pnext;
		free(cur);
		cur = next;
	}
	q1->phead = q1->ptail = NULL;
	q1->size = 0;
}
cpp 复制代码
test.c 

#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void test01() {
	Queue q1;
	QueueInit(&q1);
	QueuePush(&q1, 1);
	QueuePush(&q1, 2);
	QueuePush(&q1, 3);
	QueuePush(&q1, 4);
	QueuePush(&q1, 5);
	QueuePop(&q1);
	DataType QueFront=QueueGettop(&q1);
	printf("%d\n", QueFront);
	int con=QueueSize(&q1);
	printf("%d\n", con);
	DataType QueBack=QueueGetback(&q1);
	printf("%d\n", QueBack);
	//判空
	bool ret = QueEmpty(&q1);
	if (ret) {
		printf("队列为空\n");
	}
	else {
		printf("队列不为空\n");
	}
	//循环遍历队列元素
	while (!QueEmpty(&q1)) {
		printf("%d ", QueueGettop(&q1));
		QueuePop(&q1);
	}
}
int main() {
	test01();
	return 0;
}
相关推荐
yongui4783434 分钟前
MATLAB的指纹识别系统实现
算法
高山上有一只小老虎35 分钟前
翻之矩阵中的行
java·算法
jghhh0143 分钟前
RINEX文件进行卫星导航解算
算法
爱思德学术1 小时前
中国计算机学会(CCF)推荐学术会议-A(计算机科学理论):LICS 2026
算法·计算机理论·计算机逻辑
CVHub1 小时前
多模态图文训推一体化平台 X-AnyLabeling 3.0 版本正式发布!首次支持远程模型推理服务,并新增 Qwen3-VL 等多款主流模型及诸多功能特性,等
算法
hoiii1871 小时前
MATLAB实现Canny边缘检测算法
算法·计算机视觉·matlab
qq_430855882 小时前
线代第二章矩阵第四课:方阵的幂
算法·机器学习·矩阵
roman_日积跬步-终至千里2 小时前
【计算机设计与算法-习题2】动态规划应用:矩阵乘法与钢条切割问题
算法·矩阵·动态规划
kupeThinkPoem2 小时前
计算机算法导论第三版算法视频讲解
数据结构·算法
sali-tec2 小时前
C# 基于halcon的视觉工作流-章67 深度学习-分类
开发语言·图像处理·人工智能·深度学习·算法·计算机视觉·分类