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

栈与队列的实现

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

栈与队列的实现

栈:一种特殊的线性表,只允许在固定的一端插入或删除元素。进行数据的插入或删除的一端称为栈顶,另一端称为栈低。栈中元素遵循先进后出、后入先出原则(像弹夹一样)

 

//头文件模块
//Stack.h
#include 
#include 
#include 
#include 
struct Stack
{
	int* a;//数组来实现
	int top;//栈顶
	int capacity;//检测空间,不够增容
};
typedef struct Stack ST;
void StackInit(ST* ps);//初始化
void StackDestory(ST* ps);//销毁
void StackPush(ST* ps, int x);//插入,进栈
void StackPop(ST* ps);//删除,出栈
int  StackTop(ST* ps);//取栈里的数据
int  StackSiez(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 = (int*)malloc(sizeof(int) * 4);//开辟4个整形字节
	ps->capacity = 4;//记录4个整形字节
	ps->top = 0;//栈顶为0
}
void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
void StackPush(ST* ps, int x)
{
	assert(ps);
	if (ps->top == ps->capacity)//满了扩容,扩2倍
	{
		int* tmp = (int*)realloc(ps->a, ps->capacity * 2 * sizeof(int));
		if (tmp == NULL)
		{
			return 0;
		}
		else
		{
			ps->a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)//出栈
{
	assert(ps);
	assert(ps->top > 0);//栈为空,直接终止
	ps->top--;
}
int  StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);//栈为空时报错
	return ps->a[ps->top - 1];
}
int  StackSiez(ST* ps)//求栈里的数据个数
{
	assert(ps);
	return ps->top;
}
bool  StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//主函数模块
//main.c
#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
int main()
{
	ST s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	StackPush(&s, 5);
	while (!StackEmpty(&s))//如果栈不为空
	{
		printf("%d ",StackTop(&s));//取出数据
		StackPop(&s);//取出数据后删除
	}
	printf("n");
	StackDestory(&s);
}

队列:只允许在固定的一端插入或删除数据,遵循先进先出原则

入队列:队尾

出队列:队头

//头文件模块
//Queue.h
#include 
#include 
#include 
#include 
struct Queue
{
	int data;
	struct Queue* next;
};
typedef Queue Qnode;
struct Queue
{
	Queue* head;//头删
	Queue* tail;//尾插
};
typedef Queue Queue;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);//释放
void QueuePush(Queue* pq, int x);//队尾入
void QueuePop(Queue* pq);//对头出
int QueueFront(Queue* pq);//取出对头的数据
int QueueBack(Queue* pq);//取出对尾的数据
int QueueSize(Queue* pq);//计算数据的个数
bool QueueEmpty(Queue* pq);//判断队是否为空

 

//实现模块
//Queue.c
#define  _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}
void QueueDestory(Queue* pq)
{
	assert(pq);
	Queue* cut = pq->head;
	while (cut)
	{
		Queue* next = cut->next;//删掉前先保存下一个
		free(cut);
		cut = next;
	}
	pq->haed = pq->tail = NULL;
}
void QueuePush(Queue* pq, int x)//入队列
{
	assert(pq);
	Queue* newnode = (Queue*)malloc(sizeof(Queue));
	if (newnode != NULL)
	{
		return 0;
	}
	//初始化一下
	newnode->date = x;
	newnode->next = NULL;
	if (pq->tail == NULL)//如果没有头节点
	{
		pq->tail = pq->head = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head);//出队列要保证不为空
	if (pq->head->tail == NULL)//只剩最后一个结点,防止tail为野指针
	{
		free(pq->head);
		pq->head = pq->tail;
	}
	else
	{
		Qnode* next = pq->head->next;
		free(pq->head);
		pq->head = next;//变成新的头
	}
	
}
int QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->adata;
}
int QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->tail->adata;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	int size = 0;
	Queue* cut = pq->head;
	while (cut)
	{
		size++;
		cut = cut->next;
	}
	return size;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head;
}

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

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

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