有头结点的单链表的各种操作

//有头结点的单链表

template <class T>
struct Node
{
    T data;
    Node<T> *next;
};
//-----------------------------------------------
//初始化单链表
template <class T>
int init_list(Node<T> **L)
{
    (*L) = (Node<T>*)malloc(sizeof(Node<T>));
    (*L)->next = NULL;
    return 1;
}
//-----------------------------------------------
//从尾部追加一个结点
template <class T>
int append_list(Node<T> *L , T data)
{
    Node<T>* pNewNode = (Node<T>*)malloc(sizeof(Node<T>));

    pNewNode->data = data;
    pNewNode->next = NULL;

    Node<T>* pNode = L;
    while(pNode->next != NULL)
    {
        pNode = pNode->next;
    }
    pNode->next = pNewNode;
    return 1;
}
//删除尾部结点
template <class T>
int delete_tail_list(Node<T> *L)
{
    if(L == NULL)
        return 0;
    Node<T> *pNode = L->next;
    Node<T> *pPre = L;

    while(pNode != NULL)
    {
        if(pNode->next == NULL)
        {
            pPre->next = NULL;
            free(pNode);
            cout << "delete tail node success!" << endl;
            break;
        }
        pNode = pNode->next;
        pPre = pPre->next;
    }
    return 1;
}
//----------------------------------------------------------
//从头部添加一个结点
template <class T>
int add_head_list(Node<T> *L , T data)
{
    if(L == NULL)
        return 0;

    Node<T> *pNewNode = (Node<T>*)malloc(sizeof(Node<T>));
    
    pNewNode->data = data;
    pNewNode->next = L->next;
    
    L->next = pNewNode;
    
    return 1;
}
//从头部删除一个结点
template <class T>
int delete_head_list(Node<T> *L)
{
    if(L == NULL)
        return 0;
    if(L->next != NULL)
    {
        Node<T> *pNode = L->next;

        L->next = L->next->next;
        free(pNode);
        cout << "delete head list success!" << endl;
    }
    return 1;
}
//----------------------------------------------------------
//从指定位置pos(1~~len)添加
template <class T>
int insert_list(Node<T> *L , int pos , T data)
{
    if(L == NULL)
        return 0;
    Node<T> *pPre = L;
    Node<T> *pNode = L->next;

    int index = 0;
    while(pNode != NULL)
    {
        index++;

        if(pos == index)
        {
            Node<T> *pNewNode = (Node<T>*)malloc(sizeof(Node<T>));
            pNewNode->data = data;
            pNewNode->next = pNode;

            pPre->next = pNewNode;

            return 1;
        }
        pNode = pNode->next;
        pPre = pPre->next;
    }
    cout << "pos(" << pos << ") error" << endl;
    return 0;
}
//从指定位置pos(1~~len)删除一个结点
template <class T>
int delete_pos_list(Node<T> *L , int pos)
{
    if(L == NULL)
        return 0;

    Node<T> *pPre = L;
    Node<T> *pNode = L->next;

    int index = 0;
    while(pNode != NULL)
    {
        index++;
        if(pos == index)
        {
            pPre->next = pNode->next;
        
            pNode->next = NULL;
            free(pNode);

            return 1;
        }

        pNode = pNode->next;
        pPre = pPre->next;
    }
    cout << "pos(" << pos << ") error" << endl;
    return 0;
}
//-----------------------------------------------
//判断是否为空
template <class T>
bool isempyt(Node<T> *L)
{
    if(L->next == NULL)
        return true;
    return false;
}
//-----------------------------------------------
//清除链表
template <class T>
int clear_list(Node<T> *L)
{
    Node<T> *pNode = NULL;

    if(L == NULL)
    {
        return 0;
    }
    while(L->next != NULL)
    {
        pNode = L->next;
        L->next = L->next->next;
        free(pNode);
    }
    return 1;
}
//-----------------------------------------------
//删除链表
template <class T>
int delete_list(Node<T> **L)
{
    clear_list(*L);
    free(*L);
    *L = NULL;
    return 1;
}
//-----------------------------------------------
//返回长度
template <class T>
int len_list(Node<T> *L)
{
    Node<T> *pNode = L->next;

    int len = 0;
    while(pNode != NULL)
    {
        len++;
        pNode = pNode->next;
    }
    return len;
}
//-----------------------------------------------
//正序打印整个列表
template <class T>
void print_list(Node<T> *L)
{
    Node<T> * pNode = L->next;

    while(pNode != NULL)
    {
        cout << pNode->data << "  ";
        pNode = pNode->next;
    }
    cout << endl;
}
//-----------------------------------------------
//倒序打印整个列表(递归调用)
template <class T>
void reverse(Node<T> *pNode)
{
    if(pNode == NULL)
        return;
    if(pNode->next != NULL)
    {
        reverse(pNode->next);
    }
    
    cout << pNode->data << "  ";
    
    return;
}
template <class T>
void print_reverse_list(Node<T> *L)
{
    if(L == NULL)
        return;
    reverse(L->next);
    cout << endl;
    return;
}
//-----------------------------------------------
//倒置整个链表(非递归,用stack)
template <class T>
void reverse_list(Node<T> *L)
{
    if(L == NULL)
        return;
    
    stack<Node<T> *> st;

    Node<T> *pNode = L->next;
    Node<T> *pTemp = NULL;
    while(pNode != NULL)
    {
        st.push(pNode);

        pTemp = pNode;
        
        pNode = pNode->next;
        
        pTemp->next = NULL;
    }

    pNode = L;
    while(!st.empty())
    {
        pTemp = st.top();

        pNode->next = pTemp;
        pNode = pNode->next;
        st.pop();
    }
}

//倒置整个链表(非递归不用stack)
template <class T>
void reverse_list1(Node<T> *L)
{
    if(L == NULL)
        return;
    Node<T> * pNode = L->next;
    Node<T> * pNewNode = NULL;
    while(pNode != NULL)
    {
        Node<T> *pCur = pNode;
        pNode = pNode->next;
        pCur->next = pNewNode;
        pNewNode = pCur;
    }
    L->next = pNewNode;
}
//-----------------------------------------------
//返回倒数第K个结点
template <class T>
Node<T> *reci_node(Node<T> *L , int k)
{
    if(L == NULL || k <= 0)
        return NULL;
    Node<T> *pNode = L;
    Node<T> *kNode = L->next;
    
    if(kNode == NULL) return NULL;//无元素结点直接返回

    while(k > 0)
    {
        k--;
        pNode = pNode->next;
        if(pNode->next == NULL)    //已经是最后一个结点了
            break;
    }
    if(k > 0) return NULL;//超过长度

    while(pNode->next != NULL && kNode->next != NULL)
    {
        kNode = kNode->next;
        pNode = pNode->next;
    }

    return kNode;
}
//-----------------------------------------------
//返回链表中单结点
template <class T>
Node<T> * middle_node(Node<T> *L)
{
    if(L == NULL || L->next == NULL || L->next->next == NULL || L->next->next->next == NULL)//至少三个元素结点
        return NULL;

    Node<T> * pFast = L->next;
    Node<T> * pSlow = L->next;

    while(pFast->next != NULL)
    {
        pSlow = pSlow->next;    //慢指针一次走一步
        pFast = pFast->next;    //快指针一次走两步(第一步)
        if(pFast->next != NULL)
        {
            pFast = pFast->next;    //快指针一次走两步(第二步)走够两步的是单数个元素
        }
        else
        {
            break;        //快指针走了一步就到尾部了,没走够两步的是双数个元素
        }
    }
    return pSlow;
}
//-----------------------------------------------
//冒泡排序
void bubble_sort_list(Node<int> *L)
{
    if(L == NULL)
        return;

    Node<int> *pFlag = NULL;    //标志指针,有序区的首个结点指针,开始有序区没有结点所以NULL

    while(pFlag != L->next)        //
    {
        Node<int> *pCur = L->next;
        Node<int> *pNext = pCur->next;

        while(pNext != pFlag)
        {
            if(pCur->data > pNext->data)
            {
                int data = pCur->data;
                pCur->data = pNext->data;
                pNext->data = data;
            }
            pCur = pCur->next;
            pNext = pNext->next;
        }
        pFlag = pCur;    //一趟比较后有序区首结点指针
    }
}

//-----------------------------------------------
//把两个有序的链表合并成一个新链表,新链表也要求是有序的
Node<int> *merge_list(Node<int> *L1 , Node<int> *L2)
{
    if(L1 == NULL || L2 == NULL) return NULL;
    if(L1->next == NULL && L2->next == NULL) return NULL;
    
    if(L1->next != NULL && L2->next == NULL) return L1;
    if(L2->next != NULL && L1->next == NULL) return L2;

    Node<int> *pNewList = NULL;
    init_list(&pNewList);
    Node<int> *tail = pNewList;

    Node<int> *p1 = L1->next;
    L1->next = NULL;
    Node<int> *p2 = L2->next;
    L2->next = NULL;

    while(p1 != NULL && p2 != NULL)
    {
        if(p1->data < p2->data)
        {
            tail->next = p1;
            p1 = p1->next;
            tail = tail->next;
        }
        else
        {
            tail->next = p2;
            p2 = p2->next;
            tail = tail->next;
        }
    }

    if(p1 != NULL)
    {
        tail->next = p1;
    }
    if(p2 != NULL)
    {
        tail->next = p2;
    }
    
    return pNewList;
}
//-----------------------------------------------
//测试单链表
void simple_list_main()
{
    Node<int> *pList = NULL;

    init_list(&pList);

    append_list(pList , 2);
    append_list(pList , 12);
    append_list(pList , 23);
    append_list(pList , 52);
    append_list(pList , 92);
    append_list(pList , 22);
    append_list(pList , 102);
    append_list(pList , 18);
    append_list(pList , 45);
    
    print_list(pList);
    //--------------------------------------------------------
    cout << "add head list : 100" << endl;
    add_head_list(pList , 100);
    print_list(pList);
    //--------------------------------------------------------
    cout << "add list pos 1 : 999" << endl;
    insert_list(pList , 1 , 999);
    print_list(pList);

    //--------------------------------------------------------
    Node<int> *kNode = reci_node(pList, 11);
    if(kNode != NULL)
    {
        cout << "reci k : " << 11 << " data : "<< kNode->data << endl;
    }
    //--------------------------------------------------------
    Node<int> *mNode = middle_node(pList);
    if(mNode != NULL)
    {
        cout << "middle node : " << mNode->data << endl;
    }
    //--------------------------------------------------------
    cout << "bubble sort list: " << endl;
    bubble_sort_list(pList);
    print_list(pList);

    Node<int> *pOtherList = NULL;
    init_list(&pOtherList);
    append_list(pOtherList , 1);
    append_list(pOtherList , 312);
    append_list(pOtherList , 25);
    append_list(pOtherList , 57);
    append_list(pOtherList , 91);
    append_list(pOtherList , 27);
    append_list(pOtherList , 1001);
    append_list(pOtherList , 28);
    append_list(pOtherList , 49);

    cout << "other list : " << endl;
    print_list(pOtherList);

    cout << "bubble sort list: " << endl;
    bubble_sort_list(pOtherList);
    print_list(pOtherList);
    //--------------------------------------------------------
    Node<int> *pNewList = merge_list(pList , pOtherList);
    cout << "merge pList and pOtherList : " << endl;
    print_list(pNewList);

    //--------------------------------------------------------
    cout << "delete pos 18 list " << endl;
    delete_pos_list(pNewList , 18);
    print_list(pNewList);
    //--------------------------------------------------------
    cout << "print reverse list:" << endl;

    print_reverse_list(pNewList);

    //--------------------------------------------------------
    cout << "reverse list:" << endl;

    reverse_list1(pNewList);
    print_list(pNewList);

    cout << "list len : " << len_list(pNewList) <<  endl; 

    //--------------------------------------------------------
    delete_tail_list(pNewList);
    print_list(pNewList);
    //--------------------------------------------------------
    delete_head_list(pNewList);
    print_list(pNewList);
    //=========================================================
    cout << "clear list" << endl;
    clear_list(pNewList);
    
    cout << "list len : " << len_list(pNewList) <<  endl;

    cout << "delete list" << endl;
    delete_list(&pNewList);
    delete_list(&pList);
    delete_list(&pOtherList);
    return;
}

猜你喜欢

转载自blog.csdn.net/hhgfg1980/article/details/82830604