【数据结构】——栈和队列
目录
1.栈
1.1栈的概念及结构
1.2栈的实现
1.2.1具体实现
Stack.h
栈初始化
栈销毁
入栈
出栈
取栈顶数据
判空
栈中有效元素的个数
全部Stack.c的代码
测试Test.c代码
2.队列
2.1队列的概念及结构
2.2队列的实现
Queue.h
队列初始化
队列销毁
队尾入队列(尾插)
队头出队列(头删 )
获取队头元素
获取队尾元素
判空
获取队列有效元素个数
完整的Queue.c代码
测试代码Test.c
1.栈
1.1栈的概念及结构
栈:一种特殊的线性表,它只允许在固定的一端(栈顶
)进行插入和删除元素的操作。进行插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵循后进先出(Last In First Out)的原则。
1.2栈的实现
1.2.1具体实现
Stack.h
从如下的几个方面实现栈的功能,看代码。
#pragma once#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>typedef int STDatatype;
typedef struct Stack//栈
{STDatatype* a;int capacity;int top; // 初始为0,表示栈顶位置下一个位置的下标
}ST;//栈初始化
void StackInit(ST* ps);
//栈销毁
void StackDestroy(ST* ps);
//压栈,入栈
void StackPush(ST* ps, STDatatype x);
//出栈
void StackPop(ST* ps);
//取栈顶数据
STDatatype StackTop(ST* ps);
//判空
bool StackEmpty(ST* ps);
//栈中有效元素个数
int StackSize(ST* ps);
栈初始化
void StackInit(ST* ps)
{assert(ps);//方法1//ps->a = NULL;//ps->top = 0;//ps->capacity = 0;//方法2ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);if (ps->a == NULL){perror("malloc fail");exit(-1);}ps->top = 0;ps->capacity = 4;
}
栈销毁
void StackDestroy(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 = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity *= 2;}//先放数据,top再++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);/*if (ps->top == 0){return true;}else{return false;}*/return ps->top == 0;
}
栈中有效元素的个数
int StackSize(ST* ps)
{assert(ps);return ps->top;
}
全部Stack.c的代码
void StackInit(ST* ps)
{assert(ps);//ps->a = NULL;//ps->top = 0;//ps->capacity = 0;ps->a = (STDatatype*)malloc(sizeof(STDatatype) * 4);if (ps->a == NULL){perror("malloc fail");exit(-1);}ps->top = 0;ps->capacity = 4;
}void StackDestroy(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 = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity *= 2;}//先放数据,top再++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);/*if (ps->top == 0){return true;}else{return false;}*/return ps->top == 0;
}int StackSize(ST* ps)
{assert(ps);return ps->top;
}
测试Test.c代码
#include "Stack.h"void TestStack1()
{ST st;StackInit(&st);StackPush(&st, 1);StackPush(&st, 2);StackPush(&st, 3);StackPush(&st, 4);StackPush(&st, 5);printf("size:%d\n", StackSize(&st)); // 不关心底层实现//printf("size:%d\n", st.top); // 关心printf("size:%d\n", st.top + 1); // 关心StackPop(&st);StackPop(&st);StackPop(&st);StackPop(&st);StackPop(&st);//StackPop(&st);//printf("%d\n", StackTop(&st));StackDestroy(&st);
}int main()
{TestStack1();return 0;
}
2.队列
2.1队列的概念及结构
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。
队列具有先进先出 FIFO(First In First Out)
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
PS:队列和栈几乎是相反的。
2.2队列的实现
实现队列更适合选择用单链表,而不是用数组,数组不适合头插尾插,效率低。
Queue.h
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>typedef int QDataType;
//链式结构:表示队列
typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QNode;
//队列的结构
typedef struct Queue
{QNode* head;QNode* tail;int size;
}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);
队列初始化
void QueueInit(Queue* pq)
{assert(pq);pq->head = NULL;pq->tail = NULL;pq->size = 0;
}
队列销毁
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);//del = NULL;}pq->head = pq->tail = NULL;pq->size = 0;
}
队尾入队列(尾插)
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");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;}pq->size++;
}
队头出队列(头删 )
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);}pq->size--;
}
获取队头元素
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 && pq->tail == NULL;
}
获取队列有效元素个数
int QueueSize(Queue* pq)
{assert(pq);//方法1/*int size = 0;QNode* cur = pq->head;while (cur){cur = cur->next;++size;}return size;*///方法2return pq->size;
}
完整的Queue.c代码
#include "Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->head = NULL;pq->tail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);//del = NULL;}pq->head = pq->tail = NULL;pq->size = 0;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");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;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);}pq->size--;
}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 && pq->tail == NULL;
}// 1G = 1024MB
// 1024MB = 1024*1024KB
// 1024*1024KB = 1024*1024*1024Byteint QueueSize(Queue* pq)
{assert(pq);//方法1/*int size = 0;QNode* cur = pq->head;while (cur){cur = cur->next;++size;}return size;*///方法2return pq->size;
}
测试代码Test.c
#include "Queue.h"void TestQueue()
{//Queue q1;//Queue q2;//QueueInit(&q1);//QueueInit(&q2);//QueueDestroy(&q1);//QueueDestroy(&q2);Queue q;QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);printf("%d ", QueueFront(&q));QueuePop(&q);QueuePush(&q, 3);QueuePush(&q, 4);printf("%d\n", QueueSize(&q));printf("%d\n", QueueEmpty(&q));printf("%d\n", QueueFront(&q));printf("%d\n", QueueBack(&q));while (!QueueEmpty(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}printf("\n");printf("%d\n", QueueSize(&q));printf("%d\n", QueueEmpty(&q));//printf("%d\n", QueueFront(&q));//printf("%d\n", QueueBack(&q));QueueDestroy(&q);
}int main()
{//TestStack1();TestQueue();return 0;
}
【数据结构】——栈和队列相关推荐
- 数据结构栈和队列_使您的列表更上一层楼:链接列表和队列数据结构
数据结构栈和队列 When you want to store several elements somewhere in a program, the go-to data type is an a ...
- 数据结构栈与队列的应用之汽车轮渡问题——自己的一些理解
本题摘自王道数据结构栈与队列的应用的课后题,题目如下: 某汽车轮渡口,过江渡船每次能载10辆汽车过江.过江车辆分为客车类和货车类,上渡船有如下规定:同类车先到先上船,客车先于货车上船,且每上4辆客车, ...
- 数据结构——栈与队列相关题目
数据结构--栈与队列相关题目 232. 用栈实现队列 思路 225. 用队列实现栈 1.两个队列实现栈 2.一个队列实现栈 20. 有效的括号 思路 1047. 删除字符串中的所有相邻重复项 思路 1 ...
- 第十章 基本数据结构——栈和队列
摘要 本章介绍了几种基本的数据结构,包括栈.队列.链表以及有根树,讨论了使用指针的简单数据结构来表示动态集合.本章的内容对于学过数据结构的人来说,没有什么难处,简单的总结一下. 1.栈和队列 栈和队列 ...
- 大话数据结构-栈与队列
文章知识点来至于大话数据结构里边章节知识, 这篇主要介绍栈与队列在计算机中存储形式, 以及在某些算法领域中对栈和队列的相关应用.章节最后介绍了著名的逆波兰表达式, 以及通过算法来实现该表达式的运算过程 ...
- 数据结构——栈与队列操作(用栈模拟队列)
[栈与队列操作] 问题描述:假设有两个长度相同的栈 S1,S2,已知以下入栈.出栈.判栈满和判栈空操作: void Push(S,x); Elemtype Pop(S); bool StackFull ...
- 【数据结构-栈和队列】详解栈和队列(代码+STL+原理)
一.栈的应用 栈是一种先进后出(FILO)的数据结构 1.1 栈的操作实现 清空(clear): // 栈的清空操作就是把栈顶top置为-1 void clear(){top=-1; } // 清空栈 ...
- 算法与数据结构 -- 栈与队列(四)
栈与队列定义了数据的操作 一.栈 栈是一种先入先出的数据结构.可以用顺序表实现,也可以用链表实现 栈操作 判断是否为空 压栈.入栈push 出栈 pop 返回栈顶元素 peek 栈的元素个数 # co ...
- 六十二、数据结构栈和队列的相互实现
@Author:Runsen 编程的本质来源于算法,而算法的本质来源于数学,编程只不过将数学题进行代码化. ---- Runsen 算法,一门既不容易入门,也不容易精通的学问. 栈和队列都是用来保存数 ...
- 数据结构栈和队列以及常见算法题
栈 概念:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作.进行数据插入和删除操作的一端称为栈顶,另一端称为栈底.栈中的数据元素遵守后进先出LIFO(Last In First Out)的 ...
最新文章
- 通过自动回复机器人学Mybatis---加强版
- EntityModelStudio系列教程5--EMLib框架之Eql
- CVE-2019-8660 iMessage 漏洞复现
- Java实现:月,日,年,周,访问量统计
- git push时报错fatal: Could not read from remote repository.
- 爱奇艺如何开启两指双击触发奇观功能
- adb 51 android.rules,使用51-android-rules解决ubuntu上不识别 android手机的问题
- echart横坐标_echart横坐标显示问题
- ORACLE SQL优化大全
- 无人机在土地测绘中的应用
- 【学习笔记】成功解决:(字体问题)Package fontspec Error: The font “STXingkai“ cannot be found. \makecover
- Java实现一个简易联网坦克对战小游戏
- java jsp网页无法显示_jsp网页显示不了
- 联想ThinkBook 16+ 2023款 评测
- 华为认证的含金量高吗?
- Finished with error: ProcessException: ProcessXXXXXXXexited abnormally 的解决方法
- java des ecb_DES ECB加解密的Java实现
- h3c.服务器 装系统,h3c服务器u盘安装linux系统安装
- Test,Evaluate_gpu 修改,自动跑完你要的epoch
- GPS接收机设计(5)——定位解算