栈和队列的模拟实现(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;
}
相关推荐
凌肖战12 分钟前
力扣上刷题之C语言实现(数组)
c语言·算法·leetcode
秋夫人38 分钟前
B+树(B+TREE)索引
数据结构·算法
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
Milo_K1 小时前
今日 leetCode 15.三数之和
算法·leetcode
Darling_001 小时前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode
AlexMercer10121 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
Greyplayground1 小时前
【算法基础实验】图论-BellmanFord最短路径
算法·图论·最短路径
蓑 羽1 小时前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
源代码:趴菜1 小时前
LeetCode63:不同路径II
算法·leetcode·职场和发展