2012-03-14 9 views
1

各リストには、テキストへのポインター、int番号、次のリストへのポインターがある単一リンクリスト(クラスを使用)を作成したいと考えています。クラスを使用したC++単独リンクリスト

私は3つの機能を実装する必要が

:数が発生した最初のリストを削除し(単独でリンクされたリストにリストを挿入し、ポインタによって指されているテキストに応じのstrcmp持つ要素を並べ替え) インサート 除去(int型NUM)を。 単独リンクされたリスト全体を出力するprint()。

私は実行時にエラーを起こす除去機能に問題があります。問題はどこにあるのかという疑問がありますが、私はなぜそれがどういうわけか分かりません。

また、私はどのようにインサート関数にソートを実装する必要があるのか​​分からないので、もしあなたがアイデアを持っていて、あなたが私の削除関数のどこに私が本当に感謝しているか説明できたら。ループwhile (tmp != NULL) { … }後、tmpはおそらくNULLで、removes

#include <iostream> 
#include <cstdlib> 
#include <cstring> 

using namespace std; 

class list 
{ 
private: 
    int number; 

    char* word; 
    list* next; 
public: 
    void inserts(int num, char* text); 
    void removes(int num); 
    void print(); 
}; 
list* first; 

void list::print() { 
    cout <<"This is our list:"<<endl; 

    // Temp pointer 
    list *tmp = first; 

    // No nodes 
    if (tmp == NULL) { 
    cout << "EMPTY list" << endl; 
    return; 
    } 

    // One node in the list 
    if (tmp->next == NULL) { 
    cout <<"NUMBER:\t"<< tmp->number; 
    cout <<"\tWORD:\t"<< tmp->word << endl; 
    cout <<"--------------------------------"<<endl; 

    } 
    else { 
    // Parse and print the list 
    while (tmp != NULL){ 
     cout <<"NUMBER:\t"<< tmp->number; 
     cout <<"\tWORD:\t"<< tmp->word << endl; 
     cout <<"--------------------------------"<<endl; 

     tmp = tmp->next; 
    } 
} 
} 

void list::inserts(int num, char* word){ 
    // Create a new list 
    list* newlist = new list; 
    newlist->number=num; 

    newlist->word=word; 
    newlist->next=NULL; 

    // Create a temp pointer 
    list *tmp = first; 

    if (tmp != NULL) { 
    // Nodes already present in the list 
    // Parse to end of list 
    while (tmp->next != NULL) { 
     tmp = tmp->next; 
    } 

    // Point the last node to the new node 
    tmp->next=newlist; 
    } 
    else { 
    // First node in the list 
    first = newlist; 
    } 
} 

void list::removes(int num){ 
int k = 0; 
    list* tmp=first; 
    if(tmp==NULL) 
     return; 
     //Last node of the list 

    if (tmp->next == NULL && tmp->number==num) { 
    delete tmp; 
    first = NULL; 
    } 
    else { 
    //Parse thru the nodes 
    list* prev; 
    prev = new list; 
    while (tmp != NULL) 
    { 
     if (tmp->number == num && k == 0) 
      first = first->next; 
if (tmp->number == num) 
break; 
     prev = tmp; 

     tmp = tmp->next; 
k++; 
    } 

    //Adjust the pointers 
    prev->next=(tmp->next); 
    //Delete the current node 
delete tmp; 
delete prev; 

} 
} 


int main() 
{ 
    first->print(); 
    first->inserts(1200,"endian"); 
    first->print(); 
    /* first->inserts(10,"endianness"); 
    first->inserts(1200,"PEEK"); 
    first->inserts(1200,"POKE"); 
    first->inserts(1200,".MIL"); 
    first->print();*/ 
first->removes(100); 
first->print(); 
getchar(); 
} 
+2

なぜ?? *** 'のstd :: STRING'を使用するだけでなく、非常に具体的なcicumstancesで合理的である、そして、あなたの場合には、' charは* 'どんなメリットなしで物事を複雑に***「テキストへのポインタ」。 – leftaroundabout

+0

明らかに、C++はSTLの15年後のchar *で教えられています。うつ病。 – JohnMcG

答えて

0

は、ここでは、コードです。あなたは、あなたのプログラムがクラッシュし、NULLポインタを、逆参照されている

//Adjust the pointers 
prev->next=(tmp->next); 

はしかし、そのループの後、あなたはこれらの行を持っています。

これらにこれらの行を置き換えます

//Adjust the pointers 
if(tmp) 
    prev->next=(tmp->next); 


も注意してください:あなたはまだ removesルーチンでメモリ管理のバグを持っています。 prevの初期値で deleteを呼び出すことは決してなく、場合によっては保持するリストノードを削除します。あなたがやっていることの代わりに、それを次のようにして初期化してください: list* prev=0;、そして取り除くのは delete prevです。

+0

tmpがnullに変わる方法は? –

+0

'tmp = tmp-> next'という行は、最終的にリストの終わりを見つけます。 –

0

このループが実行されます。

while (tmp != NULL) 

あなたのtmpポインタがリストの最後に到達し、そして時にループブレークNULLになります。だから、

、あなたが実行します。

prev->next=(tmp->next); 

あなたの一時ポインタは「次」の値を持っていません。おそらく、ループの外側に別のポインタを保持する必要があります。

1

removes()関数の最後の行にdelete prev;を取り除きます。

私は専門家ではありませんが、削除することで、リスト内の最初のノードへの参照が失われています。ところで

は、良いコメントは、読むこと、それは非常に簡単に!

関連する問題