Lesson4--栈和队列

news2024/9/24 21:26:04

目录

1.栈

1.1栈的概念及结构

1.2栈的实现

        初始化栈

        销毁栈

        栈的扩容

        入栈

        出栈

         获取栈顶元素

        获取栈中有效元素个数

        判空

程序代码如下

Stack.h

Stack.c

test.c

2.队列

2.1队列的概念及结构

​2.2队列的实现

        初始化队列

        队尾入队列

        队头出队列

        获取队列头部元素

        获取队列队尾元素

        获取队列中有效元素个数

        队列打印

        队列判空

        销毁队列

Queue.h

Queue.c

test.c


1.栈

1.1栈的概念及结构

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

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

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

 

可以使用数组(数组栈)或者链表(链表栈)实现栈。

数组栈实现更优一些。因为数组栈 尾插尾删效率更高,且缓存利用率高。

单链表,适合头插头删,如果用头作栈顶,可以设计成单链表,但是有点怪。用尾作栈底,要设计成双向链表,否则增删数据效率低。
 

 

 1.2栈的实现

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

#define _CRT_SECURE_NO_WARNINGS 1
#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 StackInit(ST* ps);
// 销毁栈
void StackDestroy(ST* ps);
// 入栈
void StackPush(ST* ps, STDataType x);
// 出栈
void StackPop(ST* ps);
// 获取栈顶元素
STDataType StackTop(ST* ps);
// 获取栈中有效元素个数
int StackSize(ST* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(ST* ps);

初始化栈

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;//ps->top=-1;
}

销毁栈

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

栈的扩容

void StackCheckCapacity(ST* ps)
{
	assert(ps);

	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}

 入栈

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	StackCheckCapacity(ps);
	ps->a[ps->top] = x;
	ps->top++;

}

出栈

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

 获取栈顶元素

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

获取栈中有效元素个数

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

判空

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(ST* ps)
{
	assert(ps);
	/*if (ps->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}*/
	//换成下面语句就行了
	return ps->top == 0;
}

程序代码如下

Stack.h

#define _CRT_SECURE_NO_WARNINGS 1
#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 StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
int StackSize(ST* ps);
bool StackEmpty(ST* ps);

Stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = ps->capacity = 0;//ps->top=-1;
}

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

void StackCheckCapacity(ST* ps)
{
	assert(ps);

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

}

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	/*if (ps->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}*/
	//换成下面语句就行了
	return ps->top == 0;
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void test1()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	printf("%d ", StackTop(&st));

	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	StackPop(&st);
	//printf("%d ", StackTop(&st));
	StackDestroy(&st);

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

2.队列

2.1队列的概念及结构

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

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

 2.2队列的实现

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

typedef int QDataType;

//链式结构:表示队列
typedef struct QueueNode
{
	struct QueueNode* next;//指针域
	QDataType data;//数据域
}QueueNode;

//队列链表的头指针与尾指针
typedef struct Queue
{
	QueueNode* head;
	QueueNode* tail;
}Queue;

//初始化队列
void QueueInit(Queue* pq);
//队尾入队列
void QueuePush(Queue* pq, QDataType x);
//队头出队列
void QueuePop(Queue* pq);
//获取队列头部元素
QDataType QueueFront(Queue* pq);
//获取队列队尾元素
QDataType QueueBack(Queue* pq);
//获取队列中有效元素个数
int QueueSize(Queue* pq);
//队列打印
void QueuePrint(Queue* pq);
//队列判空
bool QueueEmpty(Queue* pq);
//销毁队列
void QueueDestroy(Queue* pq);

初始化队列

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

队尾入队列

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newnode = BuyQueueNode(x);
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

队头出队列

void QueuePop(Queue* pq)
{
	assert(pq);
	//if (pq->head == NULL)
	//	return;
	assert(!QueueEmpty(pq));

	QueueNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;
	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}
}

获取队列头部元素

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 n = 0;
	QueueNode* cur = pq->head;
	while (cur)
	{
		++n;
		cur = cur->next;
	}

	return n;
}

队列打印

void QueuePrint(Queue* pq)
{
	QueueNode* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");

}

队列判空

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

销毁队列

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;// QueueNode* cur =NULL
	while (cur != NULL)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}

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

Queue.h

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

typedef int QDataType;

//链式结构:表示队列
typedef struct QueueNode
{
	struct QueueNode* next;//指针域
	QDataType data;//数据域
}QueueNode;

//队列链表的头指针与尾指针
typedef struct Queue
{
	QueueNode* head;
	QueueNode* tail;
}Queue;

//初始化队列
void QueueInit(Queue* pq);
//队尾入队列
void QueuePush(Queue* pq, QDataType x);
//队头出队列
void QueuePop(Queue* pq);
//获取队列头部元素
QDataType QueueFront(Queue* pq);
//获取队列队尾元素
QDataType QueueBack(Queue* pq);
//获取队列中有效元素个数
int QueueSize(Queue* pq);
//队列打印
void QueuePrint(Queue* pq);
//队列判空
bool QueueEmpty(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 = NULL;
	pq->tail = NULL;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;// QueueNode* cur =NULL
	while (cur != NULL)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->head = pq->tail = NULL;
}
QueueNode* BuyQueueNode(QDataType x)
{
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* newnode = BuyQueueNode(x);
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePrint(Queue* pq)
{
	QueueNode* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");

}

void QueuePop(Queue* pq)
{
	assert(pq);
	//if (pq->head == NULL)
	//	return;
	assert(!QueueEmpty(pq));

	QueueNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;
	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}
}

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 n = 0;
	QueueNode* cur = pq->head;
	while (cur)
	{
		++n;
		cur = cur->next;
	}

	return n;
}

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

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"Queue.h"


void test1()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePrint(&q);
	/*QueuePop(&q);
	QueuePop(&q);
	QueuePop(&q);
	QueuePop(&q);*/
	QueuePrint(&q);

	printf("%d\n", QueueFront(&q));
	printf("%d\n", QueueBack(&q));
	QueueDestroy(&q);

}

void test2()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QDataType front = QueueFront(&q);
	printf("%d ", front);
	QueuePop(&q);

	QueuePush(&q, 3);
	QueuePush(&q, 4);

	while (!QueueEmpty(&q))
	{
		QDataType front = QueueFront(&q);
		printf("%d ", front);
		QueuePop(&q);
	}
	printf("\n");
}
int main()
{
	//test1();
	test2();
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/176201.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

二、pyhon基础语法篇(黑马程序猿-python学习记录)

黑马程序猿的python学习视频&#xff1a;https://www.bilibili.com/video/BV1qW4y1a7fU/ 目录 一 、print 1. end 2. \t对齐 二、字面量 1. 字面量的含义 2. 常见的字面量类型 3. 如何基于print语句完成各类字面量的输出 三、 注释的分类 1. 单行注释 2. 多行注释 3. 注释的…

多进程|基于非阻塞调用的轮询检测方案|进程等待|重新理解挂起|Linux OS

说在前面 今天给大家带来操作系统中进程等待的概念&#xff0c;我们学习的操作系统是Linux操作系统。 我们今天主要的目标就是认识wait和waitpid这两个系统调用。 前言 那么这里博主先安利一下一些干货满满的专栏啦&#xff01; 手撕数据结构https://blog.csdn.net/yu_cbl…

nacos源码分析==服务订阅-服务端推送被订阅者最新信息给订阅者

上一篇讲到客户端发送请求到服务端进行服务注册&#xff0c;注册后&#xff0c;服务端会发出两个事件&#xff0c;第一个事件会触发另一个ServiceChangedEvent&#xff0c;这个事件被com.alibaba.nacos.naming.push.v2.NamingSubscriberServiceV2Impl#onEvent 监听&#xff0c…

16. 条件控制

总体来说&#xff0c;条件控制的效果类似c/c/c#/java中的&#xff0c;只不过在语法格式的层面上存在一定的差异。 1. if条件语法格式 if condition_1:...elif condition_2:...else:...1、python 中用 elif 代替了 c/c中的 else if&#xff0c;所以if语句的关键字为&#xff1a…

高性能排序函数实现方案

如C语言的qsort()、Java的Collections.sort()&#xff0c;这些排序函数如何实现&#xff1f; 1 合适的排序算法&#xff1f; 线性排序算法的时间复杂度较低&#xff0c;适用场景特殊&#xff0c;通用排序函数不能选择。 小规模数据排序&#xff0c;可选时间复杂度O(n^2)算法大…

【算法】滑动窗口

目录1.概述2.算法框架3.应用本文参考&#xff1a; LABULADONG 的算法网站 1.概述 &#xff08;1&#xff09;滑动窗口可以用以解决数组/字符串的子元素相关问题&#xff0c;并且可以将嵌套的循环问题&#xff0c;转换为单循环问题&#xff0c;从而降低时间复杂度。故滑动窗口算…

【数据分析】(task5)数据建模及模型评估

note 文章目录note一、建立模型二、模型评估2.1 交叉验证2.2 混淆矩阵/recall/accuracy/F12.3 ROC曲线三、Pyspark进行基础模型预测时间安排Reference一、建立模型 下载sklearn的命令pip install scikit-learn。 from sklearn.model_selection import train_test_split impor…

ARP渗透与攻防(二)之断网攻击

ARP断网攻击 系列文章 ARP渗透与攻防(一)之ARP原理 1.环境准备 kali 作为ARP攻击机&#xff0c;IP地址&#xff1a;192.168.110.26 MAC地址&#xff1a;00:0c:29:fc:66:46 win10 作为被攻击方&#xff0c;IP地址&#xff1a;192.168.110.12 MAC地址&#xff1a;1c:69:7a:a…

Tkinter的Entry与Text

Tkinter界面设计之输入控件Entry以及文本框控件Text。 目录 一、放置控件 1. pack()函数 2. place()函数 3. grid()函数 二、简单控件 1. Entry输入控件 1.1 tk.StringVar()函数&#xff1a;接收一个字符串 1.2 tk.Entry()函数&#xff1a;设置一个输入控件E 2. Text文…

CMake多文件编译

之前学习ceres-solver中的3d相关的源码的时候&#xff0c;发现对于CMake多文件工程编译中对于CMakeLists.txt的编写和处理的理解运用还是比较模糊&#xff0c;这里整理梳理一下对于不同文件夹数量如何使用。 参考文章&#xff1a; CMake使用详解二&#xff08;多文件编译&…

maya常用操作

1&#xff1a;重置工作区。2&#xff1a;切换视图。按空格切换视图。3&#xff1a;未选中状态&#xff0c;按shift&#xff0c;再点右键&#xff0c;可以打开交互式创建。这样可以在栅格上创建想要的大小。不选中交互式创建的话&#xff0c;创建的是默认未知。默认未知为正中间…

linux系统中利用QT实现车牌识别的方法

大家好&#xff0c;今天主要和大家分享一下&#xff0c;如何利用QT实现车牌识别的方法。 目录 第一&#xff1a;车牌识别基本简介 第二&#xff1a;车牌识别产品申请 第三&#xff1a;百度车牌识别API接口 第四&#xff1a;车牌识别综合测试 第一&#xff1a;车牌识别基本简…

Scala快速入门

Scala简介 Scala是一门现代的多范式编程语言&#xff0c;平滑地集成了面向对象和函数式语言的特性。Scala运行于Java平台&#xff08;JVM&#xff0c;Java 虚拟机&#xff09;上&#xff0c;并兼容现有的Java程序&#xff0c;Scala代码可以调用Java方法&#xff0c;访问Java字…

ArcGIS Pro脚本工具(17)——生成多分式标注

​朋友们&#xff0c;你们知道ArcGIS里面分式标注的四种写法么&#xff1f; 放错图了&#xff0c;是这个 分式标注的四种形式我们可以把这类叫分式标注&#xff0c;网上也有博主分享过如何在ArcGIS中制作这类标注&#xff0c;但我觉得仍有一些不足。 一是基本都使用VB编写&…

中文问题相似度挑战赛

赛题概要 请本赛题排行榜前10位的队伍&#xff0c;通过作品说明提交源代码&#xff0c;模型以及说明文档&#xff0c;若文件过大&#xff0c;可发送至官网邮箱AICompetitioniflytek.com, 若截止时间内为提交&#xff0c;官方会通过电话联系相关选手&#xff0c;若未接到通知或…

WPF作图神器Interactive DataDisplay的初步使用

文章目录安装初步使用安装 Interactive DataDisplay是一款比较优秀的C#绘图控件&#xff0c;尽管与一些商业控件还有不小的差距&#xff0c;关键是开源免费轻量。 在VS中安装控件十分简单&#xff0c;本测试基于Net Core5.0&#xff0c;在VS的菜单栏->工具->NuGet包管理…

HomeLab 常用工具一:filebrowser

前言在实际使用过程中&#xff0c;我们通常都有基于WEB 的文件操作需求&#xff08;例如从一台陌生设备上想打开看一下&#xff0c;图片等&#xff09;&#xff0c;和nextcloud 相比 filebrowser 更为轻巧也更为方便。一、filebrowser 安装这里基于docker 安装和使用&#xff0…

Prometheus 动态拉取监控服务

Prometheus 版本 2.41.0 平台统一监控的介绍和调研直观感受PromQL及其数据类型PromQL之选择器和运算符PromQL之函数Prometheus 配置身份认证Prometheus 动态拉取监控服务 我们在以前的实例中配置Prometheus 的target 都是手动配置&#xff0c;这在监控目标少的情况下还可以接受…

【基础】BMP格式

BMP格式位图 (BMP)简介格式1.1图和调色板的概念1.2 bmp文件格式1.2.1 位图文件头 14字节1.2.2 位图信息头 40字节1.2.3 调色板1.2.4 注意位图 (BMP)简介 BMP取自位图Bitmap的缩写&#xff0c;也称为DIB&#xff08;与设备无关的位图&#xff09;&#xff0c;是一种独立于显示器…

【苹果家庭群发推】软件keychain中刚打开的证书下载的证书文件要决不会报错 UNTimeIntervalNotificationTrigge

推荐内容IMESSGAE相关 作者✈️IMEAX推荐内容iMessage苹果推软件 *** 点击即可查看作者要求内容信息作者✈️IMEAX推荐内容1.家庭推内容 *** 点击即可查看作者要求内容信息作者✈️IMEAX推荐内容2.相册推 *** 点击即可查看作者要求内容信息作者✈️IMEAX推荐内容3.日历推 *** …