交换链表结点

来源:互联网 发布:淘宝小卖家怎么定位 编辑:程序博客网 时间:2024/06/05 04:31

LinkedList是最基礎的數據結構,也是面試中常見的題目,別千萬別小看了它,搞不好就折騰死你。麻雀雖小,五臟俱全!相信我,I'm not joking, I'm serious.

交換鏈表結點是最最基礎不過了,特別是在白板上寫程序的時候,極其考驗人的底層功哩,下面來看一道題目:

給定一個單向鏈表(不選擇雙向,雙向相對來說容易操作),要求交換偶數的結點。比如:1,2,3,4,則交換後為:2,1,4,3。如:1,2,3,4,5,則交換後為:2,1,4,3,5


交換過程也不是特別複雜,三個指針而已啦。這道題其實是另一道的變種,即"交換任意兩個節點"(這題需要考慮頭部、尾部的一些特殊情況)。呵呵~,微軟最喜歡出鏈表的題目了!Trust me,cheres amies。


OK,先來看看基本的定義吧。

1. 我先定義了一個結構體,其用來存放一個數和一個指標。你可以用template<class T>來進行泛化,這裡只做基本的思路,具體實現就留給大家啦。

    struct Node {        int     m_data;        Node*   m_next;        Node(int val = int(), Node *pNext = NULL) {            m_data = val;            m_next = pNext;        }    };

其他的函數定義如下,包括插入,刪除,頭部插入,尾部插入等,在下面會有說明,先來看看具體的交換操作吧。

void SwapEvenNode() {        // No Element, just return        if (m_head->m_next == m_tail)            return;                for (int i = 0; i < m_size; i++) {            if ((i+1) % 2 == 0) {                Node *pPrevPrev = FindPointBeforeIndex(i);                Node *pPrev = FindPointBeforeIndex(i+1);                Node *pCurrent = pPrev->m_next;                  // Swap node                pPrev->m_next = pCurrent->m_next;                pCurrent->m_next = pPrev;                pPrevPrev->m_next = pCurrent;            }        }    }

沒錯,你沒看錯,就是那麼簡單。 It works good!


OK,再來看看其他的一些操作。附上完整的類。

#include <iostream>#include <list>#include <vld.h>using namespace std;class LinkedList {private:    struct Node {        int     m_data;        Node*   m_next;        Node(int val = int(), Node *pNext = NULL) {            m_data = val;            m_next = pNext;        }    };public:    LinkedList() {        m_head = new Node;        m_tail = new Node;        m_head->m_next = m_tail;        m_size = 0;    }    ~LinkedList() {        ClearUp();    }    void push_back(int val) {        // Find the point before tail;                Node *p = FindPointBeforeTail();        Node *pNewNode = new Node(val,m_tail);        p->m_next = pNewNode;        m_size++;    }    void push_front(int val) {        Node *pNewNode = new Node(val,m_head->m_next);           m_head->m_next = pNewNode;        m_size++;    }    void pop_back() {         Node *p = FindPointBeforeIndex(m_size);         delete p->m_next;         p->m_next = m_tail;         m_size--;    }    void pop_front() {        m_size--;        Node *p = m_head->m_next;        m_head->m_next = p->m_next;        delete p;    }    Node* getBack() {        return FindPointBeforeTail();    }    Node* getFront() {        return m_head->m_next;    }    void insertAfter(int index, int val) {        m_size++;        Node *pCurrent = FindPointBeforeIndex(index+2);        Node *pNewNode = new Node(val,pCurrent->m_next);        pCurrent->m_next = pNewNode;    }    void insertBefore(int index, int val) {        m_size++;        Node *pCurrent = FindPointBeforeIndex(index+1);        Node *pNewNode = new Node(val,pCurrent->m_next);        pCurrent->m_next = pNewNode;    }        void deleteBefore(int index) {        Node *pCurrent = FindPointBeforeIndex(index+1);        Node *p = pCurrent->m_next;        pCurrent->m_next = p->m_next;        delete p;        m_size--;    }    void deleteAfter(int index) {        Node *pCurrent = FindPointBeforeIndex(index+2);        Node *p = pCurrent->m_next;        pCurrent->m_next = p->m_next;        delete p;        m_size--;    }    int size() const {        return m_size;    }    void SwapEvenNode() {        // No Element, just return        if (m_head->m_next == m_tail)            return;                for (int i = 0; i < m_size; i++) {            if ((i+1) % 2 == 0) {                Node *pPrevPrev = FindPointBeforeIndex(i);                Node *pPrev = FindPointBeforeIndex(i+1);                Node *pCurrent = pPrev->m_next;                  // Swap node                pPrev->m_next = pCurrent->m_next;                pCurrent->m_next = pPrev;                pPrevPrev->m_next = pCurrent;            }        }    }    void printLinkedList() {        Node *p = m_head;        while (p->m_next != m_tail) {            cout << p->m_next->m_data << ",";            p = p->m_next;        }        cout << endl;    }private:    void ClearUp() {        while (m_head != NULL) {            Node *p = m_head;            m_head=p->m_next;            delete p;        }    }    Node* FindPointBeforeTail() {        Node *p = m_head;        while (p->m_next != m_tail)            p = p->m_next;        return p;    }    Node* FindPointBeforeIndex(int index) {        Node *p = m_head;        int i = 0;        while (i < index-1) {            p = p->m_next;            i++;        }        return p;    }private:    Node        *m_head;    Node        *m_tail;    int          m_size;};int main() {    LinkedList *pList = new LinkedList;    cout << "Input the number to list: (Press -9999 to exit)" << endl;    int val = 0;    while (cin >> val && val != -9999) {        pList->push_back(val);    }    cout << "The list is: " << endl;    pList->printLinkedList();       cout << "After swap even node, the list is:" << endl;    pList->SwapEvenNode();    pList->printLinkedList();    delete pList;    system("pause");}

0 0
原创粉丝点击