C++数据结构 乱搭配模板迭代

本文介绍了一种使用C++模板元编程技术实现栈数据结构的方法,详细阐述了其内部实现机制,并通过实例展示了如何操作栈。此外,还介绍了相关类如`sl`的实现,用于获取栈中元素和进行元素的遍历、增删等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

#include <string>
#include <fstream>
using namespace std;
template<class T>
class stack
{
private:
	class s
	{
	private:
		T *data;
		s * zuo;
		s *you;
	public:
		s() :data(0), next(0){}
		s(T*da):data(da),zuo(0),you(0){}
		s*Getzuo(){ return zuo; }
		s*Getyou(){ return you; }
		void Setzuo(s*p){ zuo = p; }
		void Setyou(s*p){ you = p; }
		void SetD(T *i){ data = i; }
		T *GetD(){ return data; }
	}*head,*end;
	int top;
public:
	stack() :top(0), head(0), end(0){}
	
	void push(T *i)
	{
		if (!head)
		{
			head = new s(i);
			end = head;
			top++;
			return;
		}
		s *p = new s(i);
		p->Setzuo(end);
		end->Setyou(p);
		end = p;
		top++;
		return;
	}
	T *pop()
	{
		if (!head->Getyou())
		{
			T *t = head->GetD();
			delete head;
			top--;
			head = end = NULL;
			return t;
		}
		T *t = head->GetD();
		head = head->Getyou();
		delete head->Getzuo();
		head->Setzuo(0);
		top--;
		return t;
	}
	bool Top()
	{
		if (head)
			return true;
		return false;
	}
	int tops(){ return top; }
	~stack()
	{
		while (head)
		delete	pop();
	}
	class sl;
	friend class sl;
	class sl
	{
	private:
		stack &st;
		int dex;
	public:
		sl(stack &is) :st(is), dex(0){}
		sl(stack &is,bool) :st(is), dex(is.tops()){}
		int Getlrn()const{ return st.tops(); }
		T &operator *()const 
		{ 
			s*p = st.head;
			
			for (int i = 1; i < dex;)
				{
					p = p->Getyou();
					i++;
				}
				return *(p->GetD());
		
			
			
		}
		friend ostream&operator<<(ostream &o, const sl&ss)
		{
			return o << *ss;
		}
		T operator++(int o)
		{
			if (dex < (st.top))
			{
				s *p = st.head;
				for (int i = 0; i < dex; i++)
				{
					p = p->Getyou();
				}
				++dex;
				return *(p->GetD());
			}
			else
				return 0;
		}
		T  operator++()
		{
			++dex;
			if (dex < st.top)
			{
				s *p = st.head;
				for (int i = 0; i < st.tops(); i++)
				{
					p = p->Getyou();
				}
				return *(p->GetD());
			}
			else
			{
				return 0;;
			}
		}
		T  operator[](const int i)
		{
			if (i < st.tops())
			{
				s *p = st.head;
				for (int j = 0; j < i; j++)
				{
					p = p->Getyou();
				}
				return *(p->GetD());
			}
			else
				return 0;
		}
		bool operator!=(const sl&s)
		{
			return dex != s.dex;
		}
		bool operator==(const sl &s)
		{
			return dex == s.dex;
		}
		sl operator+=(int i)
		{
			dex += 2;
			return *this;
		}
	};
	sl bing()
	{
		return sl(*this);
	}
	sl ends()
	{
		return sl(*this, true);
	}
};

class  dd
{
public:
	dd(){i = 0; }
	~dd(){}
	dd(int j) :i(j){}
	operator int(){ return i; }
	
	int get(){ return i; }
	void Set(int is){ i = is; }
private:
	int i;
};
template<class T>
class ns
{
public:
	ns() :p(0){}
	void push(T *i)
	{
		if (!p)
			p = new stack< T>;
		p->push(i);
	}
	T *pop()
	{
		return p->pop();
	}
	T  operator[](const int i)
	{
		
		return p->operatorp[](i);
	}
	~ns(){
		delete p;
	}
private:
	stack<T> *p;
};

void main111()
{
	//stack<dd>s;
	//string sr;
	//ifstream in("String.h");
	///*while (getline(in, sr))
	//	s.push(new string(sr));*/
	//for (int i = 0; i < 10; i++)
	//{
	//	s.push(new dd(i));
	//}
	//in.close();
	//stack<dd>::sl v = s.bing();
	//stack<dd>::sl vc = s.ends();
	//for (int i = 0; i < v.Getlrn(); i++)
	//{
	//	cout << v[i] << endl;;
	//}
	//cout << vc << endl;
	ns<dd>  sp;
	for (int i = 0; i < 10; i++)
	{
		sp.push(new dd(i));
	}
	dd *p;
	for (int i = 0; i < 10; i++)
	{
		p = sp.pop();
		cout << *p << endl;
		delete p;
	}


	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值