C++单链表操作:节点的插入、删除、链表的转置

定义一个链表类 ,文件名Link.h
#include <iostream>

using namespace std;

template<typename T>
class Link
{
public:
    //构造函数创建链表,即创建头节点
    Link();
    ~Link();
    
    //打印整个链表的值
    int printLink() const;

    //插入一个节点,参数为此节点要存放的数据
    //此接口不排序、不判断是否已经存在,单纯的插入一个数据
    int insertNode(const T& data);
    
    //删除节点,删除所有值为data的节点
    int deleteNode(const T& data);

    //转置整个链表
    int transpose();
                                                                                                                                                                                                                                                        
private:
    struct Node
    {
        T data;
        Node* next;
    };

    //创建一个指向第一个节点的指针,且它不存放数据
    Node* head;
};//class end

template<typename T>
Link<T>::Link()
{   
	head = nullptr;
}

template<typename T>
Link<T>::~Link()
{   
}

//打印整个链表的值
template<typename T>
int Link<T>::printLink() const
{
    cout << "{" ;
    for (auto ptr = head; ptr != nullptr; ptr=ptr->next)
    {
        cout << ptr->data << "->";
    }
    cout << "}" << endl;
    return 0;
}

//插入一个节点,参数为此节点要存放的数据
//此接口不排序、不判断是否已经存在,单纯的插入一个数据
template<typename T>
int Link<T>::insertNode(const T& data)
{
    //创建新节点
    Node *newNode = new Node();
    newNode->data = data;
    newNode->next = head;
    head = newNode;   
    return 0;
}

//删除节点,删除所有值为data的节点
template<typename T>                                                                                                                                                                                                                                    
int Link<T>::deleteNode(const T& data)
{
    //p_one 用来定位待删节点的前一个节点
    Node* p_one = head;
    //p_second 用来定位待删除的节点
    Node* p_second = head;

    //链表本来就为空
    if (nullptr == head)
    {
        cout << "The link is empty!" << endl;
        return 0;
    }

    //当head所指的节点为待删除节点时
    //之所以遍历,是因为存在“链表开始的几个节点数据都为data”的可能
    while (data == head->data)
    {
        head = head->next;

        //链表被删空了
        if (nullptr == head)
        {
            cout << "No data in the link now!" << endl;
            return 0;
        }

        p_one = head;
        p_second = head;
    }

    //程序至此,已经确定第一个节点不是待删除节点,且不为空
    //直接开始判断第二个节点
    for( p_second = p_one->next; p_second != nullptr; p_second = p_second->next)
    {   
        if (data == p_second->data)
        {//找到待删除节点,将其前一个节点的next指针指向待删除节点的后一个节点
            p_one->next = p_second->next;   
            continue;
        }//找到待删除节点时,p_one不能移动,暂停原地

        p_one = p_one->next;
    }   
}

//转置整个链表
template<typename T>
int Link<T>::transpose()
{
    //链表为空或只有一个节点是不需要转置的
    if (nullptr == head or nullptr == head->next)
    {
        return 0;
    }
    //从头节点开始转置,需要3个指针
    Node* p1 = nullptr;//p1为运行最慢的指针,在最后面,是转置的目标指向
    Node* p2 = head;//p2为真正转置的指针,每次只改变p2的next指向
    Node* p3 = head->next;//p3为最前面的指针,保存p2原先的next指向,保障遍历的进行
    
    while(nullptr != p3)
    {
        p2->next = p1;//p2所指节点的next指针指向p1,over

        p1 = p2;
        p2 = p3;
        p3 = p3->next;
    }

    //至此,p3一定是nullptr,只需要将最后一个节点Next指向前面即可
    p2->next = p1;
    
    head = p2;//注意结束时,要记得调整head的指向

    return 0;

程序的main文件作为测试文件:

#include <iostream>
#include "Link.h"

using namespace std;

int test_insertNode()
{
    cout << "--------test insert------" << endl;
    Link<int> link;
    for (int i = 0; i < 10; ++i)
    {   
        link.insertNode(i);
    }   
    link.printLink();
    return 0;
}

int test_deleteNode()
{
    cout << "\n--------test delete------" << endl;
    Link<int> link;
    link.insertNode(5);
    link.insertNode(5);
    link.insertNode(6);
    link.insertNode(5);
    link.insertNode(5);
    link.insertNode(5);
    link.insertNode(7);
    link.insertNode(5);
    link.insertNode(5);
    cout << "Befor deletet, the link is:";
    link.printLink();
    link.deleteNode(5);
    cout << "After deletet, the link is:";
    link.printLink();
    cout << "\n" << endl;
    return 0;
}

int test_transpose()
{
    cout << "\n--------test transpose------" << endl;
    Link<int> link;
    for (int i = 0; i < 10; ++i)
    {   
        link.insertNode(i);
    }   
    cout << "Befor transpose, the link is:";
    link.printLink();
    link.transpose();
    cout << "After transpose, the link is:";
    link.printLink();
    return 0;   
}

int main()
{
    test_insertNode();
    test_deleteNode();
    test_transpose();
    return 0;
}

程序运行结果:

--------test insert------
{9->8->7->6->5->4->3->2->1->0->}

--------test delete------
Befor deletet, the link is:{5->5->7->5->5->5->6->5->5->}
After deletet, the link is:{7->6->}

--------test transpose------
Befor transpose, the link is:{9->8->7->6->5->4->3->2->1->0->}
After transpose, the link is:{0->1->2->3->4->5->6->7->8->9->}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值