(数据结构)栈和队列的实现

一、栈

1.栈的概念

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

2.栈的物理图解

在这里插入图片描述

在这里插入图片描述

3.栈的代码实现

3.1 栈的结构

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;//栈顶
	int capacity;//栈的容量
}ST;

3.2 接口

void STInit(ST* pst);//初始化栈
void STDestroy(ST* pst);//销毁栈
void STPush(ST* pst, STDataType x);//入栈
void STPop(ST* pst);//出栈
STDataType STTop(ST* pst);//获取栈顶元素
bool STEmpty(ST* pst);//判断栈是否为空
int STSize(ST* pst);//获取栈中有效元素个数

3.3 初始化

void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
  //pst->top = -1  //top指向栈顶数据
	pst->top = 0;  // top 指向栈顶数据的下一个位置
	pst->capacity = 0;
}

3.4 入栈

void STPush(ST* pst,STDataType x)
{
	assert(pst);
	if (pst->top == pst->capacity)
	{   
		//若原内存大小为0,则新内存大小赋值为4,不为0则加倍
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = realloc(pst->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newCapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

3.5 出栈

void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));//断言栈不为空

	pst->top--;
}

3.6 获取栈顶元素

STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));//断言栈不为空

	return pst->a[pst->top-1];
	//由于top初始化为0,故这里要top-1才能获取栈顶元素
}

3.7 判断栈是否为空

bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
	//在STInit函数中top初始化为0,若初始化为-1,则这里条件改为pst->top == -1
}

3.8 获取栈中有效元素个数

int STSize(ST* pst)
{
	assert(pst);

	return pst->top;
	//若top初始化为-1,则这里的需写成top+1
}

3.9 销毁栈

void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);

	pst->a = NULL;//防止出现野指针
	pst->top = pst->capacity = 0;
}

二、队列

1.队列的概念

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

入队列:进行插入操作的一端称为队尾 (tail)
出队列:进行删除操作的一端称为队头(head)
在这里插入图片描述

2. 队列的物理图解

在这里插入图片描述

3. 队列的代码实现

3.1 队列的结构

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

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

3.2 接口

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);//判断队列是否为空

3.3 队列的初始化

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

3.4 入队

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

	QNode* newnode = 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++;
}

3.5 出队

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

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

3.6 获取队列头部元素

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

	return pq->phead->data;
}

3.7 获取队列尾部元素

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

	return pq->ptail->data;
}

3.8 获取队列中有效元素的个数

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

	return pq->size;
}

3.9 判断队列是否为空

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

	return pq->phead == NULL
		&& pq->ptail == NULL;
}

3.10 销毁队列

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值