数据结构——栈与队列

一、栈

1.1****栈的概念及结构

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

1.2****栈的实现(数组栈)

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

1.2.1 栈的基本功能实现

cs 复制代码
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
typedef int  SDateType;
typedef struct Stack
{
	SDateType* a;
	int top;
	int capacity;

}Stack;

//初始化栈和销毁栈
void InitStack(Stack* ps);
void DestoryStack(Stack* ps);

//出栈和入栈
void StackPush(Stack* ps, SDateType x);
void StackPop(Stack* ps);

//栈的有效个数和栈顶元素
int  StackSize(Stack* ps);
int   StackTop(Stack* ps);

//栈是否为空
bool StackEmpty(Stack* ps);
1.2.1.1 栈的初始化和销毁
cs 复制代码
//初始化栈和销毁栈
void InitStack(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void DestoryStack(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;

}
1.2.1.2 入栈和出栈
cs 复制代码
//出栈和入栈
void StackPush(Stack* ps, SDateType x)
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		SDateType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SDateType* tmp = (SDateType*)realloc(ps->a,newcapacity*sizeof(SDateType));
		if (tmp == NULL)
		{
			perror("realloc fail:");
				return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;

	}
	//尾插
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);//只少有一个元素,才能删除
	ps->top--;
}
1.2.1.3 栈的元素个数和栈顶元素
cs 复制代码
//栈的有效个数和栈顶元素
int  StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}
int   StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
1.2.1.4 栈是否为空
cs 复制代码
//栈是否为空
bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

1.2.2 Stack.h

cs 复制代码
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
typedef int  SDateType;
typedef struct Stack
{
	SDateType* a;
	int top;
	int capacity;

}Stack;

//初始化栈和销毁栈
void InitStack(Stack* ps);
void DestoryStack(Stack* ps);

//出栈和入栈
void StackPush(Stack* ps, SDateType x);
void StackPop(Stack* ps);

//栈的有效个数和栈顶元素
int  StackSize(Stack* ps);
int   StackTop(Stack* ps);

//栈是否为空
bool StackEmpty(Stack* ps);

1.2.3 Stach.c

cs 复制代码
#include"Stack.h"
//初始化栈和销毁栈
void InitStack(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void DestoryStack(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;

}

//出栈和入栈
void StackPush(Stack* ps, SDateType x)
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		SDateType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SDateType* tmp = (SDateType*)realloc(ps->a,newcapacity*sizeof(SDateType));
		if (tmp == NULL)
		{
			perror("realloc fail:");
				return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;

	}
	//尾插
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);//只少有一个元素,才能删除
	ps->top--;
}

//栈的有效个数和栈顶元素
int  StackSize(Stack* ps)
{
	assert(ps);
	return ps->top;
}
int   StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}

//栈是否为空
bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}

二、队列

2.1****队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out)的原则。

入队列进行插入操作的一端称为队尾

出队列进行删除操作的一端称为队头

2.2 队列的实现(单链表队列)

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

2.2.1 队列的基本功能实现

cs 复制代码
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<assert.h>
typedef int QDateType;
typedef struct QueueNode
{
	QDateType val;
	struct QueueNode * next;
}QueueNode;
typedef struct Queue
{
	QueueNode *head;
	QueueNode *tail;
	QDateType size;
}Queue;
// 初始化队列
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);

// 队尾入列和出列
void QueuePush(Queue* pq, QDateType x);
void QueuePop(Queue* pq);


// 返回队头和队尾
QDateType QueueFront(Queue* pq);
QDateType QueueBack(Queue* pq);

// 获取队列中有效元素个数
int QueueSize(Queue* pq);

// 检测队列是否为空
bool QueueEmpty(Queue* pq);
2.2.1.1 队列的初始化和销毁
cs 复制代码
// 初始化队列
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
//队列的销毁
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head==NULL;
}
2.2.1.2 入列和出列
cs 复制代码
//入列
void QueuePush(Queue* pq, QDateType x)
{
	assert(pq);
	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
	
	node->val = x;
	node->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = node;
		pq->size++;
	}
	else
	{
		pq->tail->next = node;
		pq->tail = node;
		pq->size++;
	}
}
//出列
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);//只少保证一个节点
	QueueNode* del = pq->head;
	pq->head = pq->head->next;
	free(del);
	del = NULL;
	pq->size--;
	if (pq->head == NULL)//只有一个节点处理
	{
		pq->tail = NULL;
	}
}
2.2.1.3 返回队头和队尾元素
cs 复制代码
// 返回队头和队尾
QDateType QueueFront(Queue* pq)
{
	assert(pq);
	return pq->head->val;
}
QDateType QueueBack(Queue* pq)
{
	assert(pq);
	return pq->tail->val;
}
2.2.1.4 队列元素个数
cs 复制代码
// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}
2.2.1.5 队列是否为空
cs 复制代码
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head==NULL;
}

2.2.2 Queue.h

cs 复制代码
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<assert.h>
typedef int QDateType;
typedef struct QueueNode
{
	QDateType val;
	struct QueueNode * next;
}QueueNode;
typedef struct Queue
{
	QueueNode *head;
	QueueNode *tail;
	QDateType size;
}Queue;
// 初始化队列
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);

// 队尾入列和出列
void QueuePush(Queue* pq, QDateType x);
void QueuePop(Queue* pq);


// 返回队头和队尾
QDateType QueueFront(Queue* pq);
QDateType QueueBack(Queue* pq);

// 获取队列中有效元素个数
int QueueSize(Queue* pq);

// 检测队列是否为空
bool QueueEmpty(Queue* pq);

2.2.1 Queue.c

cs 复制代码
#include"Queue.h"
// 初始化队列
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDateType x)
{
	assert(pq);
	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
	
	node->val = x;
	node->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = node;
		pq->size++;
	}
	else
	{
		pq->tail->next = node;
		pq->tail = node;
		pq->size++;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);//只少保证一个节点
	QueueNode* del = pq->head;
	pq->head = pq->head->next;
	free(del);
	del = NULL;
	pq->size--;
	if (pq->head == NULL)//只有一个节点处理
	{
		pq->tail = NULL;
	}
}

// 返回队头和队尾
QDateType QueueFront(Queue* pq)
{
	assert(pq);
	return pq->head->val;
}
QDateType QueueBack(Queue* pq)
{
	assert(pq);
	return pq->tail->val;
}

// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}



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

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;

}
相关推荐
OxYGC24 分钟前
[玩转GoLang] 5分钟整合Gin / Gorm框架入门
开发语言·golang·gin
锐策27 分钟前
Lua 核心知识点详解
开发语言·lua
TNTLWT38 分钟前
单例模式(C++)
javascript·c++·单例模式
水饺编程44 分钟前
Windows 命令行:cd 命令3,当前目录,父目录,根目录
c语言·c++·windows·visual studio
kyle~44 分钟前
C/C++---动态内存管理(new delete)
c语言·开发语言·c++
m0_552200821 小时前
《UE5_C++多人TPS完整教程》学习笔记49 ——《P50 应用瞄准偏移(Applying Aim Offset)》
c++·游戏·ue5
m0_552200821 小时前
《UE5_C++多人TPS完整教程》学习笔记50 ——《P51 多人游戏中的俯仰角(Pitch in Multiplayer)》
c++·游戏·ue5
落日沉溺于海1 小时前
React From表单使用Formik和yup进行校验
开发语言·前端·javascript
songx_992 小时前
leetcode10(跳跃游戏 II)
数据结构·算法·leetcode
Yuki’2 小时前
网络编程---UDP
c语言·网络·网络协议·udp