【初阶数据结构】栈和队列——C语言(详解)

目录

一、栈

1.1栈的概念及结构

1.2栈的实现

 1.2.1静态栈的实现

1.3动态栈的实现

1.3.1栈的创建

1.3.2栈的初始化

1.3.3栈的清空销毁

1.3.4栈的元素插入

1.3.5栈顶元素的删除

1.3.6返回栈顶数据

1.3.7求栈的大小

1.3.8判断栈是否为空

二、栈的实现完整代码

三、队列 

3.1队列的概念及结构

3.2队列的实现

3.2.1队列的创建

3.2.2队列的初始化

3.2.3队列内存的释放和销毁

3.2.4插入有效数据

3.2.5删除队列数据

3.2.6得到队头和队尾的数据

3.2.7判断是否为空

四、队列完整代码


一、栈

1.1栈的概念及结构

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

 

1.2栈的实现

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

 1.2.1静态栈的实现

实际中一般不用,我们主要使用支持动态增长的栈

typedef int STDataType;
#define N 10
typedef struct Stack
{
STDataType _a[N];
int _top; // 栈顶
}Stack;

1.3动态栈的实现

1.3.1栈的创建

typedef int STDatatype;
typedef struct STack
{
	STDatatype* a;
	int capacity;
	int top;
}ST;

这个结构和顺序表相似,a指向动态开辟的数组,capacity指的是动态开辟数组的容量,只不过这里的top指向的是栈顶也可以变相理解为栈里面的元素个数 。

1.3.2栈的初始化

void STInti(ST*st)
{
	st->a = NULL;
	st->capacity = 0;
	st->top = 0;//栈顶元素的下一个
}

当这里的top为0时我们插入一个元素,top会++,此时top可以取0或者1,但是我们已经插入一个数据了,因此top代表栈顶元素的下一个。

1.3.3栈的清空销毁

void STDer(ST* st)
{
	assert(st);
	free(st->a);
	st->capacity = st->top = 0;
}

首先就是要用assert判断是否传入栈了,如果传入栈就释放动态开辟的空间,再将容量置空,栈顶和栈底相融合。

1.3.4栈的元素插入

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

插入元素时我们要判断容量和栈顶是否融合,如果融合就要开辟新的空间,然后将想要插入的数据放入栈中,再将栈顶后移。

1.3.5栈顶元素的删除

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

 删除元素时不仅要判断是否有栈还要判断栈中的top是否为0,如果不为0,直接将top--就行;

1.3.6返回栈顶数据

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

 返回栈顶数据top一定要大于0,当top为1时候返回的是top-1也就是0的值,然后top--此时top为0,当一开始的top为0时会造成越界,这点我们要注意。

1.3.7求栈的大小

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

 初始化时我们将top置为0,当我们添加一个有效数据时top会++,这里的top也可以理解为栈的大小。

1.3.8判断栈是否为空

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

当top为0时可以表示栈空,0等于0,返回bool值为true。

二、栈的实现完整代码

#define _CRT_SECURE_NO_WARNINGS 67
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDatatype;
typedef struct STack
{
	STDatatype* a;
	int capacity;
	int top;
}ST;
//初始化
void STInti(ST*st)
{
	st->a = NULL;
	st->capacity = 0;//栈顶元素的下一个
	st->top = 0;
}
//清空
void STDer(ST* st)
{
	assert(st);
	free(st->a);
	st->capacity = st->top = 0;
}
//插入
void STPush(ST* st,STDatatype x)
{
	if (st->capacity == st->top)
	{
		int newcapacity = st->capacity == 0 ? 4 : st->capacity * 2;
		STDatatype* tmp = (STDatatype*)realloc(st->a, sizeof(STDatatype) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}
		st->a = tmp;
		st->capacity = newcapacity;
	}
	st->a[st->top] = x;
	st->top++;
}
//删除
void STPop(ST* st)
{
	assert(st);
	assert(st->top);
	st->top--;
}
//打印d
STDatatype STTop(ST* st)
{
	assert(st);
	assert(st->top > 0);
	return st->a[st->top-1];
}
//求大小
int STSize(ST* st)
{
	assert(st);
	return st->top;
}
//判断栈是否为空
bool STEmpty(ST* st)
{
	assert(st);
	return st->top == 0;
}
int main()
{
	ST st;
	STInti(&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);
	}
	STDer(&st);

	return 0;
}

 

由于栈的特殊性我们在输出一个栈的数据时就要删除一个空间,刚开始top指向的是栈顶元素的下一个,打印有效数据是会减一也就是栈顶元素,然后删除top--指向刚才的栈顶元素的位置。

三、队列 

3.1队列的概念及结构

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

3.2队列的实现

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

3.2.1队列的创建

typedef int QDatatype;
typedef struct QueueNode
{
	struct Queue* next;
	QDatatype data;
}QNode;

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

这里我们使用的是无头单向非循环链表实现,但是队列的特点是先进先出,也就是对应链表中的尾插和头删,由于无头单项链表寻找尾部特别麻烦我们就在创建一个结构体里面放着指向链表结构头和尾的指针。

3.2.2队列的初始化

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

我们不能直接对队列进行初始化,只能对指向队列头和尾的两个指针初始化,将其置空。

3.2.3队列内存的释放和销毁

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

根据指向头的指针找到队列的头依次往后释放内存,最后再将指向队列头和尾的指针置空。

3.2.4插入有效数据

void QueuePush(Que* pq,QDatatype x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->next = NULL;
	newnode->data = x;
	if ( pq->tail==NULL)
	{
		pq->phead=pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

插入数据是我们要特别注意指向队列尾的指针是否尾空如果尾空代表此时还没有队列,创建新的空间让队列的头和尾都指向这个空间,如果指向队列尾的指针不为空代表尾插,让指向尾的下一个指针指向新开辟的空间,再将指向尾的指针指向新的空间。

3.2.5删除队列数据

void QueuePop(Que* pq)
{
	assert(pq);
	assert(pq->phead);
	QNode* cur = pq->phead->next;
	free(pq->phead);
	pq->phead = cur;
	pq->size--;
}

这里我们就要判断指向队列头的指针是否尾空了,如果为空代表此时没有队列即没有数据可以删除,如果不为空创建一个指针让其指向队列头的下一个,然后释放队列的头,然后再将队列的头指向刚才创建的指针。

3.2.6得到队头和队尾的数据

//得到队头数据
QDatatype QueueFront(Que* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->phead->data;
}
//得到队尾数据
QDatatype QueueBack(Que* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->tail->data;
}

这里我们也要判断指向队列头的指针是否为空,如果为空代表此时没有队列,如果不为空则直接返回指向头或者尾所指向队列内部的有效数据。

3.2.7判断是否为空

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

当我们一边得到队头的数据时,会一边释放队头的空间,最后指向队列头的指针会被置空,返回的bool值为true。

四、队列完整代码

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int QDatatype;
typedef struct QueueNode
{
	struct Queue* next;
	QDatatype data;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* tail;
	int size;
}Que;
//初始化
void QueueInit(Que* pq)
{
	assert(pq);
	pq->phead = pq->tail = NULL;
	pq->size = 0;
}
//释放销毁内存
void QueueDer(Que* pq)
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	//free(pq->phead);
	pq->phead = pq->tail = NULL;
	pq->size = 0;
}
//插入数据
void QueuePush(Que* pq,QDatatype x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->next = NULL;
	newnode->data = x;
	if ( pq->tail==NULL)
	{
		pq->phead=pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}
//删除数据
void QueuePop(Que* pq)
{
	assert(pq);
	assert(pq->phead);
	QNode* cur = pq->phead->next;
	free(pq->phead);
	pq->phead = cur;
	pq->size--;
}
//得到队头数据
QDatatype QueueFront(Que* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->phead->data;
}
//得到队尾数据
QDatatype QueueBack(Que* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->tail->data;
}
//得到个数
int QueueSize(Que* pq)
{
	assert(pq);
	return pq->size;
}
//判断是否为空
bool QueueEmpty(Que* pq)
{
	assert(pq);
	return pq->phead == NULL;
}
int main()
{
	Que q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	QueuePush(&q, 6);
	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
	QueueDer(&q);
	return 0;
}

 

栈和队列的相似之处是一边得到有效数据时会一边删除或者释放内存,所以我们在在打印有效数据时就会进行这样的操作。

C语言实现栈和队列的全部内容就讲解完了,欢迎大家在评论区多多讨论!

猜你喜欢

转载自blog.csdn.net/qq_55119554/article/details/132917087