C语言数据结构-----栈和队列(概念,代码实现及简单练习)

前言

本篇主要介绍栈和队列的相关知识,练习以及代码实现。
代码主要展示部分功能的实现。完整代码在gitee上查看。
链接: 栈和队列的完整代码实现


1.栈

1.1 栈的概念及结构

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

在这里插入图片描述

1.2 栈的实现

栈的实现有两种方式
在这里插入图片描述

1.3 栈的代码实现

1.3.1 栈的初始化

void STInit(ST* pst)
{
    
    
	assert(pst);

	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;//top是什么?0?
}

在这里插入图片描述
所以有两种设计方案:
在这里插入图片描述

我们采用top=0的方案!

1.3.2 栈顶插入

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

1.3.3 栈顶删除

void STPop(ST* pst)
{
    
    
	assert(pst);
	// top为0不能减减
	assert(pst->top > 0);
	pst->top--;
}

1.3.4 返回栈顶数据

STDataType STTop(ST* pst)
{
    
    
	assert(pst);
	// 不为空
	assert(pst->top > 0);
	return pst->a[pst->top - 1];//表达式应该是(pst->top) - 1,表示栈顶元素的前一个元素
}

1.3.5 判断栈是否为空

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

	if (pst->top == 0)
		return true;
	else
		return false;
}

1.3.6 获取栈中有效元素个数

int STSize(ST* pst)
{
    
    
	assert(pst);
	return pst->top;
}

1.3.7 销毁栈

void STDestroy(ST* pst)
{
    
    
	assert(pst);

	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}

1.3.8 主函数测试

int main()
{
    
    
	ST s;
	STInit(&s);
	STPush(&s, 1);
	STPush(&s, 2);
	STPush(&s, 3);
	printf("%d ", STTop(&s));
	STPop(&s);
	printf("%d ", STTop(&s));
	STPop(&s);
	STPush(&s, 4);
	STPush(&s, 5);
	//    一     对     多
	// 入栈顺序  --  出栈顺序
	while (!STEmpty(&s))
	{
    
    
		printf("%d ", STTop(&s));
		STPop(&s);
	}
	printf("\n");

	return 0;
}

后进先出是相对的,具体要看代码
入栈数据只有一种,出栈顺序可以是多种。

1.4 栈的练习题

在这里插入图片描述

2.队列

2.1 队列的概念及结构

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

在这里插入图片描述
在这里插入图片描述

2.2 队列的实现

在这里插入图片描述

2.3 队列的代码实现

2.3.1 队列的初始化

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

2.3.2 队列的尾插

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

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	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++;
}

2.3.3 队列的头删

void QueuePop(Queue* pq)
{
    
    
	assert(pq);
	assert(pq->phead);
	QNode* del = pq->phead;
	pq->phead = pq->phead->next;
	free(del);
	del = NULL;
	if (pq->phead == NULL)
		pq->ptail = NULL;

	pq->size--;
}

在这里插入图片描述

2.3.4 获取队列头部元素

QDataType QueueFront(Queue* pq)
{
    
    
	assert(pq);
	assert(pq->phead);

	return pq->phead->val;
}

2.3.5 获取队列尾部元素

QDataType QueueBack(Queue* pq)
{
    
    
	assert(pq);
	assert(pq->ptail);

	return pq->ptail->val;
}

2.3.6 检测队列是否为空

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

2.3.7 获取队列中有效元素个数

int QueueSize(Queue* pq)
{
    
    
	assert(pq);
	return pq->size;
}

2.3.8 摧毁

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;
}

2.3.9 主函数测试

int main()
{
    
    
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	printf("%d ", QueueFront(&q));
	QueuePop(&q);
	printf("%d ", QueueFront(&q));
	QueuePop(&q);

	QueuePush(&q, 4);
	QueuePush(&q, 5);
	while (!QueueEmpty(&q))
	{
    
    
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}

	QueueDestroy(&q);
	return 0;
}

2.4 队列的练习题

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_57425280/article/details/134374957
今日推荐