数据结构 —— 栈和队列的概念、结构及实现。

一、栈的概念及结构

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

二、栈的实现

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

下面是代码的实现,依然是建 3 个文件来实现栈的功能,如下:

Stack.h 文件: 

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
 
 
// // 静态
//#define N 10
//typedef int STDataType;
//typedef struct Stack
//{
//	STDataType Data[N];
//	int Top; // 标识栈顶的位置
//}ST;
 
// 动态
typedef int STDataType;
typedef struct Stack
{
	STDataType* Data;
	int Top; // 标识栈顶的位置
	int Capacity; // 栈的容量
}ST;
 
// 初始化
void StackInit(ST* ps);
 
// 销毁
void StackDestroy(ST* ps);
 
// 插入\进栈\入栈
void StackPush(ST* ps, STDataType Data);
 
// 出栈
void StackPop(ST* ps);
 
// 取栈顶的元素
STDataType StackTop(ST* ps);
 
// 判断栈是否为空
bool StackEmpty(ST* ps);
 
// 计算栈里面的数据
int StackSize(ST* ps);

Stack.c 文件: 

#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
 
// 初始化
void StackInit(ST* ps)
{
	assert(ps);
 
	ps->Data = NULL;
	ps->Top = 0;
	ps->Capacity = 0;
}
 
// 判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
 
	//if (ps->Top > 0)
	//{
	//	return false;
	//}
	//else
	//{
	//	return true;
	//}
 
	return ps->Top == 0;
}
 
// 插入\进栈\入栈
void StackPush(ST* ps, STDataType Data)
{
	assert(ps);
	
	if (ps->Top == ps->Capacity)
	{
		int NewCapacity = ps->Capacity == 0 ? 4 : ps->Capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->Data, sizeof(STDataType) * NewCapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
 
		ps->Data = tmp;
		ps->Capacity = NewCapacity;
	}
 
	ps->Data[ps->Top] = Data;
-	ps->Top++;
}
 
// 出栈
void StackPop(ST* ps)
{
	assert(ps);
 
	// 栈为空不能再删
	//assert(ps->Top > 0);
	assert(!StackEmpty(ps));
 
	ps->Top--;
}
 
// 取栈顶的元素
STDataType StackTop(ST* ps)
{
	assert(ps);
 
	assert(!StackEmpty(ps));
	return ps->Data[ps->Top - 1]; // Top 初始值是 0,记录的是栈顶位置(栈顶元素+1位置)
}
 
// 计算栈里面的数据
int StackSize(ST* ps)
{
	assert(ps);
 
	return ps->Top; // Top 初始值是 0,记录的是栈顶位置(最后一个元素的下一个位置=>元素个数)
}
 
// 销毁
void StackDestroy(ST* ps)
{
	assert(ps);
 
	free(ps->Data);
 
	ps->Data = NULL;
	ps->Top = ps->Capacity = 0;
}

StackTest.c 文件: 

#define _CRT_SECURE_NO_WARNINGS 1
 
#include "Stack.h"
 
void Test1()
{
	ST ps;
	StackInit(&ps);
	StackPush(&ps, 1);
	StackPush(&ps, 2);
	StackPush(&ps, 3);
	StackPush(&ps, 4);
	StackPush(&ps, 5);
 
	while (!StackEmpty(&ps))
	{
		printf("%d ", StackTop(&ps));
		StackPop(&ps);
	}
	printf("\n");
 
	StackDestroy(&ps);
}
 
int main()
{
	Test1();
 
	return 0;
}

 三、队列的概念及结构

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

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

动图如下:

四、队列的实现

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

 下面是代码的实现,依然是建 3 个文件来实现队列的功能,如下: 

Queue.h 文件

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

typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType Data;
}QNode;

typedef struct Queue
{
	QNode* head; // 头位置
	QNode* tail; // 尾位置
	//int Size; // 在结构体里面定义 Size 入队\出队,改变 Size
}Queue;

// 初始化
void QueueInit(Queue* PQ);

// 检查
bool QueueEmpty(Queue* PQ);

// 入队
void QueuePush(Queue* PQ, QDataType Data);

// 出队
void QueuePop(Queue* PQ);

// 队列第一个数据
QDataType QueueFront(Queue* PQ);

// 队列最后一个数据
QDataType QueueBack(Queue* PQ);

// 计算个数
int QueueSize(Queue* PQ);

// 销毁释放
void QueueDestroy(Queue* PQ);

Queue.c 文件

#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"


// 初始化
void QueueInit(Queue* PQ)
{
	assert(PQ);

	PQ->head = PQ->tail = NULL;
}

// 检查是否为 NULL
bool QueueEmpty(Queue* PQ)
{
	assert(PQ);

	return PQ->head == NULL; // 判断 head 或者 tail 是否为 NULL 都可以
}

// 入队
void QueuePush(Queue* PQ, QDataType Data)
{
	assert(PQ);

	QNode* Newnode = (QNode*)malloc(sizeof(QNode)); // malloc 的是结点(QNode),类型千万别弄错
	if (Newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}

	Newnode->Data = Data;
	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(!QueueEmpty(PQ));

	// 如果只剩最后一个结点,此时 head、tail 的 next 都是 NULL, 再删的话 tail 就成野指针了
	// assert(PQ->head->next); 
	if (PQ->head->next == NULL)
	{
		free(PQ->head);
		PQ->head = PQ->tail = NULL;
	}
	else
	{
		QNode* next = PQ->head->next; // next 的类型也是 QNode* 
		free(PQ->head);

		PQ->head = next;
	}
}

// 队头的数据
QDataType QueueFront(Queue* PQ)
{
	assert(PQ);
	assert(!QueueEmpty(PQ));

	return PQ->head->Data;
}

// 队尾的数据
QDataType QueueBack(Queue* PQ)
{
	assert(PQ);
	assert(!QueueEmpty(PQ));

	return PQ->tail->Data;
}

// 计算个数 
int QueueSize(Queue* PQ)
{
	assert(PQ);

	int temp = 0;
	QNode* cur = PQ->head; // 定义指针 cur 遍历队列,类型也是 QNode*
	while (cur)
	{
		temp++;
		cur = cur->next;
	}
	return temp;
}

// 销毁释放
void QueueDestroy(Queue* PQ)
{
	assert(PQ);

	QNode* cur = PQ->head;
	while (cur)
	{
		QNode* next = PQ->head->next;
		free(cur);
		cur = next;
	}
	PQ->head = PQ->tail = NULL;
}

QueueTest.c 文件 

#define _CRT_SECURE_NO_WARNINGS 1

#include "Queue.h"


void Test1()
{
	Queue pq;
	QueueInit(&pq);
	
	QueuePush(&pq, 1);
	QueuePush(&pq, 2);
	QueuePush(&pq, 3);
	QueuePush(&pq, 4);
	QueuePush(&pq, 5);
	
	while (!QueueEmpty(&pq))
	{
		printf("%d ", QueueFront(&pq));
		QueuePop(&pq);
	}

	printf("\n");
	QueueDestroy(&pq);
}

int main()
{
	Test1();
	return 0;
}

以上就是 栈和队列的概念、结构及其实现,我在 栈 和 队列 的测试文件里面,我全部写好去简单测试了一下,建议每写一个功能去测试运行一下,有错误可以及时改。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值