栈和队列

栈和队列,第1张

栈的概念及其结构

栈:是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素的 *** 作。进行数据插入和删除 *** 作的一端称为栈顶,另外一端称为栈底。栈中的数据元素遵循先进后出(后进先出)的原则。
压栈: 站的插入 *** 作叫做进栈/压栈/入栈,入数据在栈顶
出栈:栈的删除 *** 作叫做出栈。出数据也在栈顶

栈的实现

站的实现一般可以使用链表或者数组实现,想对而言数组的结构实现更优一些,因为数组在尾上插入数据的代价比较小。

栈的头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;//栈顶
	int capacity;//容量
}ST;
//初始化栈
void StackInit(ST* ps);
//销毁栈
void StackDestroy(ST* ps);
//入栈
void StackPush(ST* ps, STDataType x);
//出栈
void StackPop(ST* ps);
//获取栈顶的元素
STDataType StackTop(ST* ps);
//获取栈中有效元素的个数
int StackSize(ST* ps);
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps);

栈的实现

#include"stack.h"

//初始化栈
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;//这里的top为0,表示这top指向栈顶数据的下一个
	//这里的top也可以为-1,意味着top指向栈顶数据
}
//销毁栈
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->capacity == ps->top)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
//出栈
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;
}
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
队列的概念及结构

队列:只允许在一端进行插入数据的 *** 作,在另一端进行删除数据 *** 作的特殊线性表,队列具有先进先出的特性
入队列:进行插入 *** 作的一端称为队尾
出队列:进行删除 *** 作的一端称为队头

队列的实现

队列可以用链表和数组的结构实现,使用链表的结构更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
队列的头文件

#pragma once
#include
#include
#include
#include
#include


typedef int QDataType;

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

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

//初始化队列
void QueueInit(Queue* ps);
//销毁队列
void QueueDestroy(Queue* ps);
//队尾入队列
void QueuePush(Queue* ps, QDataType x);
//队头出队列
void QueuePop(Queue* ps);
//获取队列头部元素
QDataType QueueFront(Queue* ps);
//获取队列队尾的元素
QDataType QueueBack(Queue* ps);
//返回队列中有多少元素
int QueueSize(Queue* ps);
//检测队列是否为空
bool QueueEmpty(Queue* ps);

队列的实现

#include"Queue.h"


//初始化队列
void QueueInit(Queue* ps)
{
	assert(ps);
	ps->head = NULL;
	ps->tail = NULL;
}
//销毁队列
void QueueDestroy(Queue* ps)
{
	assert(ps);
	QueueNode* cur = ps->head;
	while (cur != NULL)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head = ps->tail = NULL;

}

//队尾入队列
void QueuePush(Queue* ps, QDataType x)
{
	assert(ps);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	newnode->data = x;
	newnode->next = NULL;
	if (ps->head == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = newnode;
	}
}
//队头出队列
void QueuePop(Queue* ps)
{
	assert(ps);
	assert(ps->head);
	QueueNode* next = ps->head->next;
	free(ps->head);
	ps->head = next;
	if (ps->head == NULL)
	{
		ps->tail = NULL;
	}
}
//获取队列头部元素
QDataType QueueFront(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->head->data;
}
//获取队列队尾的元素
QDataType QueueBack(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->tail->data;
	
}
//返回队列中有多少元素
int QueueSize(Queue* ps)
{
	assert(ps);
	int count = 0;
	QueueNode* cur = ps->head;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}
//检测队列是否为空
bool QueueEmpty(Queue* ps)
{
	assert(ps);
	return ps->head == NULL;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/872594.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存