数据结构:栈和队列

由于栈和队列结构逻辑较为简单,此文只做简单介绍并附上代码参考

1. 栈

1.1 栈的概念

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

1.2 栈的实现

栈的实现可以使用数组和链表结构,使用数组更优,因为数组在尾上插入删除数据上代价小一些

1.2.1 Stack.h

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化
void STInit(ST* pst);
//销毁
void STDestory(ST* pst);
//入栈 出栈
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
//取栈顶元素
STDataType STTop(ST* pst);
//判空
bool STEmpty(ST* pst);
//数据个数
int STSize(ST* pst);

1.2.2 Stack.c

cpp 复制代码
#include"Stack.h"

//初始化
void STInit(ST* pst)
{
	assert(pst);

	pst->a = NULL;
	//top赋值为0表示栈顶元素的下一个元素
	pst->top = 0;
	pst->capacity = 0;
}

//销毁
void STDestory(ST* pst)
{
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}
//入栈 出栈
void STPush(ST* pst, STDataType x)
{
	assert(pst);

	//扩容
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("STPush::realloc fail");
			return;
		}
		pst->capacity = newcapacity;
		pst->a = tmp;
	}

	pst->a[pst->top] = x;
	pst->top++;
}

void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	pst->top--;
}

//取栈顶元素
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);

	return pst->a[pst->top - 1];
}

//判空
bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}

//数据个数
int STSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

1.2.3 test.c

cpp 复制代码
#include"Stack.h"

int main()
{
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	STPush(&s, 4);

	printf("%d\n", STTop(&s));
	//printf("%d\n", s.a[s.top]);  切忌这样访问 因为top不代表栈顶元素

	while (!STEmpty(&s))
	{
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	STDestory(&s);
	return 0;
}

2. 队列

2.1 队列概念

只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。进行插入操作的一端称为队尾 ,进行删除操作的一端称为队头 。队列遵循先入先出的原则(First In First Out)

2.2 队列的实现

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

2.2.1 Queue.h

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int QDataType;

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

//将头尾指针单独封装结构体 可避免使用二级指针 简化逻辑 还可减少函数参数
typedef struct Queue
{
	QueueNode* phead;
	QueueNode* ptail;
	int size;
}Queue;

//初始化
void QueueInit(Queue* pq);
//队列尾插
void QueuePush(Queue* pq, QDataType x);
//队列头删
void QueuePop(Queue* pq);
//队列数据个数
int QueueSize(Queue* pq);
//取队头数据
QDataType QueueFront(Queue* pq);
//取队尾数据
QDataType QueueBack(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
//队列销毁
void QueueDestory(Queue* pq);

////以下形式也可使用 但由于使用二级指针 较为复杂 不推荐
//void QueuePush(QueueNode** pphead, QueueNode** pptail, QDataType x);
//void QueuePop(QueueNode** pphead, QueueNode** pptail);

2.2.2 Queue.c

cpp 复制代码
#include"Queue.h"

//初始化
void QueueInit(Queue* pq)
{
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
//队列尾插
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("QueuePush::malloc fail");
		return;
	}
	newnode->next = NULL;
	newnode->val = x;
	//队列无节点
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
		pq->size++;
	}
	//队列有节点
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
		pq->size++;
	}
}
//队列头删
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->size != 0);
	//一个节点
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	//多个节点
	else
	{
		QueueNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}
//队列数据个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}
//取队头数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);

	return pq->phead->val;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);

	return pq->ptail->val;
}
//判空
bool QueueEmpty(Queue* pq)
{
	return pq->size == 0;
}
//队列销毁
void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->phead;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

2.2.3 test.c

cpp 复制代码
#include"Queue.h"

int main()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

	//QueuePop(&q);
	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	return 0;
}
相关推荐
渡我白衣3 小时前
list 与 forward_list:一场 STL 中的“链表哲学”之争
数据结构·c++·list
Pocker_Spades_A8 小时前
【C语言数据结构】第2章:线性表(2)--线性表的顺序存储结构
c语言·数据结构
小许学java12 小时前
七大排序算法的基本原理
数据结构·算法·排序算法
_dindong14 小时前
动规:01背包
数据结构·笔记·学习·算法·leetcode·动态规划·力扣
-雷阵雨-14 小时前
数据结构——排序算法全解析(入门到精通)
java·开发语言·数据结构·排序算法·intellij-idea
LGL6030A15 小时前
数据结构学习(1)——指针、结构体、链表(C语言)
数据结构·学习
蒙奇D索大15 小时前
【数据结构】考研算法精讲:分块查找的深度剖析 | 从“块内无序、块间有序”思想到ASL性能最优解
数据结构·笔记·学习·考研·改行学it
lingchen190617 小时前
b = [1 2 3;4 5 6;7 8 9]>> b(2,2)=[ ]??? Subscripted assignme
数据结构·算法
Vect__18 小时前
二叉树实战笔记:结构、遍历、接口与 OJ 实战
数据结构·c++·算法