2016-04-25 5 views
0

Ich habe eine Hausaufgabe, wo ich verknüpfte Listenelemente (Strings) nach dem ersten Zeichen in einer Zeichenfolge sortieren muss. Beispiel:C++ - Einfügung der verknüpften Liste Sort (String-Elemente)

Von: Ananas-> Apple-> asche> ABC-> Pearl-> Bonfire-> Ball

An:A pple->A sh->A BC->B onfire->B Nur->P ineapple->P Earl (nur die erste char)

Ich habe eine Funktion:

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; 
       } 
      } 
     } 
    } 
} 

Aber ich einen Segmentation Fault bekommen - ich denke, das bedeutet, dass ich versuche, einige Informationen zu bekommen, dass ich nicht zugreifen kann? Auch ich bin mir nicht sicher, ob:

if (insertionPointer->data.at(0) > current-> data.at(0)) 

Vergleicht Zeichenketten zuerst char? Ich versuche nur hier zu experimentieren. :( Nur um sicherzustellen, werde ich unten auch meinen ganzen Code, so dass Sie sehen können, wie ich Listen und andere Funktionen strukturiert. Ich bin neu in diesem Zeug - jede Information wird hilfreich sein.

Volles Programm Code:

#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; 
}; 
+0

Bitte setzen Sie sich nicht eine Eingabe von einer unbekannten Datei (für uns). Rufen Sie einfach die erforderlichen Listenfunktionen mit bekannten, fest codierten Werten auf. So solltest du testen, vor allem, wenn andere das Problem duplizieren wollen. Wenn 'add_element (" abc "); add_element ("123"); add_element ("Joe"); add_element ("Bob"); insertionSort(); 'funktioniert nicht, das Lesen von einer Datei wird nicht funktionieren. – PaulMcKenzie

+0

@PaulMcKenzie Ich nahm Ihren Rat und änderte meine Hauptfunktion in Code. Ich kann immer noch nicht lösen: 1. Segmentierungsfehler 2. "if (insertionPointer-> data.at (0)> current-> data.at (0)) {" Wird diese Anweisung nach nur dem ersten Zeichensatz von Zeichenfolgen sortieren? – Maartin1996

+0

Ihre Logik ist alles falsch. Ihr 'add: element' sollte sortiert eingefügt werden. – Thomas

Antwort

0

Ihr Programm wird höchstwahrscheinlich hier abstürzt:

while(insertionPointer->next != current) { 

weil insertionPointer hat null, wenn Sie

ausgeführt

Änderung der Schleifenbedingung zu

while(insertionPointer && insertionPointer->next != current) { 
+0

Gibt immer noch Segmentierungsfehler. – Maartin1996

+0

Ihr Code ist komplett geändert und es stürzt nicht ab [sehen Sie Ihren Code live] (http://coliru.stacked-crooked.com/a/9573530a60cc760a). es sortiert auch nicht .. – Thomas

Verwandte Themen