• 【数据结构】栈和队列


    1.栈

    1.1栈的实现参考代码

    #include
    #include
    #include
    #include
    
    typedef int STDataType;
    
    //数组实现的动态栈
    typedef struct Stack
    {
    	STDataType* a;
    	int top;//栈顶
    	int capacity;//总空间
    }ST;
    
    //初始化栈
    void STInit(ST* pst);
    
    //检查空间是否足够
    void CheckCapacity(ST* pst);
    
    //插入栈顶元素
    void STPush(ST* pst, STDataType x);
    
    //删除栈顶元素
    void STPop(ST* pst);
    
    //获取栈顶元素
    STDataType STTop(ST* pst);
    
    //检查栈是否为空
    bool STEmpty(ST* pst);
    
    //栈的大小
    int STSize(ST* pst);
    
    //栈的销毁
    void STDestory(ST* pst);
    
    #include"Stack.h"
    
    //检查空间是否足够
    void CheckCapacity(ST* pst)
    {
    	assert(pst);
    
    	if (pst->top == pst->capacity)
    	{
    		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
    		STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
    		if (tmp == NULL)
    		{
    			perror("CheckCapacity()::relloc():");
    			return;
    		}
    		pst->a = tmp;
    		pst->capacity = newcapacity;
    	}
    }
    
    //初始化栈
    void STInit(ST* pst)
    {
    	assert(pst);
    
    	pst->a = NULL;
    	pst->top = 0;//这里的栈顶指的是下一个存放元素的位置下标处
    	pst->capacity = 0;
    }
    
    //插入栈顶元素
    void STPush(ST* pst, STDataType x)
    {
    	assert(pst);
    
    	CheckCapacity(pst);
    
    	pst->a[pst->top] = x;
    	pst->top++;
    }
    
    //删除栈顶元素
    void STPop(ST* pst)
    {
    	assert(pst);
    	assert(pst->top > 0);//栈内不为空
    
    	pst->top--;
    }
    
    //获取栈顶元素
    STDataType STTop(ST* pst)
    {
    	assert(pst);
    	assert(pst->top > 0);
    
    	return pst->a[pst->top - 1];
    }
    
    //检查栈是否为空
    bool STEmpty(ST* pst)
    {
    	assert(pst);
    
    	return pst->top == 0;//为空就返回真
    }
    
    //栈的大小
    int STSize(ST* pst)
    {
    	assert(pst);
    
    	return pst->top;
    }
    
    //栈的销毁
    void STDestory(ST* pst)
    {
    	assert(pst);
    
    	free(pst->a);
    	pst->a = NULL;
    	pst->top = pst->capacity = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    1.2栈的概念

    允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端
    称为栈顶,另一端称为栈底
    。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
    在这里插入图片描述

    1.3栈的实现

    栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。

    在这里插入图片描述

    2.队列

    2.1队列的实现参考代码

    #include
    #include
    #include
    #include
    
    typedef int QDataType;
    typedef struct QueueNode
    {
    	QDataType val;
    	struct QueueNode* next;
    }QNode;
    
    typedef struct Queue
    {
    	QNode* phead;
    	QNode* ptail;
    	int size;
    }Queue;
    
    //队列的初始化
    void QueueInit(Queue* pq);
    
    //队尾入队列
    void QueuePush(Queue* pq, QDataType x);
    
    //队头出队列
    void QueuePop(Queue* pq);
    
    //获取队列尾部元素
    QDataType QueueBack(Queue* pq);
    
    //获取队列头部元素
    QDataType QueueFront(Queue* pq);
    
    //检测队列是否为空(为空返回非0,不为空返回0)
    bool QueueEmpty(Queue* pq);
    
    //销毁队列
    void QueueDestory(Queue* pq);
    
    //获取对列中有效元素个数
    int QueueSize(Queue* pq);
    
    #include"Queue.h"
    
    //队列的初始化
    void QueueInit(Queue* pq)
    {
    	assert(pq);
    
    	pq->phead = pq->ptail = NULL;
    	pq->size = 0;
    }
    
    //插入数据
    void QueuePush(Queue* pq, QDataType x)
    {
    	assert(pq);
    
    	QNode* newnode = (QNode*)malloc(sizeof(QNode));
    	if (newnode == NULL)
    	{
    		perror("QueuePush()::malloc()");
    		return;
    	}
    	newnode->val = x;
    	newnode->next = NULL;
    
    	if (pq->ptail == NULL)
    	{
    		pq->phead = pq->ptail = newnode;
    	}
    	else
    	{
    		pq->ptail->next = newnode;
    		pq->ptail = newnode;
    	}
    
    	pq->size++;
    }
    
    //队头出队列
    void QueuePop(Queue* pq)
    {
    	assert(pq);
    	assert(pq->phead);
    
    	QNode* del = pq->phead;
    	pq->phead = pq->phead->next;
    	free(del);
    	del = NULL;
    
    	if (pq->phead == NULL)
    		pq->ptail = NULL;
    
    	pq->size--;
    }
    
    //获取队列尾部元素
    QDataType QueueBack(Queue* pq)
    {
    	assert(pq);
    	assert(pq->phead);
    
    	return pq->ptail->val;
    }
    
    //获取队列头部元素
    QDataType QueueFront(Queue* pq)
    {
    	assert(pq);
    	assert(pq->phead);
    
    	return pq->phead->val;
    }
    
    //检测队列是否为空(为空返回非0,不为空返回0)
    bool QueueEmpty(Queue* pq)
    {
    	assert(pq);
    
    	return pq->phead == NULL;
    }
    
    //获取对列中有效元素个数
    int QueueSize(Queue* pq)
    {
    	assert(pq);
    
    	return pq->size;
    }
    
    //销毁队列
    void QueueDestory(Queue* pq)
    {
    	assert(pq);
    
    	QNode* cur = pq->phead;
    	while (cur)
    	{
    		QNode* del = cur;
    		cur = cur->next;
    		free(del);
    	}
    
    	pq->phead = pq->ptail = NULL;
    	pq->size = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148

    2.2队列的概念及结构

    队列只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头.
    在这里插入图片描述

    2.3队列的实现

    队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
    在这里插入图片描述

  • 相关阅读:
    MySQL 排名函数 RANK, DENSE_RANK, ROW_NUMBER
    Nginx+Keepalived+LVS集群实战
    免费小程序HTTPS证书
    Activity(页面)的生命周期
    用豆瓣电影和掌桥科研练习网页解析的三种方式——正则、Xpath和bs4
    uniapp路由跳转的六种方式
    中国人民大学与加拿大女王大学金融硕士——在金融领域里持续探索、成长
    数组18—push() :将一个或多个元素添加到数组的末尾
    简单的kafka&redis学习之kafka
    java基本web蓝桥杯名师工作室计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/2302_76561054/article/details/135785009