碼字不易板甘,對(duì)你有幫助 點(diǎn)贊/轉(zhuǎn)發(fā)/關(guān)注 支持一下作者
微信搜公眾號(hào):不會(huì)編程的程序圓
看更多干貨瓶籽,獲取第一時(shí)間更新
【數(shù)據(jù)結(jié)構(gòu)輕松學(xué)】系列 Github :https://github.com/hairrrrr/Date-Structure
本文的代碼已上傳至 Github
看更好的排版啸澡,閱讀原文:
https://mp.weixin.qq.com/s/GDHLwMmZHDyqpj9SLOlHNg
目錄
@[toc]
棧和隊(duì)列
一 棧
1.1 棧的概念及結(jié)構(gòu)
棧:一種特殊的線性表吝镣,其只允許在固定的一端進(jìn)行插入和刪除元素操作钝诚。進(jìn)行數(shù)據(jù)插入和刪除操作的一端稱為棧頂庐船,另一端稱為棧底银酬。棧中的數(shù)據(jù)元素遵守后進(jìn)先出LIFO(Last In First Out)的原則。
壓棧:棧的插入操作叫做進(jìn)棧/壓棧/入棧筐钟,入數(shù)據(jù)在棧頂
出棧:棧的刪除操作叫做出棧揩瞪。出數(shù)據(jù)也在棧頂
動(dòng)圖幫你理解入棧和出棧:
1.2 棧的實(shí)現(xiàn)
棧的實(shí)現(xiàn)一般可以使用數(shù)組或者鏈表實(shí)現(xiàn)
數(shù)組實(shí)現(xiàn)
//順序表實(shí)現(xiàn)棧
#include <stdlib.h>
#include <stdio.h>
#include<stdbool.h>
typedef int DataType;
typedef struct Stack
{
DataType* array;
size_t size;
size_t capacity;
}Stack;
void stackInit(Stack* s, size_t n);
void stackPush(Stack* s, DataType data);
DataType stackPop(Stack* s);
size_t stackSize(Stack* s);
bool stackIsEmpty(Stack* s);
bool stackIsFull(Stack* s);
void stackDestory(Stack* s);
如果你想了解一下鏈表實(shí)現(xiàn)拟蜻,請(qǐng)?jiān)?Github 上 【MOOC】章節(jié)內(nèi)尋找隆檀。
二 隊(duì)列
2.1 隊(duì)列的概念及結(jié)構(gòu)
隊(duì)列:只允許在一端進(jìn)行插入數(shù)據(jù)操作,在另一端進(jìn)行刪除數(shù)據(jù)操作的特殊線性表垫蛆,隊(duì)列具有先進(jìn)先出 FIFO(First In First Out)
入隊(duì)列:進(jìn)行插入操作的一端稱為隊(duì)尾
出隊(duì)列:進(jìn)行刪除操作的一端稱為隊(duì)頭
動(dòng)圖幫你理解入隊(duì)和出隊(duì):
2.2 隊(duì)列實(shí)現(xiàn)
隊(duì)列也可以數(shù)組和鏈表的結(jié)構(gòu)實(shí)現(xiàn)壹将,使用鏈表的結(jié)構(gòu)實(shí)現(xiàn)更優(yōu)一些嗤攻,因?yàn)槿绻褂脭?shù)組的結(jié)構(gòu),出隊(duì)列在數(shù)組頭上出數(shù)據(jù)瞭恰,效率會(huì)比較低屯曹。
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
typedef int DataType;
typedef struct Node
{
struct Node* next;
DataType data;
}Node;
typedef struct Queue
{
Node* front;
Node* rear;
int size;
}Queue;
void queueInit(Queue* q);
// 創(chuàng)建隊(duì)列結(jié)點(diǎn)
Node* creatNode(DataType data);
// 入隊(duì)
void queuePush(Queue* q, DataType data);
// 出隊(duì)
DataType queuePop(Queue* q);
// 判空
bool queueIsEmpty(Queue* q);
// 銷毀
void queueDestory(Queue* q);
本文參考資料:[1]https://blog.csdn.net/Baisitao_/article/details/102673498 [2]https://blog.csdn.net/shengqianfeng/article/details/99747783 [3]https://blog.csdn.net/YOLO97/article/details/82494221 [4]比特科技
查看【數(shù)據(jù)結(jié)構(gòu)輕松學(xué)】更詳細(xì)的目錄: https://github.com/hairrrrr/Date-Structure
不要忘記 star 呦~
歡迎大家在 評(píng)論區(qū)/私信 提出問(wèn)題/指正錯(cuò)誤,謝謝觀看惊畏。
我是程序圓恶耽,我們下次再見。