Data structure-stack (Stack) and queue (Queue)

Stack and queue

1. The definition of stack

The stack is a linear table limited to insert and delete operations only at the end of the table.

We call the end that allows data insertion and deletion as the top of the stack, the other end is called the bottom of the stack, and the stack without any data elements is called the empty stack. Stack is also called Last In First Out (Last In First Out) linear table.

1.1 The sequential storage structure of the stack

The implementation of the stack can generally be implemented using an array or a linked list, and the structure of the array is relatively better. Because the cost of inserting data on the end of the array is relatively small.

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

1.2 Sequential stack based on dynamic array implementation

//初始化
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. Definition of queue

A queue is a linear table that only allows insert operations at one end and delete operations at the other end.

The queue is a first-in-first-out (First In First Out) linear table. A segment allowed to be inserted is called the end of the queue, and the end allowed to be deleted is called the head of the queue.

2.1 Chained storage structure of queue

Queues can also be implemented with the structure of arrays and linked lists. It is better to use the structure of linked lists, because if the structure of an array is used, the efficiency of outputting the queue at the head of the array will be lower.

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

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

2.2 Chain queue based on singly linked list

//初始化队列
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;
}

Guess you like

Origin blog.csdn.net/weixin_50843868/article/details/112308406