以C++实现的数据结构与算法(栈)


1、顺序栈


顺序栈头文件,可直接调用

#pragma once
#include<iostream>
using namespace std;

template<class T>
class LinearStack {
public:
	LinearStack(int maxSize);
	~LinearStack();
	bool isEmpty();
	bool isFull();
	bool push(const T&x);
	bool Top(T&x);
	bool pop(T&x);
	void outPut(ostream&cout)const;
private:
	int top;//表示栈顶
	int maxSize;//栈中最大元素个数
	T*element;//一位动态数组
};
//实现构造函数
template<class T>
LinearStack<T>::LinearStack(int maxSize) {
	this->maxSize = maxSize;
	element = new T[maxSize];
	top = -1;
}
//实现析构函数
template<class T>
LinearStack<T>::~LinearStack() {
	delete[]element;
}
//实现判断是否为空
template<class T>
bool LinearStack<T>::isEmpty() {
	return top == -1;
}
//实现判断栈是否为满
template<class T>
bool LinearStack<T>::isFull() {
	return top + 1 == maxSize;
}
//实现进栈
template<class T>
bool LinearStack<T>::push(const T&x) {
	if (isFull()) {
		return false;
	}
	else {
		top++;
		element[top] = x;
		return true;
	}
}
//实现求栈顶元素
template<class T>
bool LinearStack<T>::Top(T&x) {
	if (isEmpty()) {
		return false;
	}
	else {
		x = element[top];
		return true;
	}
}
//实现出栈
template<class T>
bool LinearStack<T>::pop(T&x) {
	if (isEmpty()) {
		return false;
	}
	else {
		x = element[top];
		top--;
		return true;
	}
}
//重载
template<class T>
void LinearStack<T>::outPut(ostream&cout)const {
	for (int i = 0; i <=top; i++)
	{
		cout << element[i] << endl;
	}
}
template<class T>
ostream&operator<<(ostream&cout, const LinearStack<T>&x) {
	x.outPut(cout);
	return cout;
}

顺序栈的应用(除基取余法)

//顺序栈的应用(除基取余法)
#include<iostream>
using namespace std;
#include"stack.h"

void conversion(int,int);//转换函数

int main() {
	int n, base;
	cout << "请输入十进制数:";
	cin >> n;
	cout << "请输入要转换的进制的基数:";
	cin >> base;
	conversion(n,base);
	cout << endl;

	system("pause");
	return 0;
}
void conversion(int n,int base) {
	int x, y;
	y = n;
	LinearStack<int>s(100);
	while (y)
	{
		s.push(y%base);
		y = y / base;
	}
	cout << "十进制数" << n << "转换为" << base << "进制为:";
	while (!s.isEmpty()) {
		s.pop(x);
		cout << x;
	}
}

2、链式栈

头文件可直接调用

#pragma once
#include<iostream>
using namespace std;

//节点类
template<class T>
class LinkNode {
	template<class T>
	friend class LinearStack;//将链式表类声明为友类
public:
	LinkNode() {
		next = NULL;
	}
private:
	T data;	//节点元素
	LinkNode<T>*next;//指向下一个节点的指针
};
template<class T>
class LinearStack {
public:
	LinearStack();
	~LinearStack();
	bool isEmpty()const;
	bool push(const T&x);
	bool Top(T&x);
	bool pop(T&x);
	void outPut(ostream&cout)const;
private:
	LinkNode<T>*top;//表示栈顶
	int size;
};
//实现构造函数
template<class T>
LinearStack<T>::LinearStack() {
	top = NULL;
	size = 0;
}
//实现析构函数
template<class T>
LinearStack<T>::~LinearStack() {
	T x;
	while (top != NULL) {
		pop(x);
	}
}
//判断栈是否为空
template<class T>
bool LinearStack<T>::isEmpty()const {
	return top == NULL;
}
//实现进栈
template<class T>
bool LinearStack<T>::push(const T&x) {
	LinkNode<T>*p = new LinkNode<T>();
	if (p == NULL) {
		return false;
	}
	else {
		p->data = x;
		p->next = top;
		top = p;
		size++;
		return true;
	}
}
//实现求栈顶的元素
template<class T>
bool LinearStack<T>::Top(T&x) {
	if (isEmpty()) {
		return false;
	}
	else {
		x = top->data;
		return true;
	}
}
//实现出栈
template<class T>
bool LinearStack<T>::pop(T&x) {
	LinkNode<T>*p;
	if (isEmpty()) {
		return false;
	}
	else {
		x = top->data;
		p = top;
		top = top->next;
		delete p;
		size--;
		return true;
	}
}
//重载
template<class T>
void LinearStack<T>::outPut(ostream&cout)const {
	LinkNode<T>*p;
	p = top;
	for (int i = 0; i < size; i++)
	{
		cout << p->data << endl;
		p = p->next;
	}
}
template<class T>
ostream&operator<<(ostream&cout,const LinearStack<T>&x) {
	x.outPut(cout);
	return cout;
}

主函数调用

#include<iostream>
using namespace std;
#include"linearStack.h"

int main() {
	
	LinearStack<int>stack;
	cout << "是否为空:" << stack.isEmpty() << endl;
	for (int i = 0; i < 5; i++)
	{
		stack.push(i);
	}
	cout << "是否为空:" << stack.isEmpty() << endl;
	cout << stack << endl;
	int x;
	stack.Top(x);
	cout << "栈顶元素为:" << x << endl;
	while (stack.Top(x)!=NULL)
	{
		stack.pop(x);
		cout << x << endl;
	}
	cout << "是否为空:" << stack.isEmpty() << endl;
	cout << stack << endl;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值