用兩種方法實現棧---順序表和鏈表

棧是一種先進後出的數據結構,棧中的數據是先進後出的(First In Last Out, FILO)。棧只有一個出口,允許新增元素(只能在棧頂上增加)、移出元素(只能移出棧頂元素)、取得棧頂元素等操作。在STL中,棧是以別的容器作爲底部結構,再將接口改變,使之符合棧的特性就可以了。棧裏面常用的函數有push()(增),pop()(刪),size()(求大小),top()(頂部)和empty()(空),

下面是用順序表實現棧的各種函數

順序表代碼實現:

#include <iostream>
#include <assert.h>
using namespace std;


template<typename T>
class Stack
{
public:
	Stack()
		:_a(NULL)
		, _top(0)
		, _capacity(0)
	{}

	~Stack()
	{
		if (_a)
		{
			delete[] _a;
		}
	}

public:

	void _CheckCapacity()
	{
		if (_a == NULL)
		{
			_capacity = 3;
			_a = new T[_capacity];
			return;
		}

		if (_top == _capacity)
		{
			_capacity *= 2;
			T* tmp = new T[_capacity];
			for (size_t i = 0; i < _top; i++)
			{
				tmp[i] = _a[i];
			}

			delete[] _a;
			_a = tmp;
		}

	}

	void Push(const T& x)
	{
		_CheckCapacity();
		_a[_top++] = x;
	}

	void Pop()
	{
		assert(_top > 0);
		--_top;
	}

	size_t Size()
	{
		return _top;
	}

	bool Empty()
	{
		return _top == 0;
	}

	T& Top()
	{
		return _a[_top];
	}


	
private:
	T* _a;
	size_t _top;
	size_t _capacity;
};

鏈表實現棧:

鏈表實現代碼:

#include <iostream>
#include <assert.h>
using namespace std;

template<typename T>
struct Node
{
	Node(const T& x)
		:_data(x)
		,_next(NULL)
	{}

	T _data;
	Node<T>* _next;
};

template<typename T>
class Stack
{
public:
	Stack()
		:_top(NULL)
		,_base(NULL)
		,_size(0)
	{}

	~Stack()
	{}

	void Push(const T& x)
	{
		if (_top == NULL)
		{
			_top = new Node<T>(x);
			_base = _top;
		}
		else
		{
			
			_top->_next = new Node<T>(x);
			_top = _top->_next;
		}
		++_size;
	}

	void Pop()
	{
		assert(_top);
		
		Node<T>* cur = _base;
		
		while (cur->_next != _top)
		{
			if (cur == NULL)
			{
				printf("Stack is empty!\n");
				return;
			}
			cur = cur->_next;
		}

		delete _top;
		_top = cur;
		--_size;
	}

	T& Top()
	{
		return _top->_data;
	}
	bool Empty()
	{
		return _top == NULL;
	}
	size_t Size()
	{
		return _size;
	}


private:
	Node<T>* _top;
	Node<T>* _base;
	size_t _size;
};


測試代碼:

test.cpp

void Test()
{
	Stack<int> s1;
	s1.Push(0);
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	s1.Push(5);

	s1.Pop();
	s1.Pop();
	s1.Pop();

	int top = s1.Top();
	size_t size = s1.Size();
	cout << s1.Empty() << endl;


}

int main()
{
	Test();
	system("pause");
	return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章