栈和队列的实现


一、栈的实现

初始化栈

//初始化栈
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;//top指向栈当前元素的下一个位置
	ps->capacity = 0;
}

销毁栈

void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

入栈

//入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		//栈满
		//如果是第一次开辟,先开辟四个,否则以两倍增加
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (temp == NULL)
			exit(-1);
		ps->a = temp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

出栈

//出栈
void StackPop(ST* ps)
{
	assert(ps);
	//assert(ps->top > 0);//如果栈为空则出错
	assert(!StackEmpty(ps));
	ps->top--;
}

获取栈顶元素

//获取栈顶元素
STDataType StackTop(ST* ps)
{
	assert(ps);
	//assert(ps->top > 0);
	assert(!StackEmpty(ps));
	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;
}

二、队列的实现

结构定义

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
//以链表实现队列
typedef int QDataType;
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QueueNode;

typedef struct Queue
{
	QueueNode* head;
	QueueNode* tail;
}Queue;

初始化队列

// 初始化队列
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}

队尾入队列

// 队尾入队列
void QueuePush(Queue* pq, QDataType data)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
		exit(-1);
	newnode->data = data;
	newnode->next = NULL;
	//检查是否为空
	if (pq->head == NULL)
	{
		//为空,头尾指向第一个结点
		pq->head = pq->tail = newnode;
	}
	else
	{
		//不为空,尾插
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

}

队头出队列

// 队头出队列
void QueuePop(Queue* pq)
{
	//判断队列是否为空
	assert(pq);
	assert(!QueueEmpty(pq));
	QueueNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;


	//当全部元素都出队列后尾指针置空
	if (pq->head == NULL)
		pq->tail = NULL;
}

获取队列头部元素

// 获取队列头部元素
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}

获取队列队尾元素

// 获取队列队尾元素
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

获取队列中有效元素个数

// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	int count = 0;
	QueueNode* sur = pq->head;
	while (sur != NULL)
	{
		count++;
		sur = sur->next;
	}
	return count;
}

检测队列是否为空

// 检测队列是否为空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

销毁队列

// 销毁队列
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* sur = pq->head;
	while (sur != NULL)//不能写出sur!=pq->tail,这样会少删了最后一个
	{
		QueueNode* next = sur->next;
		free(sur);
		sur = next;
	}
	pq->head = pq->tail = NULL;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s_persist

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值