[Data structure] C language implements stack and queue

Table of contents

1. Stack

1. Concept and structure of stack

 2. How to implement the stack

 3. Code implementation

3.1 Definition of stack

3.2 Functions to be implemented in the stack

3.3 Function implementation

2. Queue

1. The concept and structure of queue

 2. How to implement queue

3. Code implementation 

3.1 Queue definition

3.2 Functions to be implemented in the queue

3.3 Function implementation


1. Stack

1. Concept and structure of stack

Stack: A special linear list that only allows insertion and deletion of elements at a fixed end. One end where data insertion and deletion operations are performed is called the top of the stack, and the other end is called the bottom of the stack. The elements in the stack follow the LIFO (Last In First Out) principle.

Pushing the stack: The insertion operation of the stack is called push/push/push, and the inserted data is on the top of the stack.

Pop off the stack: The deletion operation of the stack is called popping off the stack. The outgoing data is also on the top of the stack.

 2. How to implement the stack

As a type of linear table, the stack can be implemented in two ways. One is a sequential structure implemented based on an array, and the other is implemented based on a linked list. Relatively speaking, we prefer to use arrays when implementing stack structures . Array structures are better to implement because the cost of inserting data at the end of arrays is relatively small.

 3. Code implementation

3.1 Definition of stack

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int STDataType;
typedef struct Stack
{
	STDataType* data; //动态数组实现
	int size;         //栈中有效数据个数
	int capacity;     //栈的容量
}ST;

3.2 Functions to be implemented in the stack

//初始化队列
void StackInit(ST* ps);

// 入栈
void StackPush(ST* ps, STDataType x);

// 出栈
void StackPop(ST* ps);

// 获取栈顶元素
STDataType StackTop(ST* ps);

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

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

// 销毁栈
void StackDestroy(ST* ps);

3.3 Function implementation

//栈的初始化
void StackInit(ST* ps)
{
	assert(ps);
	int newcapacity = 4; //初识容量设置为4
	STDataType* a = (STDataType*)malloc(sizeof(STDataType) * newcapacity);
	if (a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->data = a;
	ps->size = 0;//初识化为零,代表的是栈顶元素的下一个位置,同时也表示栈内的元素个数
	ps->capacity = newcapacity;
}

//压栈操作,将元素压入栈中
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
        //如果栈中元素有效个数已经达到容量大小,就进行扩容
		STDataType* a = (STDataType*)realloc(ps->data, 2 * ps->capacity * sizeof(STDataType));
		if (a == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->data = a;
		ps->capacity *= 2;
	}
	ps->data[ps->size] = x;
	ps->size++;
}

//出栈,将栈顶元素弹出
void StackPop(ST* ps)
{
	assert(ps);
    //防止栈中没有元素仍进行出栈操作
    //如果对空栈进行出栈,下一次进栈就会造成越界问题
	assert(ps->size > 0);
	ps->size--;
}

//取出栈顶元素
STDataType StackTop(ST* ps)
{
	assert(ps);
	return ps->data[ps->size - 1];
}

//返回栈中元素个数
int StackSize(ST* ps)
{
	assert(ps);
	return ps->size;
}

//判断栈是否为空栈
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->size == 0;
}

//销毁栈
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->capacity = ps->size = 0;
}

2. Queue

1. The concept and structure of queue

Queue: A special linear list that only allows data insertion operations at one end and deletion data operations at the other end. The elements in the queue follow the First In First Out (First In First Out) principle.

Entering the queue: The end where the insertion operation is performed is called the tail of the queue.

Dequeue: The end that performs the deletion operation is called the head of the queue.

 

 2. How to implement queue

Queues can also be implemented using the structure of arrays and linked lists. It is better to use the structure of linked lists, because if you use the structure of an array, the data will be output from the queue at the head of the array. After the data is output, the data needs to be moved, which will be less efficient.

3. Code implementation 

3.1 Queue definition

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int QDataType;
// 链式结构:表示队列 
typedef struct QListNode
{
	struct QListNode* next;
	QDataType data;
}QNode;

// 队列的结构 
typedef struct Queue
{
	QNode* front; //队头
	QNode* rear;  //队尾
	int size;     //队列元素个数
}Queue;

3.2 Functions to be implemented in the queue

// 初始化队列 
void QueueInit(Queue* pq);

// 队尾入队列
void QueuePush(Queue* pq, QDataType data);

// 队头出队列
void QueuePop(Queue* pq);

// 获取队列头部元素
QDataType QueueFront(Queue* pq);

// 获取队列队尾元素
QDataType QueueBack(Queue* pq);

// 获取队列中有效元素个数
int QueueSize(Queue* pq);

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* pq);

// 销毁队列
void QueueDestroy(Queue* pq);

3.3 Function implementation

#include "Queue.h"
//初识化队列
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->front = pq->rear = NULL;
	pq->size = 0;
}

//入队列
void QueuePush(Queue* pq, QDataType data)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = data;
	newnode->next = NULL;
	if (pq->front == NULL)
	{
		pq->front = pq->rear = newnode;
	}
	else
	{
		pq->rear->next = newnode;
		pq->rear = newnode;
	}
	pq->size++;
}

//出队列
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->front->next == NULL)
	{
		free(pq->front);
		pq->front = pq->rear = NULL;
	}
	else
	{
		QNode* next = pq->front->next;
		free(pq->front);
		pq->front = next;
	}
	pq->size--;
}

//取队头元素
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->front->data;
}

//取队尾元素
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->rear->data;
}

//返回队列中元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

//判断队列是否为空
int QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->front == NULL && pq->rear == NULL;
}

//销毁队列
void QueueDestroy(Queue* pq)
{
	assert(pq);
	assert(pq->size > 0);
	QNode* cur = pq->front;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->size = 0;
	pq->front = pq->rear = NULL;
}

The above is the stack and queue implemented in C language. I hope it can help everyone. If there is something wrong, please correct me in the comment area.
 

 

Guess you like

Origin blog.csdn.net/m0_74459723/article/details/128531401