理解栈和队列

概念:一种特殊的线性表,其只允许在固定的一端进行插入和删除操作元素操作。进行插入和删除操作的一段被称为栈顶,另一端称为栈底。栈中的元素遵循后进先出的原则

  • 压栈:栈的插入操作叫作进栈/压栈/入栈,入数据在栈顶        
  • 出栈:栈的删除操作叫作出栈,出数据也在栈顶

结构:

声明

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

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

void STInit(ST* pst);//初始化
void STDestroy(ST* pst);//销毁
void STPush(ST* pst, STDataType x);//入栈
void STPop(ST* pst);//出栈
STDataType STTop(ST* pst);//栈顶
int STSize(ST* pst);//存储有效数据个数
bool STEmpty(ST* pst);//判空

定义

栈的实现一般可以使用数组或者链表实现,相对来说数组的结构更优,因为数组在尾插上面代价更小,更简单

初始化

void STInit(ST* pst)//初始化
{
	assert(pst);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

销毁

void STDestroy(ST* pst)//销毁
{
	assert(pst);
	free(pst);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

入栈

void STPush(ST* pst, STDataType x)//入栈
{
	assert(pst);
	if (pst->capacity == pst->top)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* temp = (STDataType*)realloc(pst->a,sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->capacity = newcapacity;
		pst->a = temp;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

出栈

void STPop(ST* pst)//出栈
{
	assert(pst);
	assert(!STEmpty(pst));
	pst->top--;
}

栈顶

STDataType STTop(ST* pst)//栈顶
{
	assert(pst);
	return pst->a[pst->top-1];
}

存储有效数据个数

int STSize(ST* pst)//存储有效数据个数
{
	assert(pst);
	return pst->top;
}

判空

bool STEmpty(ST* pst)//判空
{
	assert(pst);
	return pst->top==0;
}

 

队列

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

结构:

声明

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

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

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QueueInit(Queue* pq);
void QueueDestroy(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);

定义

初始化

void QueueInit(Queue* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

销毁

void QueueDestroy(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

插入

需要考虑空节点和非空节点两种情况

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->ptail == NULL)
	{
		assert(pq->phead == NULL);

		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}


	pq->size++;
}

删除

考虑一个节点和多个节点情况

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	// 1、一个节点
	// 2、多个节点
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		// 头删
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}

	pq->size--;
}

找头

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->data;
}

找尾

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->data;
}

有效数据个数

int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

判空

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	/*return pq->phead == NULL
		&& pq->ptail == NULL;*/
	return pq->size == 0;
}

栈优缺点

  • 优点:操作简单,效率高
  • 缺点:只能在栈顶操作,访问其他元素需要弹出栈顶元素

队列优缺点

  • 优点:适合按顺序处理数据的场景,如任务调度
  • 缺点:无法随机访问元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值