经常让写的关于链表的代码

#include <iostream>

using namespace std;

struct Node
{
public:
    int data;
    Node *next;
    Node(){};
    Node(int i)
    {
        data = i;
        next = NULL;
    }
};
class List
{
public:
    Node *head;
    Node *curr;
    List()
    {
        head = NULL;
        curr = NULL;
    }
    void insert_from_tail(Node *tmp)
    {
        if(head == NULL)
        {
            head = new Node();
            head->next = tmp;
            curr = tmp;
        }
        else
        {
            curr->next = tmp;
            curr = tmp;
        }
    }
    int length()const
    {
        int count = 0;
        Node *tmp = head->next;
        while(tmp != NULL)
        {
            tmp = tmp->next;
            count++;
        }
        return count;
    }
};
//完成两个节点的值的交换
void Swap(Node *t1,Node *t2)
{
    int tmp = t1->data;
    t1->data = t2->data;
    t2->data = tmp;
}
//冒泡排序
void bubble_sort(List &l)
{        
    int len = l.length();
    for(int i = len-1 ; i > 0 ; --i)
    {
        Node *tmp = l.head->next;
        for(int j = 0 ; j < i ; ++j)
        {
            if(tmp->data > tmp->next->data)
            {
                Swap(tmp,tmp->next);
            }
            tmp = tmp->next;
        }
    }
}
//完成单链表的逆序并输出
void reverse_print(List &l)
{
    if(l.head == NULL || l.head->next == NULL)//链表是空
    {
        return ;
    }
    Node *p1 = l.head->next;
    Node *p2 = l.head->next->next;
    p1->next = NULL;//这里一定要让p1->next = NULL,因为逆序后p1实际上是链表的最后一个节点
    while(p2 != NULL)
    {
        Node *tmp = p2->next;
        p2->next = p1;    
        p1 = p2;
        p2 = tmp;
    }
    l.head->next = p1;//注意这里第一个节点是p1,而不是p2
    Node *p = l.head->next;
    while(p != NULL)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;
}
//合并两个单链表
void merge(List l1 ,List l2 ,List &l3)
{
    Node *p1 = l1.head->next;
    Node *p2 = l2.head->next;
    while(p1 != NULL || p2 != NULL)
    {
        if(p2 == NULL || (p1 != NULL && p1->data <= p2->data))
        {
            l3.insert_from_tail(p1);
            p1= p1->next;
        }
        else
        {
            l3.insert_from_tail(p2);
            p2 = p2->next;
        }
    }
}
int main()
{
    /*---------------------------------------*/
    //初始化单链表
    int i;
    Node *p;
    List l;
    Node t[5] = {2,1,4,3,0};
    for(i = 0 ; i < 5 ; ++i)
    {
        l.insert_from_tail(&t[i]);    
    }
    //单链表的长度
    cout<<"初始化单链表的长度是:";
    cout<<l.length()<<endl;
    cout<<"初始化链表中的数据是:";
    p = l.head->next;
    while(p != NULL)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;

    /*----------------------------------------*/
    //排序后的结果
    bubble_sort(l);
    cout<<"排序后链表中的数据是:";
    p = l.head->next;
    while(p != NULL)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;
    /*----------------------------------------*/
    //逆序单链表
    cout<<"逆序后链表中的数据是:";
    reverse_print(l);
    /*----------------------------------------*/
    //合并两个拍好序的单链表
    List l1,l2,l3;
    Node t1[4] = {1,3,4,5};
    Node t2[4] = {2,6,7,8};
    for(i = 0 ; i < 4 ; ++i)
    {
        l1.insert_from_tail(&t1[i]);
        l2.insert_from_tail(&t2[i]);
    }
    cout<<"{1,3,4,5}和{2,6,7,8}合并后的链表中的数据:";
    merge(l1,l2,l3);
    p = l3.head->next;
    while(p != NULL)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;
    return 0;
}

你可能感兴趣的:(经常让写的关于链表的代码)