栈和队列的介绍

栈:

什么是栈?

栈 是一种特殊的线性表 

栈的特性: 只能在一端插入/删除输入
插入的地方叫进栈,删除的地方出栈,栈的修改只针对栈顶

栈需要的头文件(c语言中)

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>

栈的定义

typedef int STDataType;

typedef struct Stack
{
	STDataType* _a;
	int top;//表示栈顶
	int capacity;//空间大小

}ST;

栈中所需要实现的接口

void StackInit(ST* ps);//初始化

void StackDestory(ST* ps);//销毁

void StackPush(ST* ps,STDataType x);//栈顶的插入

void StackPop(ST* ps);//栈顶的删除 

STDataType StackTop(ST* ps);//看查栈顶的位置

int StackSize(ST* ps);//看查多大

bool StackEmpty(ST* ps);//判断为不为空

栈接口的实现

#include"Stack.h"


void StackInit(ST* ps)//初始化
{
	assert(ps);
	ps->top = 0;
	ps->capacity = 4;
	ps->_a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->_a == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
}

void StackDestory(ST* ps)//销毁
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->top = ps->capacity = 0;
}

void StackPush(ST* ps, STDataType x)//栈顶的插入
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = realloc(ps->_a, ps->capacity * 2 * sizeof(STDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->_a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->_a[ps->top++] = x;
}

void StackPop(ST* ps)//栈顶的删除 
{
	assert(ps);
	assert(ps->top > 0);

	ps->top--;
}

STDataType StackTop(ST* ps)//看查栈顶的位置
{
	assert(ps);
	assert(ps->top > 0);
	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;
}

队列

什么是队列?

队列是一种特殊的线性表 

队列  只允许在一端插入数据 另一端删除数据

队列需要的头文件

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>

队列的定义 以及设置头/尾节点

typedef int QDataType;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct Queue
{
	QNode* head;//头节点
	QNode* tail;//尾节点
}Queue;

队列需要的实现的接口

void QueueInit(Queue* pq);//初始化

void QueueDestory(Queue* pq);//销毁

void QueuePush(Queue* pq,QDataType x);//头插

void QueuePop(Queue* pq);//尾删

QDataType QueueFront(Queue* pq);//取头数据

QDataType QueueBack(Queue* pq);//取尾

int QueueSize(Queue* pq);

bool QueueEmpty(Queue* pq);

队列接口的实现

#include"Queue.h"

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

void QueueDestory(Queue* pq)//销毁
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QDataType x)//尾插
{
	assert(pq);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;

	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newNode;
	}
	else
	{
		pq->tail->next = newNode;
		pq->tail = newNode;
	}
}

void QueuePop(Queue* pq)//头删
{
	assert(pq);
	assert(pq->head);
	if (pq->head->next == NULL)//只有一个节点的时候
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;

	}
}


QDataType QueueFront(Queue* pq)//取头数据
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
QDataType QueueBack(Queue* pq)//取尾
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->data;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		++size;
		cur = cur->next;
	}
	return size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

  • 28
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dabai__a

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

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

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

打赏作者

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

抵扣说明:

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

余额充值