簡體   English   中英

C ++-鏈表插入排序(字符串元素)

[英]C++ - Linked List Insertion Sort (String elements)

我有一個家庭作業,必須在字符串中的第一個字符之后對鏈接列表元素(字符串)進行排序。 例:

來自:菠蘿->蘋果->灰-> Abc->珍珠->篝火->球

於: pple-> SH-> BC-> onfire-> 清一色> ineapple-> 厄爾(僅第一個字符)

我做了一個功能:

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

您的程序很可能在這里崩潰:

while(insertionPointer->next != current)  {

因為執行時insertionPointer已變為null

insertionPointer = insertionPointer->next;

將循環條件更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM