用类实现单链表操作

来源:互联网 发布:乐视网络电视报价 编辑:程序博客网 时间:2024/06/05 17:23

 /*—————————————————————————————————
    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;
}

原创粉丝点击