網站首頁 編程語言 正文
關于棧
- 棧的有關理論在我另外一文中寫出,這篇文章只展示代碼實現;
- 數組棧非常容易實現,就不做其他敘述;
注:數據結構系列將持續更新,歡迎交流討論…
示例代碼
- 示例代碼中提供兩個構造函數實現數組棧;
- 棧的清空仍然采用邏輯置空;
- 棧的實現主要注意棧頂元素就可以了……
#include <iostream>
using namespace std;
#define MAXSIZE 13
class Stack
{
public:
Stack();
Stack(int Size);
bool push(int data);
bool pop();
int getTop();
int IsEmpty();//判斷是否為空
void Empty();//置空
private:
int* m_pSt;
int m_top;
int m_curSize;
int m_Capacity;
};
int main()
{
Stack* pSt = new Stack;
pSt->push(1);
pSt->push(2);
pSt->push(3);
while (!pSt->IsEmpty())
{
cout << pSt->getTop() << " ";
pSt->pop();
}
return 0;
}
Stack::Stack()
{
this->m_pSt = new int[MAXSIZE];
m_top = -1;
m_curSize = 0;
m_Capacity = MAXSIZE;
}
Stack::Stack(int Size)
{
this->m_pSt = new int[Size];
m_top = -1;
m_curSize = 0;
m_Capacity = Size;
}
bool Stack::push(int data)
{
if (this->m_curSize >= this->m_Capacity || this->m_Capacity <= 0)
return false;
m_pSt[++m_top] = data;
m_curSize++;
return true;
}
bool Stack::pop()
{
if (m_curSize <= 0)
return false;
m_curSize--;
m_top--;
return true;
}
int Stack::getTop()
{
return this->m_pSt[m_top];
}
int Stack::IsEmpty()
{
return m_top == -1;
}
void Stack::Empty()
{
m_top = -1;
}
原文鏈接:https://blog.csdn.net/qq_46282869/article/details/126336641
相關推薦
- 2022-08-10 詳細講解Swift中的類型占位符_Swift
- 2023-03-20 C#如何遠程讀取服務器上的文本內容_C#教程
- 2022-08-06 SQL?Server備份數據庫的完整步驟_MsSql
- 2022-06-02 redis?sentinel監控高可用集群實現的配置步驟_Redis
- 2022-12-02 Linux下的自動化構建工具之make/makefile的用法詳解_linux shell
- 2022-04-30 C#實現鼠標消息捕獲_C#教程
- 2022-06-20 正則表達式之字符串模式匹配實例詳解_正則表達式
- 2022-07-22 git提交代碼設置某些文件不可上傳
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支