数据结构 栈与队

目录

栈的概念

栈的实现

队的概念

队的实现


栈的概念

栈(stack)是一种线性表,与其他线性表相比删除与输入都是再同一个地方进出

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

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

栈的实现

栈的结构体与初始化

stack.h

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int SNODEDetaTpye;
typedef struct StackNODE
{
	SNODEDetaTpye data;
	struct StackNODE* next;
}SNODE;

void SNODEInit(SNODE* head);//初始化

void SNODEDestory(SNODE* head);//销毁栈

stack.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS

#include "stack.h"

void SNODEInit(SNODE* head)//初始化函数
{
	head->next = NULL;
	head->data = 0;
	return;
}

栈的销毁函数

cpp 复制代码
void SNODEDestory(SNODE* head)//销毁栈
{
	SNODE* dest, *pnode = head->next;
	while (pnode != NULL)
	{
		dest = pnode;
		pnode = pnode->next;
		free(dest);
		dest = NULL;
	}
	
	return;
}

判断栈的是否为空栈

cpp 复制代码
bool SNODEEmpty(SNODE* head)//判断是否为空栈
{
	if (head->next == NULL)
		return true;
	else
		return false;
}

栈的入栈函数

cpp 复制代码
void SNODEPush(SNODE* phead, SNODEDetaTpye a)//入栈函数
{  
	SNODE* newhead = (SNODE*)malloc(sizeof(SNODE));
	if (newhead == NULL)
	{
		perror("malloc");
		exit(1);
	}
	newhead->next = phead->next;
	phead->next = newhead;
	phead->next->data = a;
	return;
}

void Print(SNODE* head)//输出
{
	SNODE* phead = head;
	phead = phead->next;
	while (phead)
	{
		printf(" %d ->", phead->data);
		phead = phead->next;
	}
	printf("\n");
	return;
}

int main()
{
	SNODE head;

	SNODEInit(&head);
	SNODEPush(&head, 1);
	SNODEPush(&head, 2);
	SNODEPush(&head, 3);
	SNODEPush(&head, 4);
	Print(&head);

	SNODEDestory(&head);
	return 0;
}

栈的出栈函数

cpp 复制代码
void SNODEPop(SNODE* head)
{
	if (SNODE_Empty(head))
	{
		printf("栈为空函数\n");
		return;
	}
	else
	{
		SNODE* pop = head->next;
		head->next = head->next->next;
		free(pop);
		pop = NULL;
	}
	return;
}
int main()
{
	SNODE head;

	SNODEInit(&head);
	SNODEPush(&head, 1);
	SNODEPush(&head, 2);
	SNODEPush(&head, 3);
	SNODEPush(&head, 4);

	Print(&head);
	SNODE_Pop(&head);

	Print(&head);
	SNODE_Pop(&head);	

	Print(&head);
	SNODE_Pop(&head);

	Print(&head);
	SNODE_Pop(&head);

	Print(&head);
	SNODE_Pop(&head);

	SNODEDestory(&head);
	return 0;
}

取栈的有效元素个数

cpp 复制代码
int SNODE_Fine(SNODE* head)//查找有效元素个数,就是入栈的个数
{
	SNODE* phead = head->next;
	int num=0;
	while (phead)
	{
		num++;
		phead = phead->next;
	}
	return num;
}

int main()
{
	SNODE head;

	SNODEInit(&head);
	SNODEPush(&head, 1);
	SNODEPush(&head, 2);
	SNODEPush(&head, 3);
	SNODEPush(&head, 4);
	printf("有效个数:%d\n", SNODE_Fine(&head));

	Print(&head);
	SNODE_Pop(&head);
	printf("有效个数:%d\n", SNODE_Fine(&head));

	Print(&head);
	SNODE_Pop(&head);	
	printf("有效个数:%d\n", SNODE_Fine(&head));

	Print(&head);
	SNODE_Pop(&head);
	printf("有效个数:%d\n", SNODE_Fine(&head));

	Print(&head);
	SNODE_Pop(&head);
	printf("有效个数:%d\n", SNODE_Fine(&head));

	Print(&head);
	SNODE_Pop(&head);
	printf("有效个数:%d\n", SNODE_Fine(&head));

	SNODEDestory(&head);
	return 0;
}

队的概念

(queue)

在现实世界里,排队我们肯定从队尾进入,从队头出去,那么队的概念就是这样的,有两个口一个负责入队,一个口负责出队。

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

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

队的实现

队的结构体和头尾指针结构体

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int DateType;
typedef struct queuendoe
{
	DateType data;
	struct queuenode* next;
}QN;

typedef struct queue
{
	QN* phead;
	QN* ptail;
	int size;
}Q;

队的初始函数

cpp 复制代码
void queue_init(Q* queue)//初始化
{
	QN* phead = (QN*)malloc(sizeof(QN));//哨兵位
	if (phead == NULL)
	{
		perror("malloc");
		exit(1);
	}
	phead->next = NULL;

	queue->phead = queue->ptail = phead;
	queue->size = 0;
	return;
}

队的销毁函数

cpp 复制代码
void queue_destory(Q* queue)//销毁队
{
	QN* dest;
	while (queue->phead != queue->ptail)
	{
		dest = queue->phead;
		queue->phead = queue->phead->next;
		free(dest);
		dest = NULL;
		queue->size--;
	}
	printf("队列销毁成功");
	return;
}

队的入队函数

cpp 复制代码
void queue_push(Q* queue, DateType a)//入队
{
	QN* node = (QN*)malloc(sizeof(QN));
	if (node == NULL)
	{
		perror("malloc");
		exit(1);
	}
	node->data = a;
	node->next = NULL;
	queue->ptail->next = node;
	queue->ptail = node;
	queue->size++;
	return;
}

队的出队函数

cpp 复制代码
void queue_pop(Q* queue)//出队函数
{
	assert(queue!=NULL);
    
    if (queue->size)
	{
		QN* pop;
		pop = queue->phead;
		queue->phead = queue->phead->next;
		free(pop);
		pop = NULL;
		queue->size--;
	}
	else
	{
		printf("队列为空\n");
	}
	return;
}

主函数的实现

cpp 复制代码
void print(Q* queue)//输出
{
	QN* p = queue->phead->next;
	while (p != NULL)
	{
		printf(" %d ->", p->data);
		p = p->next;
	}
	printf("\n");

	return;
}

int main()
{
	Q queue;
	queue_init(&queue);
	queue_push(&queue, 1);
	queue_push(&queue, 2);
	queue_push(&queue, 3);
	queue_push(&queue, 4);
	print(&queue);

	queue_pop(&queue);
	print(&queue);

	queue_pop(&queue);
	print(&queue);	
	
	queue_pop(&queue);
	print(&queue);	
	
	queue_pop(&queue);
	print(&queue);
	queue_pop(&queue);

	queue_init(&queue);
	queue_push(&queue, 1);
	queue_push(&queue, 2);
	queue_push(&queue, 3);
	queue_push(&queue, 4);
	print(&queue);
	queue_destory(&queue);
	
	return;
}

输出结果:


感谢观看!

悠仁さん

相关推荐
CSharp精选营10 小时前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
刘马想放假4 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠5 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦11 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠12 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾12 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres82113 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q13 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒13 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记13 天前
单项不带头不循环链表
数据结构·链表