網站首頁 編程語言 正文
關于棧
- 棧的有關理論在我另外一文中寫出,這篇文章只展示代碼實現;
- 數組棧非常容易實現,就不做其他敘述;
注:數據結構系列將持續更新,歡迎交流討論…
示例代碼
- 示例代碼中提供兩個構造函數實現數組棧;
- 棧的清空仍然采用邏輯置空;
- 棧的實現主要注意棧頂元素就可以了……
#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-10-19 Python?np.where()的詳解以及代碼應用_python
- 2022-04-08 WPF綁定Binding用法_基礎應用
- 2023-12-15 Linux系統設置多個IP地址、默認路由、指定路由、永久刪除默認路由
- 2023-04-04 C/C++關于實現CAN信號的獲取方法_C 語言
- 2022-05-10 一起來學習C++中remove與erase的理解_C 語言
- 2022-05-18 Qt?關于容器的遍歷迭代器的使用問題小結_C 語言
- 2022-02-07 virtualenvwrapper 解決安裝報錯,virtualenvwrapper 永久生效
- 2022-09-13 Nginx報錯104:Connection?reset?by?peer問題的解決及分析_nginx
- 最近更新
-
- 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同步修改后的遠程分支