栈和队列

栈的概念及结构

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

 

栈的实现(动态)

在实现之前,我们要思考:
首先,这是一个线性表实现的,所以我们有两个选择:顺序表,也就是数组实现;或者链表实现。

栈的实现 —— 数组好还是链表好?
其实我们栈的插入和删除相当于尾插尾删用数组的唯一缺陷就是不够的时候要增容
用单链表就不太好了:尾插尾删要找尾如果不找尾,用尾指针记录尾结点。但是即便这样,我们也要找到前一个,所以要不就使用双链表实现。如果一定想使用单链表,把把栈顶栈底反一下,用头插,不要用尾插即可
所以用单链表也是ok的

如果用尾作栈顶,那么用双链表好
如果要用单链表实现,那么就用让头作栈顶
总和各方面的要素,使用数组(顺序表)实现是最合适的。
 

Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.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);         //取栈顶的元素
bool StackEmpty(ST* ps);            //判断栈是否为空
int StackSize(ST* ps);             //获取栈有多少数据

 Stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

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

//销毁
void StackDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	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 fail\n");
			exit(-1);
		}
		ps->capacity = newcapacity;
		ps->a = tmp;

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


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

}


//获取栈有多少数据
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}


Test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void TestStack()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPush(&st, 5);
	while (!StackEmpty(&st))
	{
		printf("%d", StackTop(&st));
		StackPop(&st);
	}
	printf("\n");

	StackDestory(&st);
}


int main()
{
	TestStack();

	return 0;
}

队列 

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有 先进先出 FIFO(First In First Out) 入队列:进行插入操作的一端称为 队尾 出队列:进行删除操作的一端称为 队头。只一端入数据,没必要用双向循环链表,使用单链表。

请添加图片描述

队列代码 

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

Queue.c 

#define _CRT_SECURE_NO_WARNINGS 1
#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;
	}
	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(!QueueEmpty);

	//一个节点
	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(!QueueEmpty);
	return pq->head->data;

}



//取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty);
	return pq->tail->data;

}



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



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

Test.c

#include"Queue.h"
#include "Queue.h"



void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);

	printf("%d ", QueueFront(&q));
	QueuePop(&q);
	printf("%d ", QueueFront(&q));
	QueuePop(&q);

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


	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");
}

int main()
{
	TestQueue();

	return 0;
}

队列的应用场景

1、排队,保持绝对的公平性

2、广度优先遍历 BFS

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裙下的霸气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值