栈和队列详解(C语言版)

一、栈

1.栈的定义和结构

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

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

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

2.栈的实现 

栈和线性表类似,也有两种存储表示方法数组和链表。相对而言数组的结构更优一点!由于栈在使用的过程中所需要的大小难以估计,所以通常是先为栈分配一个基本容量,然后再使用的过程中,当栈的空间不够使用的时候再继续追加存储空间。所以接下来的说明是使用数组实现动态增长的栈的示例。

2.1定义结构体

typedef int STDataType;//方便后续修改类型数据
typedef struct Stack
{
	STDataType* a;
	int top;//栈顶
	int capacity;//容量
}ST;

2.2初始化和销毁

因为这里初始化top = 0 所以这里的top指向的是栈顶元素的下一个位置,这里需要注意一下!!

那我们这里使用top = 0好处是什么呢!top即存储数据的个数!

因为当新增了数据top++,就如图所示:

void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;//这里使用0的话就是顶元素的下一个位置,获取顶元素需要-1
}
void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}

 top = 0 意味着栈顶元素的下一个位置

top = -1 意味着top即栈顶元素 

 

2.3入栈

这里我们需要注意一下!因为我们需要判断栈是否满了,如果满了需要开辟空间

这里没有malloc就使用realloc,为什么呢?因为如果需要调整的位置为0,就相当于malloc!

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

2.4出栈

这里需要注意一下栈是否为空,若是为空则不能在删除

void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);//没有东西就别删了
	ps->top--;//直接访问不到此下标
}

2.5获取栈顶元素

这里要注意top指向的是栈顶元素的下一个位置,所以下标需要是top-1

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

2.6返回元素个数

top即元素个数,直接返回即可

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

2.7判断栈是否为空

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;//真:空的话返回1;假不空返回0
}

 2.8完整代码

 #include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
 
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
void STInit(ST* ps);
void STDestroy(ST* ps);

void STPush(ST* ps, STDataType x);
void STPop(ST* ps);
STDataType STTop(ST* ps);

STDataType STSize(ST* ps);

bool STEmpty(ST* ps);


void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;//这里使用0的话就是顶元素的下一个位置,获取顶元素需要-1
}
void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc is failed");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void STPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);//没有东西就别删了
	ps->top--;//直接访问不到此下标
}

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

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;//真:空的话返回1;假不空返回0
}

二、队列

1.队列的定义及结构

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

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

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

结构图:

2.队列的实现

2.1结构体定义

栈和线性表类似,也有两种存储表示方法数组和链表。相对而言数组的结构更优一点!

这里使用的是单向不带头不循环链表实现

这里定义两个指针方便修改,否则需要传二级指针去修改结构体指针

typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;
//定义两个结构体是为了方便修改,否则要传二级指针进去修改结构体指针

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Que;

两个结构体之间的关系 

 

 

 

2.2初始化

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

2.3入队

首先判断是否为空指针

判断是否满了,需要开新空间

void QueuePush(Que* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc is failed");
		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++;
}

解释图

2.4出队列 

要判断是不是空指针传入,其次要判断此链表是不是为空,若为空则报错

还要区分只有一个节点和很多节点的区别

void QueuePop(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(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--;
}

解释图 

2.5获取队头和队尾数据 

首先判断是否传入指针为空,直接返回头尾指针指数据即可

QDataType QueueFront(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}
QDataType QueueBack(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;

}

 2.6判断队列是否为空

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

2.7计算节点个数

直接返回size

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

2.8销毁队列

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

2.9全部代码

#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;
}Que;

void QueueInit(Que* pq);
void QueueDestroy(Que* pq);
void QueuePush(Que* pq, QDataType x);//相当于尾删
void QueuePop(Que* pq);//相当于头删
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);
QDataType QueueFront(Que* pq);
QDataType QueueBack(Que* pq);


#include "Queue.h"
void QueueInit(Que* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
void QueueDestroy(Que* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->size = 0;
	pq->head = pq->tail = NULL;
}
void QueuePush(Que* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc is failed");
		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 QueuePop(Que* pq)//相当于头删
{
	assert(pq);
	assert(!QueueEmpty(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 QueueFront(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}
QDataType QueueBack(Que* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;

}
bool QueueEmpty(Que* pq)
{
	assert(pq);
	return pq->head == NULL;
}
int QueueSize(Que* pq)
{
	assert(pq);
	return  pq->size;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值