栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 栈(Stack)的指针实现:构造析构、相关操作及运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 栈(Stack)的指针实现:构造析构、相关操作及运算符重载

我们先来看一下Stack要实现的操作,再附上相关代码,基本上和上一篇单链表差不多

目录

一、概述:

二、构造函数与析构函数:

1.默认构造函数:

2.复制构造函数:

3.析构函数:

三、相关操作:

1.判空empty():

2.压入新数据push():

3.栈顶数据top():

4.删除栈顶pop():

5.打印栈的数据display():

四、操作符重载:

1.=:

2. <<:

> :">3. >> :


一、概述:

分为三部分:1.构造析构 2.相关操作 3.运算符重载

template class 
Stack
{
	//运算符<<和 >> 的重载
    template friend ostream & operator << (ostream & out,const Stack & current);
    template friend istream & operator >> (istream & in, Stack & currrent);
public:
	//构造析构及=重载
	Stack();
	Stack(const Stack & origin);
	~Stack();
	Stack & operator=(const Stack & origin);

	bool empty() const;//判空
	void push(T newdata);//栈顶压入数据
	T top() const;//返回栈顶数据
	bool pop();//弹出栈顶数据
	void display(ostream & out) const;//输出栈的数据
private:
	class Node 
	{
	public://***
		T data;
		Node * next;
		//我们认为栈中没有空节点,所以data在创建时要被赋值,没有默认值
		Node(T newdata, Node * newpointer = NULL) :data(newdata),next(newpointer){};
	};
	typedef Node * Nodepointer;//Node * 别名Nodepointer

	Nodepointer  mytop;
};

二、构造函数与析构函数:

1.默认构造函数:

创建一个空栈,没有节点

template
Stack::Stack() :mytop(NULL) {};

2.复制构造函数:
template
Stack::Stack(const Stack & origin)
{
	if (origin.empty())//当origin为空时 
    {
		mytop = NULL;
	}
	else {
        //一个一个复制节点
		mytop = new Node(origin.mytop->data);
		Nodepointer originptr = origin.mytop->next;
		Nodepointer temp = mytop;

		while (originptr != NULL)
		{
			temp->next = new Node(originptr->data);
			temp = temp->next;
			originptr = originptr->next;
		}
	}
}

3.析构函数:
 
template
Stack::~Stack()
{
	Nodepointer nextptr = NULL;//记录指向 要将被删除节点的next,用于找到下一个节点
	Nodepointer temp = mytop;//用于遍历的一个临时指针
	while (temp != NULL)
	{
		nextptr = temp->next;
		delete temp;
		temp = nextptr;
	}
	mytop = NULL;
}

三、相关操作:

1.判空empty():
template
bool Stack:: empty() const
{
	return (mytop == NULL);
}

2.压入新数据push():
template
void Stack::push(T newdata)
{
	mytop = new Node(newdata, mytop);//mytop指向新创建的节点,并且该节点的next为原来的mytop
}

3.栈顶数据top():
template
T Stack::top() const
{
	if (empty())//若空,则返回错误     
    {
		cerr << "The Stack is empty";
		exit(1);
	}
	else {
		return mytop->data;
	}
}

4.删除栈顶pop():
template
bool Stack::pop()
{
	if (empty()) {
		cerr << "The Stack is empty";
		exit(1);
	}
	else {
		Nodepointer temp = mytop->next;//记录栈顶的next,一会赋给mytop
		delete mytop;
		mytop = temp;
		return true;
	}
}

5.打印栈的数据display():
template
void Stack::display(ostream & out)const
{
	Nodepointer temp = mytop;
	while (temp != NULL)
	{
		out << temp->data;
		temp = temp->next;
	}
	out << endl;
}

四、操作符重载:

1.=:
template
Stack & Stack::operator= (const Stack & origin)
{
	this->~Stack();//先把当前的数据删除
    //以下代码与复制构造函数一致
    //但是复制构造函数不能在这里使用,所以要粘贴一份
	if (origin.empty()) 
    {
		mytop = NULL;
	}
	else {
		mytop = new Node(origin.mytop->data);
		Nodepointer originptr = origin.mytop->next;
		Nodepointer temp = mytop;
		while (originptr != NULL)
		{
			temp->next = new Node(originptr->data);
			temp = temp->next;
			originptr = originptr->next;
		}
	}
	return *this;
}

2. <<:
template
ostream & operator << (ostream & out,const Stack & current)
{
    //下面一行代码在VS2017上可以运行,可能在其他编译器上不可以
    //如果不行的话 改为Node * temp = current.mytop;
	Stack::template Nodepointer temp = current.mytop;//***不加template会引发error :C7510
	while (temp != NULL)
	{
		out << temp->data << " ";
		temp = temp->next;
	}
	out << endl;
	return out;
}

3. >> :
template
istream & operator >> (istream & in, Stack & current)
{
	T newdata;
	cout << "请输入要压入栈的数据,以CTRL + Z结束:";
	while (cin >> newdata)
	{
		current.push(newdata);
	}
	return in;
}


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号