C++:链表

文章展示了两个C++实现的链表节点类,一个数据和指针是私有的,通过成员函数进行操作,另一个则是数据和指针公开。同时,文章提供了插入和删除节点的示例,以及一个完整的链表类,包括节点插入、删除、数据访问和链表管理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据和指针隐藏的节点类
#include<bits/stdc++.h>
using namespace std;

template <class T>
class cNode  //单链表节点类 
{  
    T m_data;  //数据域  
    cNode<T> *m_next; //指向下一个结点
public:
     cNode(const T& item, cNode<T> *ptr=NULL); //ptr: 后继节点 
     ~cNode() { }
     cNode<T> *next();          //返回后继结点
     void insertAfter(cNode<T> *p);//本结点后插入节点p
     cNode<T> *delAfter();         //删除本结点之后的结点并返回
     T getData() { return m_data; }
};

//template<class T>
//cNode<T>::cNode(const T& item, cNode<T> *ptr=NULL)
//{   m_data = item;
// m_next = ptr;
//}
template<class T>
cNode<T>::cNode(const T& item, cNode<T> *ptr)
{   
    m_data = item;
     m_next = ptr;
}

template<class T>
cNode<T> *cNode<T>::next()
{   
    return m_next;
}

template<class T>
void  cNode<T>::insertAfter(cNode<T> *p)
{   
    p->m_next = m_next;    
     m_next = p;
}

template<class T>
cNode<T> *cNode<T>::delAfter()
{  
    if(!m_next) return NULL; //无后继节点
     cNode<T> *ptr = m_next;
     m_next = m_next->m_next;
     return ptr;
}


/*测试方法:增加了函数getData()(也可以不增加,但m_data要改为公有成员);
            构造3个节点的链,输出每个节点的数据;
   删除中间节点,然后输出每个节点的数据。
*/ 
int main()
{ 
    cNode<int> nd1(100), nd2(200), nd3(300), *p;
     nd1.insertAfter(&nd2);
     nd1.insertAfter(&nd3);
     p = &nd1;
     while(p)
     { 
         cout<<p->getData()<<" ";
          p = p->next();
     }
     cout<<endl;
 
     nd1.delAfter();
     p = &nd1;
     while(p)
     { 
         cout<<p->getData()<<" ";
          p = p->next();
     }
     cout<<endl;
     return 0;
}
数据和指针公开的节点类
#include<bits/stdc++.h>
using namespace std;

template <class T>
class cNode  //单链表节点类.简单起见,成员均为public 
{
public:  
     T   data;  //数据域  
     cNode<T> *next;  //指向下一个结点
 
     cNode(const T& item)
    { 
        data = item; next = NULL; 
    }
     ~cNode() { }
};

int main()
{ 
    cNode<int> nd1(100), nd2(200), nd3(300), *p;
     nd1.next = &nd3; nd3.next = &nd2;
     p = &nd1;
     while(p)
     { 
         cout<<p->data<<" ";
          p = p->next;
     }
     cout<<endl;
 
     nd1.next = nd1.next->next;
     p = &nd1;
     while(p)
     { 
         cout<<p->data<<" ";
          p = p->next;
    }
     cout<<endl;
     return 0;
}

可以添加附加头结点,储存无效数据,使得头节点永不为NULL,便于一些判断

完整的链表类
#include <iostream>
using namespace std;

//节点类
template<class T>
class Node
{
public:
    T data;//数据域 
    Node<T>* next;//指针域
    //构造函数1
    Node()
    {
        next = NULL;
    } 
    //构造函数2
    Node(const T& item)
    {
        data = item;
        next = NULL;    
    } 
    //#### ~Node();//析构函数 
    //获取下一节点指针
    Node<T> *NextNode() const;
    //删除结点
    Node *DeleteAfter()
    {
        //保存当前节点的后继结点 
        Node<T> *ptr = next;
        //若没有后继结点返回空指针
        if (ptr == NULL)
            return NULL;
        //当前结点指向其原来后继的后继,即ptr的后继
        next = ptr->next;
        //返回被删除结点的指针
        return ptr; 
    }
    
    //得到该结点的值
    Node<T> *getdata()const
    {
        return data;
    }
};

//指针类 
template<class T>
class LinkedList
{
private:
    Node<T> *front, *rear, *head, *ptr;//指向表头、表尾的指针 
    Node<T> *cur,  *pre;//用于指向当前和前一个结点的指针 
    
    int size;//当前节点个数 
    int position;//当前位置 
    
    //申请及释放单链表结点空间的函数
    Node<T> *GetNode(const T& item, Node<T> *ptr = NULL);
    void FreeNode(Node<T> * p);
    
public:
    LinkedList();//构造函数 
    ~LinkedList();//析构函数 
    //重载的赋值运算符
    LinkedList<T>& operator = (const LinkedList<T> & orgList);
    //获取单链表的结点个数
    int Size() const;
    //判断单链表是否为空
    bool empty()const;
    //重新定位当前单链表结点
    int NextNode();
    int SetPosition(int pos);
    int GetPosition() const;
    
    //插入结点
    void InsertAfter(const T& item);//当前位置之后插入节点 
    void InsertAt(const T& item); //就在当前位置插入节点 
    
    //删除链表结点的函数
    void DeleteAt();
    void DeleteAfter();
    
    //修改和访问数据的函数 
    T GetData()const;
    void SetData (const T& item);
    
    //清空链表的函数
    void Clear(); 
};

//申请及释放单链表结点空间的函数
template<class T>
Node<T> *LinkedList<T>::*GetNode(const T& item, Node<T> *ptr = NULL)
{
    Node<T> *newNode = new Node<T>(item, ptr);
    //若动态内存申请失败则给出相应的提示并返回空指针
    if(!newNode)
    {
        cout << "申请失败" <<endl;
        return NULL;
    } 
    //返回新生成的结点指针
    return newNode; 
} 
template<class T>
void LinkedList<T>::FreeNode(Node<T> * p)
{
    //若ptr为空,给出相应提示并返回
    //####if(!ptr)
    if(!p)
    {
        cout << "FreeNode error!" << endl;
        return; 
    } 
    //释放结点占用的内存空间
    //####delete ptr;
    delete p;
    return; 
}

//构造函数 (建立一个空链表)
template<class T>
LinkedList<T>::LinkedList()
{
    //####head = pre = new Node<T>(0,NULL);//附加头结点
    head = pre = new Node<T>(0);
    front = rear = NULL;
    cur = NULL;
    
    size = 0;
    position = -1;
}
//析构函数
template<class T>
LinkedList<T>::~LinkedList()
{
    //清空单链表,释放所有的结点空间
    Clear(); 
}

//单链表类中重载赋值运算符的函数
template<class T>
LinkedList<T>& LinkedList<T>::operator = (const LinkedList<T>& orgList)
{
    Node<T> *p = orgList.front;
    //清空本链表
    Clear();
    //将单链表 orgList中的元素复制到本单链表
    while(p)
    {
        InsertAfter(p->data);
        p = p->NextNode();
    } 
    //设置当前结点
    SetPosition(orgList.position);
    return *this;
} 

//获取表的大小
template<class T>
int LinkedList<T>::Size() const
{
    return size;
} 

//判断表是否为空
template<class T>
bool LinkedList<T>::empty() const
{
    if (size == 0)
        return true;
    else
        return false;
}

//将后继结点设置为当前结点的函数
template<class T>
int LinkedList<T>::NextNode()
{
    //若当前结点存在,则将其后继结点设置为当前节点
    if (position >= 0 && position < size)
    {
        position++;
        pre = cur;
        //####cur = cur->NextNode();
        cur = cur->next;
    } 
    else//否则将当前位置设为表尾 
    {
        position++;

    }
    return position;//返回新位置 
}

//重置当前结点的位置
template<class T>
int LinkedList<T>::SetPosition(int pos)
{
    if (!size)//若链表为空 
        return -1;
    if (pos < 0 ||pos > size - 1)
    {
        cout << "越界" << endl;
        return -1; 
    } 
    pre = NULL;
    cur = front;
    position = 0;
    for (int k = 0; k < pos; k++)
    {
        position++;
        pre = cur;
        cur = cur->next;
    }
    return position;//返回当前结点的位置 
}

//取出当前结点位置
template<class T>
int LinkedList<T>::GetPosition() const
{
    return position;
}

//在当前结点后插入结点
template<class T>
void LinkedList<T>::InsertAfter(const T& item)
{
    //####Node<T> *p = new Node<T>(item, NULL);
    Node<T> *p = new Node<T>(item);
    if (!cur)//空链表 
    {
        head->next = p;
        rear = p;
        //####此处加入设置front指针
        front = p;
    }
    else
    {
        p->next = cur->next;//不考虑位置:头、中间、尾 
        cur->next = p;
        if (!p->next)//当前位置是链尾 
            rear = p;
    }
    size++;
    cur = p;//cur指向新结点 
} 

//在当前结点处插入新的结点
template<class T>
void LinkedList<T>::InsertAt(const T& item)
{
    Node<T> *p = new Node<T>(item, NULL);
    p->next = cur;
    pre->next = p;
    cur = p;
    size++;
    if (!rear)//尾结点为空,说明是空链 
    {
        rear = p;
        //####此处加入设置front指针
        front = p;
    }
} 

//删除当前结点
template<class T>
void LinkedList<T>::DeleteAt()
{
    Node<T> *oldNode;
    if(!cur)//若链表为空或已经到达表尾 
    {
        cout << "DeleleAt:current position is invalid!" << endl;
        return;
    }
    
    if (!pre)//说明要删除的是表头结点 
    {
        oldNode = front;
        front = cur->next;
    }
    else//说明要删除的是表中结点 
    {
        oldNode = pre->DeleteAfter();
    } 
    if (oldNode == rear)//删除表尾结点,则修改表尾指针 
    {
        rear = pre; 
    }
    cur = oldNode->next;//后继结点作为新的结点
    FreeNode(oldNode);//释放当前结点
    size--;//链表大小减一 
}

//删除当前结点后继
template<class T>
void LinkedList<T>::DeleteAfter()
{
    Node<T> *oldNode;
    if (!cur || cur == rear)//若无当前结点或者已经到达链表尾 
    {
        cout << "DeleteAfter:current position in invalid!" << endl; 
        return;
    }
    oldNode = cur->DeleteAfter();//保存被删除的结点指针并删除该节点 
    if (oldNode == rear)//删除的是表尾结点 
    {
        rear = cur;    
    } 
    FreeNode(oldNode);
    
} 

//获取当前结点数据
template<class T>
//####T LinkedLlist<T>::GetData() const
T LinkedList<T>::GetData() const
{
    if (!cur)//若链表为空或已经到达表尾
    {
        cout << "Data:current node not exist!" << endl;
        return NULL;
    } 
    return cur->data;
} 

//修改当前结点数据
template<class T>
//####void LinkedList<T>::Setdata(const T& item)
void LinkedList<T>::SetData(const T& item)
{
    if (!cur)//若链表位空或已经到达表尾
    {
        cout << "Data:current node does not exist!" << endl;
        return;
    } 
    cur->data = item;//修改当前结点的数据 
} 

//清空链表
template<class T>
void LinkedList<T>::Clear()
{
    Node<T> *cur = front, *nextNode;
    while (cur)
    {
        nextNode = cur->next;//保存后继结点指针 
        FreeNode(cur);//释放当前结点
        cur = nextNode;//原后继结点成为当前结点 
    }
    //修改链表数据
    front = rear = pre = cur = NULL;
    size = 0; 
    position = -1; 
} 
 


int main()
{
    LinkedList<int> L;

    for (int i = 0; i<=10; i++)
    {
        L.InsertAfter(i);
    }

    L.SetPosition(0);
    cout << "LinkedList:  " << endl;
    while(L.NextNode() !=  L.Size())
    {
        cout <<L.GetData() << " ";
    } 
    cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值