栈和队列的模拟实现(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;
}
相关推荐
前端小刘哥22 分钟前
赋能在线教育与企业培训:视频直播点播平台EasyDSS视频点播的核心技术与应用实践
算法
吗~喽29 分钟前
【LeetCode】四数之和
算法·leetcode·职场和发展
Net_Walke39 分钟前
【散列函数】哈希函数简介
算法·哈希算法
卿言卿语1 小时前
CC1-二叉树的最小深度
java·数据结构·算法·leetcode·职场和发展
码流之上1 小时前
【一看就会一写就废 指间算法】执行操作后的最大 MEX —— 同余、哈希表
算法·面试
仰泳的熊猫1 小时前
LeetCode:889. 根据前序和后序遍历构造二叉树
数据结构·c++·算法
2025年一定要上岸1 小时前
【日常学习】10-15 学习re
学习·算法·正则表达式
aramae2 小时前
数据结构与算法(递归)
开发语言·经验分享·笔记·算法
小欣加油2 小时前
leetcode 329 矩阵中的最长递增路径
c++·算法·leetcode·矩阵·深度优先·剪枝
Emilia486.2 小时前
【Leetcode&nowcode&数据结构】单链表的应用(初阶)
c语言·数据结构·算法·leetcode