#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");
}
C++数据结构 乱搭配模板迭代
最新推荐文章于 2025-03-15 21:26:42 发布