栈和队列的介绍

栈:

什么是栈?

栈 是一种特殊的线性表 

栈的特性: 只能在一端插入/删除输入
插入的地方叫进栈,删除的地方出栈,栈的修改只针对栈顶

栈需要的头文件(c语言中)

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>

栈的定义

typedef int STDataType;

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->top = 0;
	ps->capacity = 4;
	ps->_a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (ps->_a == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
}

void StackDestory(ST* ps)//销毁
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->top = ps->capacity = 0;
}

void StackPush(ST* ps, STDataType x)//栈顶的插入
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = realloc(ps->_a, ps->capacity * 2 * sizeof(STDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->_a = tmp;
			ps->capacity *= 2;
		}
	}
	ps->_a[ps->top++] = x;
}

void StackPop(ST* ps)//栈顶的删除 
{
	assert(ps);
	assert(ps->top > 0);

	ps->top--;
}

STDataType StackTop(ST* ps)//看查栈顶的位置
{
	assert(ps);
	assert(ps->top > 0);
	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;
}

队列

什么是队列?

队列是一种特殊的线性表 

队列  只允许在一端插入数据 另一端删除数据

队列需要的头文件

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>

队列的定义 以及设置头/尾节点

typedef int QDataType;

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

typedef struct Queue
{
	QNode* head;//头节点
	QNode* tail;//尾节点
}Queue;

队列需要的实现的接口

void QueueInit(Queue* pq);//初始化

void QueueDestory(Queue* pq);//销毁

void QueuePush(Queue* pq,QDataType x);//头插

void QueuePop(Queue* pq);//尾删

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

QDataType QueueBack(Queue* pq);//取尾

int QueueSize(Queue* pq);

bool QueueEmpty(Queue* pq);

队列接口的实现

#include"Queue.h"

void QueueInit(Queue* pq)//初始化
{
	assert(pq);
	pq->head = pq->tail = NULL;//初始化 为空
}

void QueueDestory(Queue* pq)//销毁
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QDataType x)//尾插
{
	assert(pq);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;

	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newNode;
	}
	else
	{
		pq->tail->next = newNode;
		pq->tail = newNode;
	}
}

void QueuePop(Queue* pq)//头删
{
	assert(pq);
	assert(pq->head);
	if (pq->head->next == NULL)//只有一个节点的时候
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;

	}
}


QDataType QueueFront(Queue* pq)//取头数据
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
QDataType QueueBack(Queue* pq)//取尾
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->data;
}

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

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

猜你喜欢

转载自blog.csdn.net/dabai__a/article/details/134065984