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

 栈的模拟实现

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);
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;
}
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;
}

队列的模拟实现

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);
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;
}
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值