C語(yǔ)言實(shí)現(xiàn)數(shù)組棧的代碼示例
棧的概念及結(jié)構(gòu)
棧:一種特殊的線性表,其只允許在固定的一端進(jìn)行插入和刪除元素操作。進(jìn)行數(shù)據(jù)插入和刪除操作的一端稱為棧頂,另一端稱為棧底。棧中的數(shù)據(jù)元素遵守后進(jìn)先出LIFO(Last In First Out)的原則。
壓棧:棧的插入操作叫做進(jìn)棧/壓棧/入棧,入數(shù)據(jù)在棧頂。
出棧:棧的刪除操作叫做出棧。出數(shù)據(jù)也在棧頂。
棧的定義
typedef int STDataType; typedef struct Stack { STDataType* _a;//數(shù)組 int _top; // 棧頂,類似順序表中的_size int _capacity; // 容量 }Stack;
對(duì)棧的操作
棧初始化
_top可以初始化為0,此時(shí)棧頂元素是_top-1的位置
_top也可以初始化為1,此時(shí)棧頂元素就是_top的位置
初始化為0
初始化為1
// 初始化棧 void StackInit(Stack* ps) { assert(ps); ps->_a = NULL; ps->_capacity = 0; ps->_top = 0; }
壓棧(入棧)
// 入棧 void StackPush(Stack* ps, STDataType data) { assert(ps); //擴(kuò)容 if (ps->_capacity == ps->_top) { int newcapacity = ps->_capacity == 0 ? 4 : 2 * (ps->_capacity); STDataType* tmp = (STDataType*)realloc(ps->_a, newcapacity * sizeof(STDataType)); if (tmp == NULL) { perror("realloc fail"); return; } ps->_a = tmp; ps->_capacity = newcapacity; } ps->_a[ps->_top++] = data; }
出棧
void StackPop(Stack* ps) { assert(ps); assert(!StackEmpty(ps)); ps->_top--; }
取棧頂元素
STDataType StackTop(Stack* ps) { assert(ps); return ps->_a[ps->_top-1]; }
判斷棧是否為空
bool StackEmpty(Stack* ps) { assert(ps); return ps->_top == 0; }
棧的長(zhǎng)度
_top初始化為0,此時(shí)的_top的大小剛好就是棧的長(zhǎng)度
int StackSize(Stack* ps) { assert(ps); return ps->_top; }
棧銷毀
void StackDestroy(Stack* ps) { assert(ps); ps->_capacity = ps->_top = 0; free(ps->_a); ps->_a = NULL; }
完整總代碼
頭文件
#pragma once #include<stdio.h> #include<assert.h> #include<stdlib.h> #include<stdbool.h> // 支持動(dòng)態(tài)增長(zhǎng)的棧 typedef int STDataType; typedef struct Stack { STDataType* _a;//數(shù)組 int _top; // 棧頂,類似順序表中的_size int _capacity; // 容量 }Stack; // 初始化棧 void StackInit(Stack* ps); // 入棧 void StackPush(Stack* ps, STDataType data); // 出棧 void StackPop(Stack* ps); // 獲取棧頂元素 STDataType StackTop(Stack* ps); // 獲取棧中有效元素個(gè)數(shù) int StackSize(Stack* ps); // 檢測(cè)棧是否為空,如果為空返回非零結(jié)果,如果不為空返回0 bool StackEmpty(Stack* ps); // 銷毀棧 void StackDestroy(Stack* ps);
函數(shù)定義
#include"Stack.h" // 初始化棧 void StackInit(Stack* ps) { assert(ps); ps->_a = NULL; ps->_capacity = 0; ps->_top = 0; } // 入棧 void StackPush(Stack* ps, STDataType data) { assert(ps); //擴(kuò)容 if (ps->_capacity == ps->_top) { int newcapacity = ps->_capacity == 0 ? 4 : 2 * (ps->_capacity); STDataType* tmp = (STDataType*)realloc(ps->_a, newcapacity * sizeof(STDataType)); if (tmp == NULL) { perror("realloc fail"); return; } ps->_a = tmp; ps->_capacity = newcapacity; } ps->_a[ps->_top++] = data; } // 出棧 void StackPop(Stack* ps) { assert(ps); assert(!StackEmpty(ps)); ps->_top--; } // 獲取棧頂元素 STDataType StackTop(Stack* ps) { assert(ps); return ps->_a[ps->_top-1]; } // 獲取棧中有效元素個(gè)數(shù) int StackSize(Stack* ps) { assert(ps); return ps->_top; } // 檢測(cè)棧是否為空,如果為空返回非零結(jié)果,如果不為空返回0 bool StackEmpty(Stack* ps) { assert(ps); return ps->_top == 0; } // 銷毀棧 void StackDestroy(Stack* ps) { assert(ps); ps->_capacity = ps->_top = 0; free(ps->_a); ps->_a = NULL; }
測(cè)試
#include"Stack.h" void test() { Stack s; StackInit(&s); StackPush(&s, 1); StackPush(&s, 2); StackPush(&s, 3); StackPush(&s, 4); while (StackSize(&s)>0) { printf("%d ", StackTop(&s)); StackPop(&s); } StackDestroy(&s); } int main() { test(); return 0; }
到此這篇關(guān)于C語(yǔ)言實(shí)現(xiàn)數(shù)組棧的代碼示例的文章就介紹到這了,更多相關(guān)C語(yǔ)言數(shù)組棧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你了解C語(yǔ)言浮點(diǎn)數(shù)之間的比較規(guī)則
這篇文章主要介紹了魔性的float浮點(diǎn)數(shù)精度問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08二叉樹(shù)中葉子節(jié)點(diǎn)的統(tǒng)計(jì)和樹(shù)高問(wèn)題
今天小編就為大家分享一篇關(guān)于二叉樹(shù)中葉子節(jié)點(diǎn)的統(tǒng)計(jì)和樹(shù)高問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03C語(yǔ)言編程中常見(jiàn)的五種錯(cuò)誤及對(duì)應(yīng)解決方案
這篇文章主要給大家分享的是C語(yǔ)言編程中常見(jiàn)的五種錯(cuò)誤及對(duì)應(yīng)解決方案,詳細(xì)內(nèi)容就請(qǐng)跟小編一起進(jìn)入下面的文章內(nèi)容吧2021-10-10C++通過(guò)SQLiteSDK增刪改查的實(shí)現(xiàn)示例
SQLite是一種輕量級(jí)的嵌入式數(shù)據(jù)庫(kù),可以利用SQLiteSDK執(zhí)行數(shù)據(jù)庫(kù)的增刪改查操作,本文主要介紹了C++通過(guò)SQLiteSDK增刪改查,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03詳解計(jì)數(shù)排序算法及C語(yǔ)言程序中的實(shí)現(xiàn)
技術(shù)排序算法與我們普通接觸的冒泡排序和快速排序等基于元素比較的算法不同,在編程中通過(guò)C語(yǔ)言的數(shù)組能夠清除地表達(dá)出來(lái),這里我們就來(lái)詳解計(jì)數(shù)排序算法及C語(yǔ)言程序中的實(shí)現(xiàn)2016-07-07C++編程中逗號(hào)運(yùn)算符和條件運(yùn)算符的使用方法講解
這篇文章主要介紹了C++編程中逗號(hào)運(yùn)算符和條件運(yùn)算符的使用方法講解,需要的朋友可以參考下2016-01-01C語(yǔ)言中的函數(shù)指針基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了C語(yǔ)言中的函數(shù)指針基礎(chǔ)學(xué)習(xí)教程,包括函數(shù)指針作為參數(shù)來(lái)傳遞等重要知識(shí),需要的朋友可以參考下2016-04-04C++實(shí)現(xiàn)LeetCode(23.合并k個(gè)有序鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(23.合并k個(gè)有序鏈表),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07