【数据结构】实现栈和队列

一、栈

1.栈的概念及结构

(1)栈的概念

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

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶

(2)栈的结构

在这里插入图片描述

2.栈的实现

(1)类型和函数的声明

栈的结构与顺序表相同,也是用数组。因为栈的特点是出栈、入栈在同一位置,所以用数组尾插更方便。

typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType* data;
	int top;
	int capacity;
}ST;
//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);
//入栈
void STPush(ST* ps, STDataType x);
//出栈
void STPop(ST* ps);
//检查是否为空
bool STEmpty(ST* ps);
//获取栈的元素个数
int STSize(ST* ps);
//获取栈顶元素
STDataType STTop(ST* ps);

(2)初始化栈

void STInit(ST* ps)
{
    
    
	assert(ps);
	ps->data = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

(3)销毁

void STDestroy(ST* ps)
{
    
    
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

(4)入栈

void STPush(ST* ps, STDataType x)
{
    
    
	assert(ps);
	if (ps->top == ps->capacity)
	{
    
    
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			exit(-1);
		}
		ps->data = tmp;
		ps->capacity = newcapacity;
	}
	ps->data[ps->top] = x;
	ps->top++;
}

(5)出栈

void STPop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}

(6)检查是否为空

bool STEmpty(ST* ps)
{
    
    
	assert(ps);
	return ps->top == 0;
}

(7)获取栈的元素个数

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

(8)获取栈顶元素

STDataType STTop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	return ps->data[ps->top - 1];
}

二、栈的全部代码

1.Stack.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType* data;
	int top;
	int capacity;
}ST;
//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);
//入栈
void STPush(ST* ps, STDataType x);
//出栈
void STPop(ST* ps);
//检查是否为空
bool STEmpty(ST* ps);
//获取栈的元素个数
int STSize(ST* ps);
//获取栈顶元素
STDataType STTop(ST* ps);

2Stack.c

#include "Stack.h"
//初始化
void STInit(ST* ps)
{
    
    
	assert(ps);
	ps->data = NULL;
	ps->capacity = 0;
	ps->top = 0;
}
//销毁
void STDestroy(ST* ps)
{
    
    
	assert(ps);
	free(ps->data);
	ps->data = NULL;
	ps->capacity = 0;
	ps->top = 0;
}
//入栈
void STPush(ST* ps, STDataType x)
{
    
    
	assert(ps);
	if (ps->top == ps->capacity)
	{
    
    
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			exit(-1);
		}
		ps->data = tmp;
		ps->capacity = newcapacity;
	}
	ps->data[ps->top] = x;
	ps->top++;
}
//出栈
void STPop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
//检查是否为空
bool STEmpty(ST* ps)
{
    
    
	assert(ps);
	return ps->top == 0;
}
//获取栈的元素个数
int STSize(ST* ps)
{
    
    
	assert(ps);
	return ps->top;
}
//获取栈顶元素
STDataType STTop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	return ps->data[ps->top - 1];
}

3.Test.c

#include "Stack.h"
void test()
{
    
    
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	STPush(&st, 5);
	while (!STEmpty(&st))
	{
    
    
		printf("%d ", STTop(&st));
		STPop(&st);
	}

	STDestroy(&st);
}
int main()
{
    
    
	test();
	return 0;
}

在这里插入图片描述

三、队列

1.队列的概念及结构

(1)队列的概念

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

(2)队列的结构

在这里插入图片描述

2.队列的实现

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

(1)类型和函数的声明

队列除了节点的结构体以外,还要再创建一个结构体,方便找到尾指针。

typedef int QDataType;
typedef struct QueueNode
{
    
    
	QDataType data;
	struct QueueNode* next;
}QNode;
typedef struct Queue
{
    
    
	QNode* head;
	QNode* tail;
	int size;
}Que;
//初始化
void QueInit(Que* pq);
//销毁
void QueDestroy(Que* pq);
//入队
void QuePush(Que* pq, QDataType x);
//出队
void QuePop(Que* pq);
//获取头部元素
QDataType QueFront(Que* pq);
//获取队尾元素
QDataType QueBack(Que* pq);
//获取元素个数
int QueSize(Que* pq);
//检查是否为空
bool QueEmpty(Que* pq);

(2)初始化队列

void QueInit(Que* pq)
{
    
    
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

(3)销毁

void QueDestroy(Que* pq)
{
    
    
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
    
    
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

(4)入队

入队相当于尾插,因为只有一个入口插入节点,所以直接在这个函数创建一个新节点。分两种情况:刚开始没有节点尾插、已有节点再尾插。

void QuePush(Que* pq, QDataType x)
{
    
    
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
    
    
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
    
    
		pq->head = pq->tail = newnode;
	}
	else
	{
    
    
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

(5)出队

出队相当于头删。如果没有节点,就不能再删了,所以要断言检查是否为空。这里的头删也要分两种情况:只有一个节点、一个以上的节点。

void QuePop(Que* pq)
{
    
    
	assert(pq);
	assert(!QueEmpty(pq));
	if (pq->head->next == NULL)
	{
    
    
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
    
    
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	pq->size--;
}

(6)获取头部元素

QDataType QueFront(Que* pq)
{
    
    
	assert(pq);
	assert(!QueEmpty(pq));
	return pq->head->data;
}

(7)获取队尾元素

QDataType QueBack(Que* pq)
{
    
    
	assert(pq);
	assert(!QueEmpty(pq));
	return pq->tail->data;
}

(8)获取元素个数

int QueSize(Que* pq)
{
    
    
	assert(pq);
	return pq->size;
}

(9)检查是否为空

bool QueEmpty(Que* pq)
{
    
    
	assert(pq);
	return pq->head == NULL;
}

四、队列的全部代码

1.Queue.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
    
    
	QDataType data;
	struct QueueNode* next;
}QNode;
typedef struct Queue
{
    
    
	QNode* head;
	QNode* tail;
	int size;
}Que;
//初始化
void QueInit(Que* pq);
//销毁
void QueDestroy(Que* pq);
//入队
void QuePush(Que* pq, QDataType x);
//出队
void QuePop(Que* pq);
//获取头部元素
QDataType QueFront(Que* pq);
//获取队尾元素
QDataType QueBack(Que* pq);
//获取元素个数
int QueSize(Que* pq);
//检查是否为空
bool QueEmpty(Que* pq);

2.Queue.c

#include "Queue.h"
//初始化
void QueInit(Que* pq)
{
    
    
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}
//销毁
void QueDestroy(Que* pq)
{
    
    
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
    
    
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
//入队
void QuePush(Que* pq, QDataType x)
{
    
    
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
    
    
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
    
    
		pq->head = pq->tail = newnode;
	}
	else
	{
    
    
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}
//出队
void QuePop(Que* pq)
{
    
    
	assert(pq);
	assert(!QueEmpty(pq));
	if (pq->head->next == NULL)
	{
    
    
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
    
    
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	pq->size--;
}
//获取头部元素
QDataType QueFront(Que* pq)
{
    
    
	assert(pq);
	assert(!QueEmpty(pq));
	return pq->head->data;
}
//获取队尾元素
QDataType QueBack(Que* pq)
{
    
    
	assert(pq);
	assert(!QueEmpty(pq));
	return pq->tail->data;
}
//获取元素个数
int QueSize(Que* pq)
{
    
    
	assert(pq);
	return pq->size;
}
//检查是否为空
bool QueEmpty(Que* pq)
{
    
    
	assert(pq);
	return pq->head == NULL;
}

3.Test.c

#include "Queue.h"
void test()
{
    
    
	Que q;
	QueInit(&q);
	QuePush(&q, 1);
	QuePush(&q, 2);
	QuePush(&q, 3);
	QuePush(&q, 4);
	QuePush(&q, 5);
	while (!QueEmpty(&q))
	{
    
    
		printf("%d ", QueFront(&q));
		QuePop(&q);
	}
	printf("\n");
	QueDestroy(&q);
}
int main()
{
    
    
	test();
	return 0;
}

在这里插入图片描述
在这里插入图片描述
感谢观看~

猜你喜欢

转载自blog.csdn.net/2301_77459845/article/details/132451455