栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

栈和队列的实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

栈和队列的实现

目录

1.栈

 2.队列


1.栈

1.1 栈的概念及其结构

栈:是一种特殊的线性表,只允许在固定的一端进行插入和删除元素,这一段称为栈顶,另一端称为栈底,栈中的元素遵循先进后出的原则

1.2 栈的实现

栈的实现一般是使用数组或者链表实现,先比较而言,数组实现的结构更简单一些,因为数组在尾上插入数据的代价更小。

 

#pragma once
#include
#include
#include
#include

typedef int STDataType;

//动态的栈,静态的栈去掉capacity
typedef struct Stack
{
	STDataType* a;
	int top;//标识栈顶的位置
	int capacity;
}ST;

void StackInit(ST* ps);//初始化

void StackDestory(ST* ps);//摧毁

void StackPush(ST* ps,STDataType X);//插入

void StackPop(ST* ps);//删除

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

int StackSize(ST* ps);//计算栈的元素个数

bool StackEmpty(ST* ps);//判断栈为不为空

栈的实现

#include"Stack.h"

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

void StackDestory(ST* ps)//删除栈
{
	assert(ps);
	free(ps->a);
	ps->capacity = ps->top = 0;
}

void StackPush(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)
		{
			printf("realloc failn");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = X;
	ps->top++;
}

void StackPop(ST* ps)//删除
{
	assert(ps);
	assert(ps->top >= 0);
	//assert(!StackEmpty(ps));
	ps->top--;
}

STDataType StackTop(ST* ps)//取栈顶元素
{
	assert(ps);
	assert(ps->top >= 0);
	//assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

int StackSize(ST* ps)//计算栈的元素个数
{
	assert(ps);
	return ps->top;
}

bool StackEmpty(ST* ps)//判断栈为不为空
{
	assert(ps);
	return ps->top == 0;
}

 2.队列

2.1 队列的概念及结构

队列:只允许在一段进行插入数据操作,在另一端进行数据删除的特殊线性表,其具有先进先出的特性,进行插入操作的一端是队尾,进行删除操作的一段叫做队头

2.2队列的实现

队列也是可以用数组和链表来实现的,但是队列使用链表更优一些,因为在出队的时候,比数组效率高。

队列实现的.h

#pragma once

#include
#include
#include
#include

typedef int QDataType;

typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;

}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
}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);

队列实现

#include"Queue.h"


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

void QueueDestroy(Queue* pq)//摧毁队列,释放队列
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
}

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		printf("malloc failn");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

void QueuePop(Queue* 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;
		pq->head = next;
	}
}

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

bool QueueEmpty(Queue* pq)//判断队列是否为空
{
	assert(pq);
	return pq->head == NULL;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		cur = cur->next;
		size++;
	}
	return size; 
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号