数据结构——栈(Stack)和队列(Queue)

栈和队列

1.栈的定义

栈(stack)是限定仅在表尾进行插入和删除操作的线性表。

我们把允许数据插入和删除的一端称为栈顶(top),另一端称为栈底(bottom),不含任何数据元素的栈称为空栈。栈又称为后进先出(Last In First Out)的线性表。

1.1栈的顺序存储结构

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。

//支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType* _a;
	int _top; //栈顶下标
	int _capacity;//容量
}Stack;

1.2基于动态数组实现的顺序栈

//初始化
void StackInit(Stack* pst)
{
    
    
	assert(pst);
	pst->_a = malloc(sizeof(STDataType) * 4);
	pst->_top = 0;
	pst->_capacity = 4;
}

//销毁
void StackDestory(Stack* pst)
{
    
    
	assert(pst);
	free(pst->_a);
	pst->_a = NULL;
	pst->_top = pst->_capacity = 0;
}

//入栈
void StackPush(Stack* pst, STDataType x)
{
    
    
	assert(pst);
	//判断栈是否满了
	if (pst->_top == pst->_capacity)
	{
    
    
		//增容
		pst->_capacity *= 2;
		STDataType* temp = realloc(pst->_a, sizeof(STDataType) * pst->_capacity);
		if (temp == NULL)
		{
    
    
			printf("内存不足\n");
			exit(-1);
		}
		else
		{
    
    
			pst->_a = temp;
		}
	}
	//数据入栈
	pst->_a[pst->_top] = x;
	pst->_top++;
}

//出栈
void StackPop(Stack* pst)
{
    
    
	assert(pst);
	assert(pst->_top > 0);

	--pst->_top;
}

//获取数据个数
int StackSize(Stack* pst)
{
    
    
	assert(pst);
	return pst->_top;
}

//返回1是空  返回0是非空
int StackEmpty(Stack* pst)
{
    
    
	assert(pst);

	//return pst->_top == 0 ? 1 : 0;
	return !pst->_top;
}

//获取栈顶的数据
STDataType StackTop(Stack* pst)
{
    
    
	assert(pst);
	assert(pst->_top > 0);
	
	return pst->_a[pst->_top - 1];
}

2.队列的定义

队列(queue)是只允许在一端进行插入操作,另一端进行删除操作的线性表。

队列是一种先进先出(First In First Out )的线性表,允许插入的一段称为队尾,允许删除的一端称为队头。

2.1队列的链式存储结构

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

//链式结构表示队列
typedef int QDataType;
typedef struct QueueNode
{
    
    
	struct QueueNode* _next;
	QDataType _data;

}QueueNode;
//队列的结构
typedef struct Queue
{
    
    
	QueueNode* _head;//队头
	QueueNode* _tail;//队尾
}Queue;

2.2基于单链表实现的链队列

//初始化队列
void QueueInit(Queue* pq)
{
    
    
	assert(pq);
	pq->_head = pq->_tail = NULL;
}

//销毁队列
void QueueDestory(Queue* pq)
{
    
    
	assert(pq);
	QueueNode* cur = pq->_head;
	while (cur)
	{
    
    
		QueueNode* next = cur->_next;
		free(cur);
		cur = next;
	}

	pq->_head = pq->_tail = NULL;
}

//队尾入队
void QueuePush(Queue* pq, QDataType x)
{
    
    
	assert(pq);
	//申请新空间
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newNode == NULL)
	{
    
    
		printf("内存不足\n");
		exit(-1);
	}

	newNode->_data = x;
	newNode->_next = NULL;
	//判空
	if (pq->_head == NULL)
	{
    
    
		pq->_head = pq->_tail = newNode;
	}
	else
	{
    
    
		//不为空插入队尾,将队尾指针重置
		pq->_tail->_next = newNode;
		pq->_tail = newNode;
	}
}

//队头出队
void QueuePop(Queue* pq)
{
    
    
	assert(pq);
	assert(pq->_head);
	//释放队头使其指向下一个结点
	QueueNode* next = pq->_head->_next;
	free(pq->_head);
	pq->_head = next;
	//判断只剩一个元素的时候
	if (pq->_head == NULL)
	{
    
    
		pq->_tail = NULL;
	}
}

//取队头元素
QDataType QueueFront(Queue* pq)
{
    
    
	assert(pq);
	assert(pq->_head);

	return pq->_head->_data;
}

//取队尾元素
QDataType QueueBack(Queue* pq)
{
    
    
	assert(pq);
	assert(pq->_tail);

	return pq->_tail->_data;
}

//返回1是空  返回0是非空
int QueueEmpty(Queue* pq)
{
    
    
	assert(pq);

	return pq->_head == NULL ? 1 : 0;
}

//获取数据个数
int QueueSize(Queue* pq)
{
    
    
	assert(pq);

	QueueNode* cur = pq->_head;
	int size = 0;//计数
	while (cur)
	{
    
    
		++size;
		cur = cur->_next;
	}

	return size;
}

猜你喜欢

转载自blog.csdn.net/weixin_50843868/article/details/112308406