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)文章
windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境
這篇文章主要介紹了windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Qt中定時(shí)器 QTimerEvent 和 QTimer的使用
Qt框架中的定時(shí)器功能主要包括兩種實(shí)現(xiàn)方式,包括QTimerEvent和QTimer,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
C++入門之vector的底層實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了C++入門之vector的底層實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11

