編程之美 - 無頭鏈表刪除節點

問題描述:

一個沒有頭指針的鏈表,從其中刪除任意一箇中間節點(非第一個,也不是最後一個)。


思路:

沒有頭指針,所以無法使用遍歷的方式找到目標節點的上一個節點。但可以使用的是目標節點的下一個節點。

因爲當前節點是要被刪除的,所以當前節點的值已經不需要被保存了,可以直接用下一個節點的值將其替換。

然後,直接刪除下一個節點即可。


代碼:

void delete_node(stnode* &target)
{
    stnode *p = NULL;
    if (NULL == target)
	return;

    if (NULL == target->_next)
    {
        cout << "it's last node will not delete" << endl;
	    return;
    }
    p = target;
    target = target->_next;

    p->_data = p->_next->_data;
    p->_next = p->_next->_next;
    delete target;
    target = NULL;
}



擴展問題:

給一個鏈表和它的頭指針,只遍歷一次,將鏈表反轉。


思路:

將鏈表反轉,考慮將指針的指向反轉了即可,然後將頭指針指向原來的最後一個節點即可。

例如:

1->2->3->4->NULL

將指針的指向反轉

NULL<-1<-2<-3<-4


代碼:

void swap_nodes(stnode* &head)
{
    stnode *p1 = NULL;
    stnode *p2 = NULL;
    stnode *p3 = NULL;

    if ((NULL == head) || (NULL == head->_next))
        return;
	
    p1 = head;
    p2 = p1->_next;
    p1->_next = NULL;
    while (p2 != NULL)
    {
        p3 = p2->_next;
        p2->_next = p1;
	p1 = p2;
	p2 = p3;
     }
     head = p1;
}


完整代碼:


#include <iostream>


using namespace std;

typedef struct _stnode
{
    int _data;
    _stnode *_next;
} stnode;

void printlink(stnode* link)
{
    stnode *p = link;
    if (p == NULL)
    {
        cout << "NULL" << endl;
    }
        
    while (p != NULL)
    {
        cout << p->_data << " -> ";
        p = p->_next;
    }
    cout << "NULL" <<endl<<endl;
}

void delete_node(stnode* &target)
{
    stnode *p = NULL;
    if (NULL == target)
        return;

    if (NULL == target->_next)
    {
        cout << "it's last node will not delete" << endl;
        return;
    }
    p = target;
    target = target->_next;
    p->_data = p->_next->_data;
    p->_next = p->_next->_next;

    delete target;
    target = NULL;
}

void swap_nodes(stnode* &head)
{
    stnode *p1 = NULL;
    stnode *p2 = NULL;
    stnode *p3 = NULL;

    if ((NULL == head) || (NULL == head->_next))
        return;
    
    p1 = head;
    p2 = p1->_next;
    p1->_next = NULL;
    while (p2 != NULL)
    {
        p3 = p2->_next;
        p2->_next = p1;
        p1 = p2;
        p2 = p3;
    }
    head = p1;
}

void main()
{
    int i = 0, test = 0;
    int arr[] = {1,2,3,4,5,6,7,8,9,10,};
    int len = sizeof(arr)/sizeof(arr[0]);

    stnode *ptest = NULL;
    stnode *pnew = NULL;
    stnode *phead = NULL;

    for (i = 0; i < len; i++)
    {
        pnew = new stnode;
        pnew->_data = arr[i];
        pnew->_next = NULL;
        if (i == 0)
        {
            phead = pnew;
            ptest = pnew;
        }
        else
        {
            ptest->_next = pnew;
            ptest = pnew;
        }
    }
    cout << "Initial link ================" << endl;
    printlink(phead);
    cout << endl;

    test = 6;
    ptest = phead;
    for(i = 0; i < test-1; i++)
        ptest = ptest->_next;
    
    delete_node(ptest);

    cout << "After deleting node ================" << endl;
    printlink(phead);
    cout << endl;

    cout << "\n\nSwap Links ================" << endl;

    swap_nodes(phead);
    printlink(phead);

    ptest = phead;
    while(ptest != NULL)
    {
        pnew = ptest;
        ptest = ptest->_next;

        delete pnew;
        pnew = NULL;
    }

    cin >> i;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章