C++栈

C++栈

一、栈的特点

栈是一种线性储存结构;
栈是一种后进先出(last-in-first-out)(LIFO)的数据结构;
存储数据只能限定在栈顶进行插入和删除操作。

二、栈的相关概念

(1)栈顶与栈底:允许元素插入与删除的一端称为栈顶,另一端称为栈底。
(2)压栈:栈的插入操作,叫做进栈,也称压栈、入栈。
(3)弹栈:栈的删除操作,也叫做出栈。

三、栈的储存方式

(1)基于数组的栈——以数组为底层数据结构时,通常以数组头为栈底,数组头到数组尾为栈顶的生长方向。
(2)基于单链表的栈——以链表为底层的数据结构时,以链表头为栈顶,便于节点的插入与删除,压栈产生的新节点将一直出现在链表的头部。

四、栈的常用操作及抽象类

(1)压栈,或入栈,通常命名为push
(2)弹栈,或出栈,通常命名为pop
(3)求栈的大小size
(4)返回栈顶元素top

template<class T>
class stack
{
public:
	virtual ~stack() {}
	virtual int size() const = 0;
	virtual T& top() = 0;
	virtual void pop() = 0;
	virtual void push(const T& theElement) = 0;
};

五、基于数组的栈

template<class T>
class arrayStack
{
public:
	arrayStack(int initialCapacity = 10);
	~arrayStack() { delete[] stack; }
	int size() const
	{
		return stackTop + 1;
	}
	T& top()
	{
		if (stackTop == -1)
			cout << "Empty stack" << endl;
		return stack[stackTop];
	}
	void pop()
	{
		if (stackTop == -1)
			cout << "Empty stack" << endl;
		//stack[stackTop--].~T();
		stack[stackTop].~T();
		stackTop -= 1;
	}
	void push(const T& theElement);
private:
	int stackTop;                 //当前栈顶
	int arrayLength;              //栈容量
	T* stack;                    //元素数组
};

构造函数

template<class T>
arrayStack<T>::arrayStack(int initialCapacity)
{
	if (initialCapacity < 1)
		cout << "Initial capacity must > 0! " << endl;
	arrayLength = initialCapacity;
	stack = new T[arrayLength];
	stackTop = -1;
}

入栈成员函数

template<class T>
void arrayStack<T>::push(const T& theElement)
{
	if (stackTop == arrayLength - 1)
	{
		int num = 2 * arrayLength;
		T* temp = new T[num];
		copy(stack, stack + arrayLength, temp);
		delete[]stack;
		stack = temp;
		arrayLength *= 2;
	}
	stackTop += 1;
	stack[stackTop] = theElement;
}

测试主函数:

int main()
{
	arrayStack<int> s(3);
	s.push(1);
	s.push(2);
	cout << "Length= " << s.size() << endl;
	cout << "Top=" << s.top() << endl;
	s.push(4);
	cout << "Length= " << s.size() << endl;
	cout << "Top=" << s.top() << endl;
	s.push(8);
	cout << "Length= " << s.size() << endl;
	cout << "Top=" << s.top() << endl;
	s.pop();
	cout << "Length= " << s.size() << endl;
	cout << "Top=" << s.top() << endl; 
	s.pop();
	cout << "Length= " << s.size() << endl;
	cout << "Top=" << s.top() << endl;
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

六、基于单链表的栈

//链结构体节点
template <class T>
struct chainNode
{
	T element;                                      //存储数据
	chainNode<T>* next;                             //下一个节点的地址
	chainNode(T a, chainNode<T>* b)
	{
		this->element = a;
		this->next = b;
	}
};
template<class T>
class chainStack
{
public:
	chainStack(int initialCapacity = 10)
	{
		stackTop = NULL;
		stackSize = 0;
	}
	~chainStack();
	int size() const
	{
		return stackSize;
	}
	T& top()
	{
		if (stackSize == 0)
			cout << "The stack is empty!" << endl;
		return stackTop->element;
	}
	void pop();
	void push(const T& theElement)
	{
		stackTop = new chainNode<T>(theElement, stackTop);
		stackSize++;
	}
private:
	chainNode<T>* stackTop;        //栈顶指针
	int stackSize;                  //栈中元素个数
};

析构函数

template<class T>
chainStack<T>::~chainStack()
{
	while (stackTop != NULL)
	{
		chainNode<T>* nextNode = stackTop->next;
		delete stackTop;
		stackTop = nextNode;
	}
}

pop出栈函数

template<class T>
void chainStack<T>::pop()
{
	if (stackSize == 0)
		cout << "The stack is empty!" << endl;
	chainNode<T>* nextNode = stackTop->next;
	delete stackTop;
	stackTop = nextNode;
	stackSize--;
}

测试主函数

int main()
{
	chainStack<int> cs(3);
	cs.push(1);
	cout << "Size = " << cs.size() << endl;
	cout << "Top = " <<cs.top()<< endl;
	cs.push(2);
	cs.push(3);
	cout << "Size = " << cs.size() << endl;
	cout << "Top = " << cs.top() << endl;
	cs.pop();
	cout << "Size = " << cs.size() << endl;
	cout << "Top = " << cs.top() << endl;
	cs.pop();
	cout << "Size = " << cs.size() << endl;
	cout << "Top = " << cs.top() << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值