2014-03-27 410 views
-2

插入功能我一直在指針與坦然C現在++掙扎了幾個星期。我有一個放棄的學校項目(截止日期已過)。即使我無法開啓,我仍然希望完成它練習,我堅持我清晰的方法,但可能有其他方法我應該做一些這樣的。鏈表清晰,C++

.h文件中

using namespace std; 

template < typename T > // Forward declaration of the SimpList class 
class SimpList; 

//-------------------------------------------------------------------- 
// 
// Definition of class Node<T> 
// 
//-------------------------------------------------------------------- 

template < typename T > 
class Node     // Node class for the SimpList class 
{ 
    private: 

    // Constructors 

    Node() { next = 0; } // default constructor 

    // Complete the definition inline 

    Node (const T &initItem, Node<T> *ptr) { 
     value=initItem; 
     next=ptr; 
    } 

    // Data members 

    T   value; // Node data item 
    Node  *next; // Pointer to the next node 

    friend class SimpList<T>; 
}; 

//-------------------------------------------------------------------- 
// 
// Definition of class SimpList<T> 
// 
//-------------------------------------------------------------------- 

template < typename T > 
class SimpList 
{ 
    public: 

    // Constructor (add your code inline) 

    SimpList() 
    { 
     head = &PHONY; 
     length=0; 
     // complete the data member intialization 
    } 

    // Destructor (add your code inline) 

    ~SimpList() { clear();} 

    // List manipulation operations 

    void insert (const T &newitem); // insert a data item 

    bool remove (T &item);   // remove data item 

    bool find (T &item) const;  // find data item 

    void clear();      // empty the list 

    // (add your code inline) 
    bool isEmpty() const { 
     if (length == 0) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
     } 

    // length accessor method (add your code inline) 
    int size() const { return length; } 

    // print the list items 
    void print() const; 

    private: // data members 

    Node<T> PHONY ;  // empty node that anchors the list 

    Node<T> *head;  // pointer to the beginning of the list 

    int length;   // length of list 
}; 

//-------------------------------------------------------------------- 
// 
// Implementation section 
// 
//-------------------------------------------------------------------- 

template < typename T > 
void SimpList<T>::print() const 
{ 
    if (length == 0) 
    { 
    cout << "List is empty." << endl; 
    return; 
    } 

    Node<T> *ptr=head; 
    while (ptr != NULL) 
    { 

    cout << ptr->value << " "; 
    ptr = ptr->next; 
    } 
    cout << endl; 
} 

template <typename T> 
void SimpList<T>::insert (const T &newitem) 
{ 
Node<T> *p = new Node<T>; 
p->value = newitem; 
p->next = head; 
head = p; 
length++; 

} 

template <typename T> 
void SimpList<T>::clear() 
{ 

    Node<T> *p = head->next; 

    while(p != NULL) 
{ 
    head=head->next; 
    delete p; 
} 

length = 0; 

} 
template <typename T> 
bool SimpList<T>::find (T &item) const 
{ 
Node<T> *p; 
    for(p=head; p!=NULL; p=p->next) 
     if(p->value==item) break; 

     if(p!=NULL) 
     { 
      item=p->value; 
     return true; 
     } 
     else 
     { 
      return false; 
     } 


} 
template <typename T> 
bool SimpList<T>::remove (T &item) 
{ 
Node<T> *p,*c; 

     for(c=head; c!=NULL;p=c,c=c->next) 
      if(c->value==item) break; 

     if(c!=NULL) 
     { 
      item=c->value; 
      p->next=c->next; 
      delete c; 
      length --; 
      for(c=head;c!=NULL;c=c->next) 
      cout<<c->value<<" "; 
      return true; 
     } 
     else 
      return false; 


} 

.cpp文件是在這裏。

#include <iostream> 
#include <string> 
#include "simpList.h" 


int main() 
{ 
    SimpList<int> intList; // (empty) list of integers 

    cout << "Let's build a sorted list of integers." << endl; 
    cout << endl << "Uninitialized List: "; 
    intList.print(); 
    cout << endl << "Length: " << intList.size() << endl; 

    int intData[] = { 5, 3, -2, 7, 9, -8, 1, -4 }; 

    for (int i=0; i<8; i++){ 
    intList.insert(intData[i]); 
    } 
    cout << endl << "After inserting 8 integers: "; 
    intList.print(); 
    cout << endl << "Length: " << intList.size() << endl; 

    cout << endl << "--- Testing the \"find\" method:" << endl; 

    int t = 5; 
    bool ret = intList.find(t); 

    cout << endl << t << " is in the list: " 
    << (ret ? "true" : "false") << endl; 

    t = 6; 
    ret = intList.find(t); 

cout << endl << t << " is in the list: " 
     << (ret ? "true" : "false") << endl; 

    cout << endl << "--- Testing the \"remove\" method:" << endl; 

    t = 5; 
    ret = intList.remove(t); 

    cout << endl << t << " has been removed: " 
     << (ret ? "true" : "false") << endl; 

    cout << endl << "Remaining list: "; 
    intList.print(); 
    cout << endl << "Length: " << intList.size() << endl; 

    cout << endl << "--- Testing the \"clear\" method:" << endl << endl; 

    intList.clear(); 
    intList.print(); 
    cout << endl << "Length: " << intList.size() << endl; 

    cout << endl << "--- Testing the \"isEmpty\" predicate:" << endl; 

    cout << endl << "The integer list is now empty: " 
     << (intList.isEmpty()? "true" : "false") << endl << endl; 

    cout << "Now, let's build a sorted list of strings." << endl; 

    string strData[] = { "Maria", "Ann", "Emily", "Vivian", 
         "Beth", "Carla", "Susan" }; 

    SimpList<string> strList; 

    for (int i=0; i<7; i++){ 
    strList.insert(strData[i]); 
    } 
    cout << endl << "After inserting 7 names:" << endl; 
    strList.print(); 
    cout << endl << "Length: " << strList.size() << endl; 

    cout << endl << "--- Testing the \"remove\" method:" << endl; 
    cout << endl << "Bye Carla!" << endl; 

    string str = "Carla"; 
ret = strList.remove(str); 

    cout << endl << str << " has been removed: " 
     << (ret ? "true" : "false") << endl; 

    cout << endl << "Remaining list:" << endl; 
    strList.print(); 
    cout << endl << "Length: " << strList.size() << endl; 

    cout << endl << "--- Testing the \"insert\" method:" << endl; 
    cout << endl << "Bienvenue Pauline!" << endl; 

    string nom = "Pauline"; 
    strList.insert(nom); 

    cout << endl << "Extended list:" << endl; 
    strList.print(); 
    cout << endl << "Length: " << strList.size() << endl; 

    cout << endl << "--- Auf Wiedersehen!" << endl;`enter code here` 
    cout << endl << "End of \"main()\"." << endl 
     << "At this point, the destructor is called." << endl; 


system("PAUSE");} 
  1. 固定

  2. 當打印出在列表中的項目,它打印的所有項目,以及-858993460,我已經用盡了我的如何解決它的知識。

我將非常感謝您的傢伙/ gals幫助或指向正確的方向。

+2

你從來沒有真正改變'p'(在'清除()'),它的初始設置後,但你多次可能將其刪除... – John3136

+0

這個問題似乎是題外話,因爲它是關於這樣濫用一個在線人羣調試服務。 **提到OP:**你有任何懸掛指針嗎?你有沒有調試過,並檢查你的指針值是否正確設置每個操作。先檢查一下,然後回來問一個你不明白的問題! –

+0

打印中的隨機值是列表中的'PHONY'節點。 –

回答

0

想通過該函數的邏輯。畫出來作爲紙箱,制定出的順序指針需要移動和刪除

考慮這個序列中刪除鏈接的列表。

while(there is still an item left in the list) { 
    take a copy of the existing head pointer 
    move the head pointer to the next item 
    delete the old head using the copy of the head pointer you took before changing it 
} 
set length to zero 

注意,在一個正常的列表中,頭指針應當列表爲空,而「下一個」指針將在列表的最後一個項目零設置爲零。

0

試試這個在您的清除功能

while(head!=NULL) 

。 {node * p = head; 。頭=頭戴式>下; 。刪除p 。 }

,並且確保最初的頭被設置爲null.otherwise最後一次迭代過程中它會採取一些垃圾值。

+0

謝謝,當從cpp文件打印出列表時,它返回-4,1,-8,9,7,-2,3,-858993460任何想法爲什麼它會打印出「-858993460」,我認爲這是我的PHONY節點? – Arob33

+0

最初你將頭指針分配給PHONY節點。而且它有一些地址,那麼當你在第一個p-> next = head語句中建立列表時,你將把該PHONY地址值賦給鏈表的第一個節點的下一個,它不是null,所以它將迭代一個更多的時間,並會打印垃圾值。嘗試將PHONY初始化爲NULL,然後將其分配給頭部。它應該打印好。 –