动态数组和链表设计栈(C++语言编写)

动态数组和链表设计栈

前言
本人菜鸟一枚,如有不对的地方,望指出!共同学习。
1. 动态数组设计顺序栈
直接上代码

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
template<class T>
void ChangeSize1D(T* &a, const int oldSize, const int newSize);

template<class T>
class MyStack
{
public:
	MyStack(int stackCapacity = 10);
	~MyStack();

	bool IsEmpty() const;
	T& Top() const;
	void Push(const T& item);
	void Pop();

private:
	T *stack;//动态创建一个数组
	int top;//记录栈顶的位置
	int capacity;

};


template<class T>
void ChangeSize1D(T* &a, const int oldSize, const int newSize)
{
	if (newSize < 0) throw"New length must be >=0";
	T* temp = new T[newSize];
	int number = min(oldSize, newSize);
	copy(a, a + number, temp);
	delete[] a;//在堆里开辟了一个新的区域,先由指针temp维护,然后释放a所指向的旧区域
	//然后让a指向temp所维护的区域,即新开辟的空间由两个指针共同维护
	a = temp;
}

template<class T>
MyStack<T>::MyStack(int stackCapacity) :capacity(stackCapacity)
{
	if (capacity < 1) throw"stack capacity must be >0";
	stack = new T[capacity];//动态创建一个数组
	top = -1;

}
template<class T>
MyStack<T>::~MyStack()
{
	delete[] stack;

}

template<class T>
inline bool MyStack<T>::IsEmpty() const
{
	return top == -1;

}

template<class T>
inline T& MyStack<T>::Top() const
{
	if (IsEmpty())  throw"stack is empty";
	return stack[top];
}

template<class T>
void MyStack<T>::Push(const T& item)
{
	//栈满了
	if (top == capacity - 1)
	{
		ChangeSize1D(stack, capacity, 2 * capacity);
		capacity *= 2;
	}
	stack[++top] = item;
}

template<class T>
void MyStack<T>::Pop()
{
	if (IsEmpty()) throw"stack is empty";
	//top--
	stack[top--].~T();//有可能堆栈里面保存的是类,对象减少以后,要把对象析构
}
int main()
{
	MyStack<nurbscurve> curve1;
	MyStack<string> myStringStack;

	MyStack<int> st;
	st.Push(1);
	st.Push(5);
	st.Push(10);
	st.Push(15);
	st.Push(20);
	cout << st.Top() << endl;
	st.Pop();
	cout << st.Top()<< endl;
	st.Pop();
	cout << st.Top() << endl;
	st.Pop();
	cout << st.Top() << endl;


	system("pause");
	return 0;
}

2. 链式栈

#include<iostream>
#include<algorithm>
using namespace std;
template<class T>
class LinkedStack;

template<class T>
class ChainNode
{
	friend class LinkedStack<T>;
private:
	ChainNode(const T& theData, ChainNode *n = 0)//节点里数据,新的节点所指向的节点
		:data(theData), link(n) {}//构造函数
	T data;
	ChainNode<T> *link;
};



template<class T>
class LinkedStack
{
public:
	LinkedStack() :top(0) {}
	~LinkedStack() { MakeEmpty(); }
	void MakeEmpty();
	bool IsEmpty() const;
	T& Top() const;
	void Push(const T& e);
	void Pop();


private:
	ChainNode<T> *top;

};



template<class T>
bool LinkedStack<T>::IsEmpty() const
{
	return top == 0;
}


//top指针指向先放入的数据
template<class T>
void LinkedStack<T>::Push(const T& e) 
{
	top = new ChainNode<T>(e, top);//新创建一个节点,指向原来的栈顶,调用构造函数

}
template<class T>
T& LinkedStack<T>::Top() const
{
	if (this->IsEmpty())
		throw"Stack is empty";
	return top->data;

}

template<class T>
void LinkedStack<T>::Pop() 
{
	if (this->IsEmpty())
		throw"Stack is empty";
	ChainNode<T> *delNode = top;//创建一个指针指向栈顶数据
	top = top->link;//栈顶向下移动
	delete delNode;//push是new了个新节点  pop时要delete
}


template<class T>
void LinkedStack<T>::MakeEmpty()
{
	while (!IsEmpty())
		Pop();
}
int main()
{
	cout << "链式栈测试ok!" << endl;
	LinkedStack<int> s;
	s.Push(10);
	cout << s.Top() << endl;
	s.Push(20);
	cout << s.Top() << endl;
	s.Push(30);
	cout << s.Top() << endl;

	s.Pop();
	cout << s.Top() << endl;


	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值