各リストには、テキストへのポインター、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();
}
なぜ?? *** 'のstd :: STRING'を使用するだけでなく、非常に具体的なcicumstancesで合理的である、そして、あなたの場合には、' charは* 'どんなメリットなしで物事を複雑に***「テキストへのポインタ」。 – leftaroundabout
明らかに、C++はSTLの15年後のchar *で教えられています。うつ病。 – JohnMcG