私は、リンクされたリスト要素(文字列)を文字列の最初の文字の後に並べ替える宿題を持っています。 例:からC++ - リンクされたリストの挿入Sort(String要素)
: Pineapple-> Apple-> Ash-> ABC-> Pearl-> Bonfire->ボール
へ: pple-> SH-> BC->B onfire->Bオール>P ineapple->Pアール(唯一の最初の文字)
は私が作っ機能:
void insertionSort()
{
first = current;
Node* insertionPointer = first;
current = current -> next;
for (start(); !end(); next()){ // Running through all list nodes
while (current != NULL) {
insertionPointer = first;
while(insertionPointer->next != current) {
if (insertionPointer->data.at(0) > current-> data.at(0)){ // Trying to sort strings alphabetically
// (after only first char)
string temp = current->data;
current->data = insertionPointer->data;
insertionPointer->data = temp;
}
else {
insertionPointer = insertionPointer->next;
}
}
}
}
}
をしかし、私はセグメンテーションフォールトを取得する - 私はそれが私はいくつかの情報を取得しようとしている意味を推測、私はアクセスできないということ?また、わかりません:
if (insertionPointer->data.at(0) > current-> data.at(0))
文字列を最初に比較しますか?私はここで実験しようとしています。 :( 私はすべてのコードも下に掲載していますので、リストやその他の関数をどのように構造化しているのか分かりますので参考にしてください。コード:
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;
class Node
{
public:
string data;
Node *next;
Node (string city) { data = city; next = NULL; };
};
class List
{
protected:
Node *first, *last;
public:
Node *current;
public:
List() { first = last = current = NULL; };
void add_element (string city);
void delete_element();
~List();
bool is_empty() { return (first == NULL); };
void start() { current = first; };
bool end() { return (current == NULL); };
void next(){if (!end())current = current -> next;};
void print();
void insertionSort()
{
first = current;
Node* insertionPointer = first;
current = current -> next;
for (start(); !end(); next()){ // Running through all list nodes
while (current != NULL) {
insertionPointer = first;
while(insertionPointer->next != current) {
if (insertionPointer->data.at(0) > current->data.at(0)){ // Trying to sort strings alphabetically
// (after only first char)
string temp = current->data;
current->data = insertionPointer->data;
insertionPointer->data = temp;
}else{
insertionPointer = insertionPointer->next;
}
}
}
}
}
};
int main()
{
string s;
List l;
l.add_element("Pineapple");
l.add_element("Apple");
l.add_element("Ash");
l.add_element("Abc");
l.add_element("Pearl");
l.add_element("Bonfire");
l.add_element("Ball");
l.print();
cout << endl;
l.insertionSort();
l.print();
return 0;
}
void List::add_element (string city)
{
Node *p = new Node (city);
if (first == NULL) first = last = p;
else last = last -> next = p;
current = p;
};
void List::delete_element()
{
Node *p = first;
if(!is_empty())
{ if (current == first) current = first-> next;
first = first -> next;
delete p;
if(is_empty())last = NULL;
}
};
void List::print()
{
for (start(); !end(); next())
{
cout << current->data << endl;
}
cout << endl;
};
List::~List()
{
while (!is_empty())
{
delete_element();
};
cout << "All memory of nodes deleted!"<< endl;
};
に変更ループ条件を(私たちに)未知のファイルからの入力を取得しないでください。既知のハードコードされた値で必要なリスト関数を呼び出してください。これはあなたがテストする方法です。特に、他の人が問題を再現したい場合は、そうしてください。もし 'add_element(" abc "); add_element( "123"); add_element( "Joe"); add_element( "Bob"); insertionSort(); 'は動作しませんが、ファイルからの読み込みは動作しません。 – PaulMcKenzie
@PaulMcKenzie私はあなたの助言を受けて、コードの主な機能を変更しました。私はまだ解決できません:1.セグメンテーションフォールト2. "if(insertionPointer-> data.at(0)> current-> data.at(0)){"この文は文字列の最初の文字の後にソートされますか? – Maartin1996
あなたのロジックはすべて間違っています。あなたの 'add:element'はソートされたものを挿入しなければなりません。 – Thomas