//随便写了一些, 没经过严格测试, 你自己好好看看, 不要总让别人做作业, 这个习惯不好
#include
using namespace std;
class TStackNode
{
public:
int Value;
TStackNode *Prior, *Next;
TStackNode(){ Value=0; Prior=Next=NULL; }
TStackNode(int i){ Value=i; Prior=Next=NULL; }
TStackNode(int i, TStackNode *p);
};
class TMyStack
{
public:
void Push(int x);
bool Pop(int *px);
int Find(int x, TStackNode **ppNode=NULL);
int Count(void);
void Clear(void);
bool IsEmpty(void) { return !Tail; }
TStackNode *WalkNext(TStackNode *FromNode){ return FromNode?FromNode->Next:Head; }
TMyStack() { Tail=NULL; Head=NULL; }
~TMyStack() { Clear(); }
private:
TStackNode *Tail,*Head;
};
TStackNode::TStackNode(int i, TStackNode *p)
{
Value=i;
Prior=p;
Next=p?p->Next:NULL;
if(p)p->Next=this;
if(Next)Next->Prior=this;
}
void TMyStack::Push(int x)
{
Tail=new TStackNode(x,Tail);
if(!Head)Head=Tail;
}
bool TMyStack::Pop(int *px)
{
if(Tail)
{
if(px)*px=Tail->Value;
TStackNode *nd=Tail;
Tail=Tail->Prior;
if(Tail)
Tail->Next=NULL;
else
Head=NULL;
delete nd;
return true;
}
return false;
}
int TMyStack::Find(int x, TStackNode **ppNode)
{
int i=0;
for(TStackNode *nd=Head; nd; nd=nd->Next)
{
i++;
if(nd->Value==x)
{
if(ppNode)*ppNode=nd;
break;
}
}
return i;
}
int TMyStack::Count(void)
{
int n=0;
for(TStackNode *nd=Tail; nd; nd=nd->Prior)
n++;
return n;
}
void TMyStack::Clear(void)
{
TStackNode *nd=Tail;
while(nd)
{
TStackNode *x = nd;
nd = nd->Prior;
delete x;
}
Head=Tail=NULL;
}
int main(int argc, char* argv[])
{
TMyStack OddStack, EvenStack;
TStackNode *lpnd;
cout<<"Test stack class, Copyright (C) Victor Chen"< cout<<"Input 20 integers please..."<
int n;
for(int i=0; i<20; i++)
{
cout<<"integer["< cin>>n;
if(n&1)
OddStack.Push(n);
else
EvenStack.Push(n);
}
lpnd=NULL;
cout< while((lpnd=OddStack.WalkNext(lpnd))!=NULL)
cout<<" "<Value;
lpnd=NULL;
cout< while((lpnd=EvenStack.WalkNext(lpnd))!=NULL)
cout<<" "<Value;
cout<
cout< while(OddStack.Pop(&n))
cout<<" "<
cout< while(EvenStack.Pop(&n))
cout<<" "<
cout< return 0;
}
这是随便写写的程序, 没经过严格测试, 你自己好好看看, 不要总让别人做作业, 这个习惯不好
--------------------next---------------------
阅读(1178) | 评论(0) | 转发(0) |