栈和队列(手撕)

本文详细介绍了栈和队列这两种基本的数据结构,包括栈的后进先出(LIFO)特性及其数组实现中的操作,如压栈、出栈和栈顶元素获取等。同时,文中也涵盖了队列的先进先出(FIFO)特点,以及链表实现的入队、出队和队列大小的计算方法。
摘要由CSDN通过智能技术生成

目录

一、栈

栈的数组实现

 二、队列

队列的链表实现:


一、栈

栈:一种特殊的线性表,只允许在固定的衣服按进行插入和删除操作。数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中元素遵循后进先出LIFO原则

压栈:栈的插入操作(进栈/压栈/入栈)

出栈:栈的删除操作。

 

栈的数组实现

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;           //标识栈顶位置
	int capacity;

}ST;






#include"Stack.h"


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

	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;//指向栈顶元素
}


// 入栈 (动态扩容)
void StackPush(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, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;

}

// 出栈 
void StackPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);//判空
	pst->top--;
}

// 获取栈顶元素 
STDataType StackTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);//判空

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

// 获取栈中有效元素个数 
int StackSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;//空时 为真
}

// 销毁栈 
void StackDestroy(ST* pst)
{
	assert(pst);
	
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;           //标识栈顶位置
	int capacity;

}ST;






#include"Stack.h"


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

	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;//指向栈顶元素
}


// 入栈 (动态扩容)
void StackPush(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, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;

}

// 出栈 
void StackPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);//判空
	pst->top--;
}

// 获取栈顶元素 
STDataType StackTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);//判空

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

// 获取栈中有效元素个数 
int StackSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;//空时 为真
}

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

 二、队列

队列:只允许在一段进行插入,在另一端进行删除操作的特殊线性表,队列遵循先进先出FIFO原则。

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

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

队列的链表实现:

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


//减少双指针
typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;




#include"Queue.h"

//入队(尾插)
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (Queue*)malloc(sizeof(Queue));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->val = x;
	newnode->next = NULL;

	//不带哨兵位需要对空进行处理
	if (pq->ptail == NULL)
	{
		pq->ptail = pq->phead = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}

	pq->size++;
}

//出队(头删)
void QueuePop(Queue* pq)
{
	assert(pq);

	//空/
	assert(pq->phead);

	QNode* del = pq->phead;
	pq->phead = pq->phead->next;
	free(del);
	del = NULL;

	//只剩一个结点
	//phead移动到ptail后面的时候
	//防止ptail为野指针
	if (pq->phead == NULL)
	{
		pq->ptail = NULL;
	}

	pq->size--;
}
//初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = 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;
}



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

	//空
	assert(pq->phead);

	return pq->phead->val;
}

//取尾部数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);

	//空
	assert(pq->ptail);

	return pq->ptail->val;
}

//判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->phead == NULL;
}

//队的大小
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值