用类实现单链表操作

 /* —————————————————————————————————
    Node.h
    Declearation of Class Node.
—————————————————————————————————
*/
#ifndef _NODE
#define  _NODE

class  Nodes
{
public :
    Nodes(
void );
    
void  NodesCreat( void );       // 创建链表
     void  NodesPrint( void );       // 遍历输出链表
     void  NodesDelete( void );      // 删除链表节点
     void  NodesInsert( void );      // 插入节点
     void  NodesReverse( void );     // 链表逆置
private :
    
struct  List
    {
        
int  index;
        List 
* next;
    } 
* head, * end;     // 首尾指针
     int  iListLen;      // 链表长度
};

#endif

/* —————————————————————————————————
    Node.cpp
    Source File of Class Node.
—————————————————————————————————
*/
#include 
" iostream.h "
#include 
" Node.h "

Nodes::Nodes(
void )       // 构造函数实现节点空,长度零
{
    head
= end = NULL;
    iListLen
= 0 ;
}

void  Nodes::NodesCreat( void )    
{
    
int  iListNum,iCount = 0 ;
    List 
* ptemp = NULL;
    cout
<< " Input The ListNum You want: " ;
    cin
>> iListNum;       // 创建的节点数
    cout << " Input Every List: " ;
    
while  (iCount < iListNum)
    {
        ptemp
= new  List;
        cin
>> ptemp -> index;
        
if  (head == NULL)     
        {   
            head
= end = ptemp;
            end
-> next = NULL;
        }
        
else      // 链表不为空
        {
            end
-> next = ptemp;
            end
= ptemp;
            end
-> next = NULL;
        }
        iCount
++ ;iListLen ++ ;
    }
    cout
<< " Creat Or Add sucess... " ;
}

void  Nodes::NodesPrint( void )    
{
    List 
* pListIndex; int  iLen = 0 ;
    
if  (head == NULL)
    {
        cout
<< "  No List Now " ;
        
return ;
    }
    
else      // 链表不为空
    {
        cout
<< " Total List Num: " << iListLen << endl;
        iLen
= 1 ;
        pListIndex
= head;
        cout
<< iLen << " # " << pListIndex -> index << endl;
        
while  (pListIndex -> next)
        {
            iLen
++ ;
            pListIndex
= pListIndex -> next;
            cout
<< iLen << " # " << pListIndex -> index << endl;
        }
    }
}

void  Nodes::NodesDelete( void )
{
    
int  iListPos,n = 1 ;List  * pListIndex, * pListTemp;
    
if  (head == NULL)     
    {
        cout
<< "  No list Now " ;
        
return ;
    }
    
else      // 链表不为空
    {
        cout
<< " Input the Delete Position: " ;
        cin
>> iListPos;
        
if  (iListPos > iListLen)    // 删除的节点超出总长度
        {
            cout
<< " Error! " ;
            cout
<< " The Total ListNum is: " << iListLen << endl;
            
return ;
        }
        
if  (iListPos == 1 || iListPos == iListLen)     // 删除的节点在头和尾
        {
            
if  (iListPos == 1 )     // 删除头节点
            {
                pListIndex
= head;
                head
= head -> next;
                delete pListIndex;
            }
            
else      // 删除尾节点
            {
                
for  (pListIndex = head;pListIndex -> next != end;)
                    pListIndex
= pListIndex -> next;
                delete end;
                end
= pListIndex;
                end
-> next = NULL;
            }
        }
        
else   // 删除节点在非特殊位置
        {
            
for  (pListIndex = head;n < iListPos - 1 ;n ++ )
                pListIndex
= pListIndex -> next;
            pListTemp
= pListIndex -> next;
            pListIndex
-> next = pListTemp -> next;
            delete pListTemp;
        }
        iListLen
-- ;
    }
    cout
<< " The List has been deleted... " ;
}

void  Nodes::NodesInsert( void )
{
    
int  iListPos,n = 1 ;
    List 
* pListIndex = NULL, * pListInsert = NULL;
    pListInsert
= new  List;
    cout
<< " Insert List Information: " ;
    cin
>> pListInsert -> index;
    cout
<< " Input The The Insert Positon: " ;
    cin
>> iListPos;
    pListInsert
-> next = NULL;
    
if  (head == NULL)      // 如果链表为空则创建插入点
    {
        cout
<< " No List Now ,Insert you List as head. " ;
        head
= end = pListInsert;
        end
-> next = NULL;
        NodesPrint();
        
return ;
    }
    
if  (iListPos == 1   ||  iListPos == iListLen)       // 插入位置在头和尾
    {
        
if  (iListPos == 1
        {   
            pListInsert
-> next = head;head = pListInsert;
        }
        
if (iListPos == iListLen)
        {
            end
-> next = pListInsert;end = pListInsert;
        }
    }
    
else      // 非特殊位置的插入操作
    {       
        
if  (iListPos > iListLen)
        {
            cout
<< " Error ! Beyond the Num! " ;
            
return ;
        }
        
for  (pListIndex = head;n < iListPos - 1 ;n ++ )
                pListIndex
= pListIndex -> next;
        pListInsert
-> next = pListIndex -> next;
        pListIndex
-> next = pListInsert;
    }
    iListLen
++ ;
    cout
<< " The List has been Inserted... " ;
}

void  Nodes::NodesReverse( void )
{
    List 
* pListIndex, * pListTemp;
    
if  (head != NULL)
    {
        pListIndex
= head;
        
while  (pListIndex -> next != NULL)
        {
            pListTemp
= pListIndex -> next;
            pListIndex
-> next = pListTemp -> next;
            pListTemp
-> next = head;
            head
= pListTemp;
            pListTemp
= NULL;
        }
        end
= pListIndex;
    }
    
else  
    {
        cout
<< " No List Now .You can't Reverse...  " ;
        
return ;
    }
}
/* ——————————————————————————————————
    main.cpp
    The main function
——————————————————————————————————
*/
#include 
" iostream.h "
#include 
" Node.h "
void  Screen(Nodes  & obj)
{
    
char  op;
    
while  ( 1 )
    {
    cout
<< " " ;
    cout
<< "   ———————————————— " ;
    cout
<< "   |     Node List Operation      | " ;
    cout
<< "   ———————————————— " ;
    cout
<< "   |  1.Creat Or Add The List     | " ;
    cout
<< "   |  2.Delete The List           | " ;
    cout
<< "   |  3.Insert The List           | " ;
    cout
<< "   |  4.Reverse The List          | " ;
    cout
<< "   |  5.Print All The List        | " ;
    cout
<< "   |  0.Exit                      | " ;
    cout
<< "   ———————————————— " ;
    cout
<< " Input The Option Please: " ;
    cin
>> op;
    
switch (op)
    {
        
case   ' 0 ' : return ;
        
case   ' 1 ' :obj.NodesCreat(); break ;
        
case   ' 2 ' :obj.NodesDelete(); break ;
        
case   ' 3 ' :obj.NodesInsert(); break ;
        
case   ' 4 ' :obj.NodesReverse(); break ;
        
case   ' 5 ' :obj.NodesPrint(); break ;
        
default :cout << " Error Option.Input Again... " ;
    }
    }
}
int  main( int  argc, char   * argv[])
{
    Nodes 
object ;
    Screen(
object );
    
return   0 ;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值