2014-04-15 67 views
3

我在C++中使用模板來創建鏈接列表。我編寫了一些函數,但刪除節點(無論是從頭部還是尾部)都不能正常工作。它顯示一個錯誤並終止。在調試時,監視窗口中顯示的'this'包含一個空的頭部,我不知道爲什麼。這裏是我的代碼:刪除鏈接列表中的節點不起作用?

SLlistSc.h

#ifndef SLlistSc_H 
#define SLlistSc_H 

#include<iostream> 
using namespace std; 

template<class T> class SLlist; 

template <class T> class snode{ 
    friend class SLlist<T>; 
private: 
    T info; 
    snode<T> *next; 
public: 
    inline T getter() const { return info; } 
    inline void setter(T setValue){ info = setValue; } 
    snode(){ info = 0; next = 0; } 
    snode(T inf , snode *nex = 0){ info = inf; next = nex; } 
    ~snode(){ delete next; } 

}; 
template <class T> class SLlist{ 
private: 
    snode<T> *head, *tail; 
    static int total; 
public: 
    SLlist(){ head = tail = 0; } 

    ~SLlist(){ 
     snode<T> *p = head; 
     while (head != 0){ 
      head = head->next; 
      delete p; 
      p = head; 
     } 
    } 
    inline void incrcnt(){ total++; } 
    inline void decrcnt(){ total--; } 
    void displayList(); 
    void addToHead(T inf); 
    void addToTail(T inf); 
    T deleteFromHead(); 
    T deleteFromTail(); 
    int returnIndex(); 
    void deleteInfo(); 
    void deleteAll(); 
}; 

template <class T> void SLlist<T>::displayList(){ 
    snode<T> *p = head; 
    while (p != 0){ 
     cout << "\n->" << p->info; 
     p = p->next; 
    } 
    return; 
} 

template <class T> void SLlist<T>::addToHead(T inf){ 
    snode<T> *temp = new snode<T>(inf, 0); 
    if (head == 0){ head = tail = temp; temp = 0; delete temp; incrcnt(); return; } 
    else { temp->next = head; head = temp; temp = 0; delete temp; incrcnt(); return; } 
} 

template <class T> void SLlist<T>::addToTail(T inf){ 
    snode<T> *temp = new snode<T>(inf, 0); 
    if (head == 0){ head = tail = temp; temp = 0; delete temp; incrcnt(); return; } 
    else{ tail->next = temp; tail = temp; temp = 0; delete temp; return; } 
} 

template <class T> T SLlist<T>::deleteFromHead(){ 
    if (head == 0){ cout << "\nList is already empty"; return (T)0; } 
    if (head == tail){ delete head; head = tail = 0; T info = head->info; return info; } 
    else { 
     T info = head->info; 
     snode<T> *temp = head; 
     head = head->next; 
     temp = 0; 
     delete temp; 
     return info; 
    } 
} 

template <class T> T SLlist<T>::deleteFromTail(){ 
    if (head == 0){ cout << "\nList is already empty"; return (T)0; } 
    if (head == tail){ delete head; head = tail = 0; T info = head->info; return info; } 
    else { 
     T info = tail->info; 
     snode<T> *temp = head; 
     while (temp != 0) 
     { 
      temp = tail; 
     } 
     delete tail; 
     tail = temp; 
     temp = 0; delete temp; 
     return (T)info; 
    } 
} 

#endif 

和cpp文件:SLlistSc.cpp

// SLlistSc.cpp : Defines the entry point for the console application. 
// 

#include"SLlistSc.h" 
#include<iostream> 
using namespace std; 

template <class T> int SLlist<T>::total = 0; 

void main() 
{ 
    cout << "\t\t\t Singly Linked List Super Class Implementation"; 
    SLlist<int> List1; 
    int userchoice=0, inf=0; 
    do{ 
     cout << "\n\n\t\t Menu" 
      << "\n\t1. Display List" 
      << "\n\t2. Add to Head" 
      << "\n\t3. Add to tail" 
      << "\n\t4. Delete from Head" 
      << "\n\t5. Delete from Tail" 
      << "\n\t6. Exit"; 
     cin >> userchoice; 

     switch (userchoice){ 
     case 1: List1.displayList(); break; 
     case 2: cout << "\nEnter info to be added:"; 
       cin >> inf; 
       List1.addToHead(inf); break; 
     case 3: cout << "\nEnter info to be added:"; 
       cin >> inf; 
       List1.addToTail(inf); break; 
     case 4: inf = List1.deleteFromHead(); 
       cout << "\n Value" << inf << "deleted from list"; break; 
     case 5: inf = List1.deleteFromTail(); 
       cout << "\n Value" << inf << "deleted from list"; break; 
     case 6: cout << "\n\t\t\t\tExiting"; 
       exit(0); 
     default: continue; 
     } 
    } while (userchoice < 4); 
} 

我想問的另一件事是,當我們使用模板類上(例如A級),我們需要在任何地方都用A代替A。是否因爲編譯器生成一個新的類即ie。

一個<牛逼>心不是= A.

,同時使用snode類中友元類SLlist,爲什麼我要聲明如下:

template<class T> class SLlist; 

的snode課前?爲什麼不能我直接在snode類聲明爲:

friend class SLlist<T>; 

此外,如果有improvemet的在我的代碼的機會,請去努力我。謝謝。

+0

*顯示錯誤並終止* - 錯誤是什麼? 「 –

+0

」SL111Sc.exe中0x01323DB6處未處理的異常:0xC0000005:訪問衝突讀取位置0xFEEEFEF2。「它與'這個'指針顯示在監視窗口中有關,因爲它在頭部和尾部都是空的,我不知道爲什麼? –

+0

我認爲你正在做你的節點的雙重刪除。節點析構函數正在刪除節點,然後你的列表析構函數正在刪除節點。 – pstrjds

回答

3

您正試圖使用​​無效的指針,因爲包含該指針的塊已被釋放。

Microsoft C++調試運行時庫會使用模式0xFEEEFEEE覆蓋釋放的內存,從而使此問題更易於識別。

+0

任何與我的模板混淆場景? –

+0

請爲模板問題提出一個新問題,這與指針問題無關。 –