日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

數據結構之數組棧的實現

作者:assassin$ 更新時間: 2022-08-15 編程語言
關于棧
  • 棧的有關理論在我另外一文中寫出,這篇文章只展示代碼實現;
  • 數組棧非常容易實現,就不做其他敘述;
    注:數據結構系列將持續更新,歡迎交流討論…
示例代碼
  • 示例代碼中提供兩個構造函數實現數組棧;
  • 棧的清空仍然采用邏輯置空;
  • 棧的實現主要注意棧頂元素就可以了……
#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

欄目分類
最近更新