C ++ - 链接列表插入排序(字符串元素)

时间:2016-04-25 15:45:21

标签: c++ linked-list

我是一个家庭作业,我在字符串中的第一个字符后对链表元素(字符串)进行排序。 例如:

来自: Pineapple-> Apple-> Ash-> Abc-> Pearl-> Bonfire-> Ball

收件人: A pple-> A sh-> A bc-> B onfire-> B all-> 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;
};

1 个答案:

答案 0 :(得分:0)

你的程序最有可能崩溃:

while(insertionPointer->next != current)  {

因为insertionPointer在您执行

时变为null
insertionPointer = insertionPointer->next;

将循环条件更改为

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