| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 795 人关注过本帖
标题:出个题目,O(n)时间里逆转单链表 补全函数。
只看楼主 加入收藏
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
结帖率:100%
收藏
 问题点数:0 回复次数:4 
出个题目,O(n)时间里逆转单链表 补全函数。
补写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;
}
搜索更多相关主题的帖子: 时间 函数 单链 
2010-01-07 19:29
forclwy
Rank: 4
等 级:业余侠客
帖 子:167
专家分:255
注 册:2008-10-21
收藏
得分:0 
不会,帮顶!楼主程序很漂亮
2010-01-07 20:26
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
以下是引用forclwy在2010-1-7 20:26:57的发言:

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



谢谢,很多人都这么说。

我不是菜鸟。。
2010-01-07 22:41
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:0 
真的是好看。。
2010-01-08 14:02
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
update
2010-01-11 19:01
快速回复:出个题目,O(n)时间里逆转单链表 补全函数。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019088 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved