![]() |
#2
forclwy2010-01-07 20:26
|
递归的,非递归的。

#include<iostream>
using namespace std;
struct Exception{
Exception(){cout<<"Exception"<<endl;}
};
template<class Type>
struct node{
Type val;
node<Type> *next;
node<Type>(){next=NULL;}
node(Type v):val(v){next=NULL; }
friend ostream & operator<<(ostream &out,node<Type> & n)
{
out<<n.val;
return out;
}
};
template <class Type>
class SList{
private:
node<Type> *hummy;
protected:
node<Type> * getPre(node<Type> *pointer)
{
try{
node<Type> * n;
for(n=hummy;n!=NULL;n=n->next)
{
if( n->next==pointer)
break;
}
if( pointer== hummy)
throw Exception();
return n;
}catch( Exception () )
{
cout<<"Fist node with out pre node"<<endl;
return NULL;
}
}
node<Type> * getNext(node<Type> *pointer)
{
try{
if(pointer->next==NULL)
throw Exception ();
return pointer->next;
}catch( Exception e)
{
cout<<"End of List"<<endl;
return NULL;
}
}
bool insert(node<Type> *pointer, Type v)
{
try
{
node<Type> *n=new node<Type>(v);
if( NULL == n )
throw Exception();
else
{
n->next=pointer->next;
pointer->next=n;
return true;
}
}catch (Exception e )
{
cout<<"New Error"<<endl;
return false;
}
}
bool del(node<Type> *pointer)
{
try
{
if( pointer != hummy->next)
{
node<Type> * pre=getPre(pointer);
pre->next=pointer->next;
delete pointer;
pointer=NULL;
}
else
{
hummy->next=pointer->next;
delete pointer;
pointer=NULL;
}
}catch( Exception())
{
}
}
public:
SList(){ hummy=new node<Type>();}
SList(int n)
{
hummy=new node<Type>();
for( int i=0;i<n;i++)
{
Type m;
cin>>m;
insert(hummy,m);
}
}
friend void reverse( SList & s)
{
}
void travel()
{
node<Type> * n;
for( n=hummy->next; n!=NULL;n=n->next)
{
cout<<*n<<" ";
}
cout<<endl;
}
};
int main()
{
int m;
cin>>m;
SList<int> sl(m);
sl.travel();
reverse(sl);
sl.travel();
return 0;
}
using namespace std;
struct Exception{
Exception(){cout<<"Exception"<<endl;}
};
template<class Type>
struct node{
Type val;
node<Type> *next;
node<Type>(){next=NULL;}
node(Type v):val(v){next=NULL; }
friend ostream & operator<<(ostream &out,node<Type> & n)
{
out<<n.val;
return out;
}
};
template <class Type>
class SList{
private:
node<Type> *hummy;
protected:
node<Type> * getPre(node<Type> *pointer)
{
try{
node<Type> * n;
for(n=hummy;n!=NULL;n=n->next)
{
if( n->next==pointer)
break;
}
if( pointer== hummy)
throw Exception();
return n;
}catch( Exception () )
{
cout<<"Fist node with out pre node"<<endl;
return NULL;
}
}
node<Type> * getNext(node<Type> *pointer)
{
try{
if(pointer->next==NULL)
throw Exception ();
return pointer->next;
}catch( Exception e)
{
cout<<"End of List"<<endl;
return NULL;
}
}
bool insert(node<Type> *pointer, Type v)
{
try
{
node<Type> *n=new node<Type>(v);
if( NULL == n )
throw Exception();
else
{
n->next=pointer->next;
pointer->next=n;
return true;
}
}catch (Exception e )
{
cout<<"New Error"<<endl;
return false;
}
}
bool del(node<Type> *pointer)
{
try
{
if( pointer != hummy->next)
{
node<Type> * pre=getPre(pointer);
pre->next=pointer->next;
delete pointer;
pointer=NULL;
}
else
{
hummy->next=pointer->next;
delete pointer;
pointer=NULL;
}
}catch( Exception())
{
}
}
public:
SList(){ hummy=new node<Type>();}
SList(int n)
{
hummy=new node<Type>();
for( int i=0;i<n;i++)
{
Type m;
cin>>m;
insert(hummy,m);
}
}
friend void reverse( SList & s)
{
}
void travel()
{
node<Type> * n;
for( n=hummy->next; n!=NULL;n=n->next)
{
cout<<*n<<" ";
}
cout<<endl;
}
};
int main()
{
int m;
cin>>m;
SList<int> sl(m);
sl.travel();
reverse(sl);
sl.travel();
return 0;
}