注册 登录
编程论坛 C++教室

出个题目,O(n)时间里逆转单链表 补全函数。

Devil_W 发布于 2010-01-07 19:29, 795 次点击
补写reverse函数,实现链表逆转。

递归的,非递归的。



程序代码:
#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;
}
4 回复
#2
forclwy2010-01-07 20:26
不会,帮顶!楼主程序很漂亮
#3
Devil_W2010-01-07 22:41
以下是引用forclwy在2010-1-7 20:26:57的发言:

不会,帮顶!楼主程序很漂亮



谢谢,很多人都这么说。

我不是菜鸟。。
#4
lijm19892010-01-08 14:02
真的是好看。。
#5
Devil_W2010-01-11 19:01
update
1