2012-04-18 49 views
0

我創建了一個LinkedList類,它具有刪除列表中第一個元素的功能,以及刪除列表中最後一個元素的功能。第一個很容易,在刪除元素後,我將它設置爲指向下一個元素。很棒。但是,當我刪除最後一個元素時,我必須將它指向列表中的最後一個元素。我無法弄清楚如何做到這一點。請指教。下面是代碼:如何在C++中的鏈表中創建一個先前的指針?

void LinkedList::pop_front() 
{ 
    mFront->data = NULL; 
    mFront = mFront->next; 
} 

我怎樣才能刪除最後一個元素,但復位尾巴的功能,使其指向新的尾巴?

void LinkedList::pop_back() 
{ 
mBack->data = NULL; 
... 
} 

class LinkedList 
{ 
    public: 

     // Default Constructor 
     // Purpose: Initializes an empty list 
     // Parameters: none 
     // Returns: none 
     LinkedList(); 

     // The push_front function 
     // Purpose: add an item to the front of the list 
     // Parameters: a int item for the front 
     // Returns: none   
     void push_front(int data); 

     // The push_back function 
     // Purpose: insert an item into the back of the list 
     // Parameters: int item to add the the back 
     // Returns: none     
     void push_back(int data); 

     // The pop_front function 
     // Purpose: delete the item in the front of the list 
     // Parameters: none 
     // Returns: none 
     void pop_front(); 

     // the pop_back function 
     // Purpose: remove the item at the end of the list 
     // Parameters: none 
     // Returns: none   
     void pop_back(); 

     // The getFirst function 
     // Purpose: print the first item in the list 
     // Parameters: none 
     // Returns: none 
     void getFirst(); 

     // the GetLast function 
     // Purpose: return the last item in the list 
     // Parameters: none 
     // Returns: none 
     void getLast(); 

     void printList(); 

     // the clear function 
     // Purpose: clear the list, free memory 
     // Parameters: none 
     // Returns: none 
     void clear(); 

     // Destructor 
     // Purpose: clear up memory 
     // Parameters: none 
     // Returns: none 
     ~LinkedList(); 

    private: 

      LinkedList *mFront; // point to the front of our list 
      LinkedList *mBack; // point to the back of our list 
      LinkedList *next; // the next node 
      LinkedList *previous; // the previous node 
      int data; // our list data manipulator 
+0

如果你想O(1)去除的元素,你需要一個[雙向鏈表(HTTP:/ /en.wikipedia.org/wiki/Doubly_linked_list)(即所有節點上的'next'和'prev'指針)。 – 2012-04-18 21:03:42

+0

如果這是家庭作業,最好添加'家庭作業'標籤,以便人們可以嘗試給出更多的解釋和更少的代碼。 – thiton 2012-04-18 21:06:07

回答

4

單鏈表不提供O(1)刪除最後一個元素。您必須從頭開始查看整個列表以查找倒數第二個元素。

Node* i = mFront; 
while (i->next != mBack) i = i->next; 
mBack = i; 
+0

它表示Node未定義。我是初學者,是否需要參考該課程的數據成員?或者,創建一個新的指針?我編輯了上面的代碼來顯示我的類名和數據成員。 – Paxwell 2012-04-18 21:29:58

+0

明白了。謝謝。 – Paxwell 2012-04-19 13:52:07

1

如果列表不是雙鏈接,你就必須在開始的第一個元素找到最後一個:

void LinkedList::pop_back() 
{ 
    mBack->data = NULL; 
    current = mFront; 
    do{ 
     current = current->next 
    } while (current->next); 
    mBack = current; 
} 

非常重要 - 因爲data似乎是一個指針,你可能會遇到內存泄漏。只設置data = NULL不釋放內存,你必須明確地將其刪除:

delete mFront->data; 
+0

你的代碼也給我錯誤。我不確定目前是什麼引用,我改變了上面的代碼來代表我的課 – Paxwell 2012-04-18 21:34:11

+0

想通了。謝謝。 – Paxwell 2012-04-19 13:52:19