数据结构之栈和队列

栈和队列

一 结构与概念

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

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

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

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

二 代码实现

栈的初始化:

c 复制代码
void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

栈的销毁:

c 复制代码
void STDestroy(ST* ps)
{
	if (ps->arr != NULL)
		free(ps->arr);

	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

入栈:

c 复制代码
void StackPush(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->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!\n");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}

判断栈是否为空:

c 复制代码
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

出栈:

c 复制代码
void StackPop(ST* ps)
{
	assert(!StackEmpty(ps));
	--ps->top;
}

取栈顶元素:

c 复制代码
STDataType StackTop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}

获取栈中有效元素个数:

c 复制代码
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

栈的整段代码包含

  • Stack.c
c 复制代码
#include"Stack.h"

void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
void STDestroy(ST* ps)
{
	if (ps->arr != NULL)
		free(ps->arr);

	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}
//入栈--栈顶
void StackPush(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->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!\n");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	//空间足够
	ps->arr[ps->top++] = x;
}
//栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//出栈--栈顶
void StackPop(ST* ps)
{
	assert(!StackEmpty(ps));
	--ps->top;
}
//取栈顶元素
STDataType StackTop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
  • Stack.h
c 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
//定义栈的数据结构
typedef int STDataType;
typedef struct Stack
{
	STDataType* arr;
	int top;          //指向栈顶位置
	int capacity;     //容量
}ST;
//初始化
void STInit(ST* ps);
//销毁
void STDestroy(ST* ps);

//入栈--栈顶
void StackPush(ST* ps, STDataType x);
//出栈--栈顶
void StackPop(ST* ps);

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

//栈是否为空
bool StackEmpty(ST* ps);
//获取栈中有效元素个数
int STSize(ST* ps);
  • test.c
c 复制代码
#include"Stack.h"

void test()
{
	ST st;
	STInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);

	//StackPop(&st);
	//StackPop(&st);
	//StackPop(&st);
	//StackPop(&st);
	//StackPop(&st);

	while (!StackEmpty(&st))
	{
		STDataType top = StackTop(&st);
		printf("%d ", top);
		StackPop(&st);
	}

	STDestroy(&st);
}

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

队列

一 结构与概念

概念:只允许在一端进行数据插入操作,在另一端进行删除数据操作的特殊线性表。队列是先进先出特,与栈相反,栈是先进后出。
入队列 :进行插入操作的一端称为队尾
出队列 :进行删除操作的一段称为对头

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

二 代码实现

  • Queue.h
c 复制代码
#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;
}QueueNode;

//定义队列的结构
typedef struct Queue
{
	QueueNode* phead;  //队头
	QueueNode* ptail;  //队尾
	int size;          //队列有效元素个数
}Queue;

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

//入队列--队尾
void QueuePush(Queue* pq, QDataType x);
//出队列--队头
void QueuePop(Queue* pq);

//取队头数据
QDataType QueueFront(Queue* pq);
//取队尾数据
QDataType QueueBack(Queue* pq);

//队列判空
bool QueueEmpty(Queue* pq);
//队列有效元素个数
int QueueSize(Queue* pq);
  • Queue.c
c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail!\n");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;

	//队列为空,队头和队尾都是newnode
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else {
		//pq->ptail newnode
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;
	}
	pq->size++;
}

//队列判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}
//队列有效元素个数
int QueueSize(Queue* pq)
{
	//int size = 0;
	//QueueNode* pcur = pq->phead;
	//while (pcur)
	//{
	//	size++;
	//	pcur = pcur->next;
	//}
	//return size;

	return pq->size;
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	//只有一个结点
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else {
		//多个结点
		QueueNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	--pq->size;
}
//取队头数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->phead->data;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}
//销毁队列
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
  • test.c
c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void test()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	//while (QueueSize(&q))
	//{
	//	printf("%d\n", QueueSize(&q));
	//	QueuePop(&q);
	//}

	printf("phead:%d\n", QueueFront(&q));
	printf("ptail:%d\n", QueueBack(&q));

	QueueDestroy(&q);

}
int main()
{
	test();
	return 0;
}
相关推荐
uesowys2 小时前
算法开发指导-数据结构-Tree
数据结构·算法·
白太岁2 小时前
C++:(4) 内存布局、编译流程、关键字及其链接性
c语言·汇编·jvm·c++
古译汉书2 小时前
【IoT死磕系列】Day 3:学习HTTP!实战:STM32手写GET请求获取天气实战(附源码+八股文)
数据结构·stm32·物联网·网络协议·学习·算法·http
郝学胜-神的一滴2 小时前
计算思维:数字时代的超级能力
开发语言·数据结构·c++·人工智能·python·算法
m0_531237172 小时前
C语言-数组练习
c语言·开发语言·算法
二年级程序员3 小时前
一篇文章掌握“双向链表”
c语言·数据结构·链表
元亓亓亓3 小时前
考研408--数据结构--day14--B树&B+树&散列表
数据结构·b树·散列表·b+树·408
季明洵3 小时前
Java实现循环队列、栈实现队列、队列实现栈
java·数据结构·算法··队列
Non importa3 小时前
二分法:算法新手第三道坎
c语言·c++·笔记·qt·学习·算法·leetcode