我想在双链表上进行堆排序。我无法将堆排序实现到我的双链表。此列表包含学生信息。
我的代码:
// C++ program for implementation of Heap Sort
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct DoubleLinked{
int number;
int failCount;
string name;
string surname;
string field;
DoubleLinked * next;
DoubleLinked * prev;
} *CompStudents = NULL, *CompIter = NULL;
DoubleLinked* DoubleLinkedInsert(DoubleLinked* temp, int number, string name, string surname, string field, int failCount){
if(temp == NULL){
temp = new DoubleLinked;
temp->number = number;
temp->name = name;
temp->surname = surname;
temp->failCount = failCount;
temp->field = field;
temp->next = NULL;
temp->prev = NULL;
return temp;
}else{
CompIter = temp;
while (CompIter->next != NULL){
CompIter = CompIter->next;
}
CompIter->next = new DoubleLinked;
CompIter->next->prev = CompIter;
CompIter = CompIter->next;
CompIter->number = number;
CompIter->name = name;
CompIter->surname = surname;
CompIter->failCount = failCount;
CompIter->field = field;
CompIter->next = NULL;
return temp;
}
}
void DoubleLinkedShow(DoubleLinked* temp){
if(temp != NULL){
cout << left << setw(13) << "Numara";
cout << left << setw(18) << "İsim";
cout << left << setw(14) << "Soyisim";
cout << left << setw(30) << "Bölüm";
cout << left << setw(30) << "Dersi Alma Sayısı" << endl;
CompIter = temp;
while(CompIter != NULL){
cout << left << setw(13) << CompIter->number;
cout << left << setw(18) << CompIter->name;
cout << left << setw(14) << CompIter->surname;
cout << left << setw(30) << CompIter->field;
cout << left << setw(30) << CompIter->failCount << endl;
CompIter = CompIter->next;
}
}else{
cout << "Öğrenci Bulunamadı. Lütfen Öğrenci Ekleyin." << endl;
}
}
void InsertAll(){
cout << left << setw(13) << "Numara";
cout << left << setw(18) << "İsim";
cout << left << setw(14) << "Soyisim";
cout << left << setw(30) << "Bölüm";
cout << left << setw(30) << "Dersi Alma Sayısı" << endl;
string line;
ifstream compFile ("ComputerStudents.txt");
if (compFile.is_open()){
while ( getline (compFile,line) ){
stringstream ss(line);
vector<string> result;
while( ss.good() )
{
string substr;
getline( ss, substr, '-' );
result.push_back( substr );
}
cout << left << setw(13) << result[0];
cout << left << setw(18) << result[1];
cout << left << setw(14) << result[2];
cout << left << setw(30) << result[3];
cout << left << setw(30) << result[4] << endl;
int no,failCount;
std::istringstream os(result[0]);
os >> no;
std::istringstream os1(result[4]);
os1 >> failCount;
CompStudents = DoubleLinkedInsert(CompStudents,no,result[1],result[2],result[3],failCount);
}
compFile.close();
}
else cout << "Öğrenci Dosyası Bulunamadı.";
}
int DoubleLinkedSize(DoubleLinked* tempSize){
CompIter = tempSize;
int i = 0;
while(CompIter != NULL){
i++;
CompIter = CompIter->next;
}
return i;
}
void DoubleLinkedSwap(int i,int k,DoubleLinked* swapList){
DoubleLinked* temp1 = swapList, *temp2 = swapList;
for(int j = 0; j < i;j++){
temp1 = temp1->next;
}
for(int j = 0; j < k;j++){
temp2 = temp2->next;
}
int number = temp1->number,failCount = temp1->failCount;
string name = temp1->name,surname = temp1->surname,field = temp1->field;
temp1->number = temp2->number;
temp1->name = temp2->name;
temp1->surname = temp2->surname;
temp1->field = temp2->field;
temp1->failCount = temp2->failCount;
temp2->number = number;
temp2->name = name;
temp2->surname = surname;
temp2->field = field;
temp2->failCount = failCount;
}
// To DoubleLinkedHeapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void DoubleLinkedHeapify(DoubleLinked* tempHeap, int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
DoubleLinked* largesti = tempHeap;
for(int k=0;k<largest;k++){
largesti = largesti->next;
}
DoubleLinked* li = tempHeap;
for(int k=0;k<l;k++){
if(li == NULL)
break;
li = li->next;
}
DoubleLinked* ri = NULL;
if(li != NULL) ri = li->next;
// If left child is larger than root
if (l < n && li->number > largesti->number)
largest = l;
// If right child is larger than largest so far
if (r < n && ri->number > largesti->number)
largest = r;
// If largest is not root
if (largest != i)
{
DoubleLinkedSwap(i, largest,tempHeap);
// Recursively heapify the affected sub-tree
DoubleLinkedHeapify(tempHeap, n, largest);
}
}
// main function to do heap sort
void DoubleLinkedHeapSort(DoubleLinked* tempSort, int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
DoubleLinkedHeapify(tempSort, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
DoubleLinkedSwap(0, i,tempSort);
// call max heapify on the reduced heap
DoubleLinkedHeapify(tempSort, i, 0);
}
}
// Driver program
int main()
{
InsertAll();
int n;
n = DoubleLinkedSize(CompStudents);
DoubleLinked * heaped = CompStudents;
DoubleLinkedHeapSort(heaped, n);
cout << "Sorted array is \n";
DoubleLinkedShow(heaped);
}
基本变量是CompStudents,表示“计算机编程专业学生”。我将学生固定在里面,我想按数字变量(从小到大)对该列表进行排序。
例如: 这是列表的打印。数字未排序。
Numara İsim Soyisim Bölüm Dersi Alma Sayısı
1030510111 Ömer Faruk Uslu Bilgisayar Mühendisliği 0
1030516785 Ali Colak Bilgisayar Mühendisliği 0
1030516450 Mehmet Böyük Bilgisayar Mühendisliği 1
1030516467 Fatma Reyhan Sarıkaya Bilgisayar Mühendisliği 1
1030516799 Kemal Aydın Bilgisayar Mühendisliği 3
1030510113 Zekeriya Doğan Bilgisayar Mühendisliği 1
1030516786 Emrah Alkan Bilgisayar Mühendisliği 0
1030516784 Süleyman Özgür Ozarpacı Bilgisayar Mühendisliği 0
1030516787 Mehmet Kaynak Bilgisayar Mühendisliği 2
1030516576 Kaan Soytarıcı Bilgisayar Mühendisliği 0
1030510001 Burcu Ayşen Kaplan Bilgisayar Mühendisliği 0
1030510003 Ahmet Kaplan Bilgisayar Mühendisliği 0
按数字排序,但这是错误的
Numara İsim Soyisim Bölüm Dersi Alma Sayısı
1030510001 Burcu Ayşen Kaplan Bilgisayar Mühendisliği 0
1030516467 Fatma Reyhan Sarıkaya Bilgisayar Mühendisliği 1
1030516787 Mehmet Kaynak Bilgisayar Mühendisliği 2
1030516576 Kaan Soytarıcı Bilgisayar Mühendisliği 0
1030510113 Zekeriya Doğan Bilgisayar Mühendisliği 1
1030510003 Ahmet Kaplan Bilgisayar Mühendisliği 0
1030516784 Süleyman Özgür Ozarpacı Bilgisayar Mühendisliği 0
1030516785 Ali Colak Bilgisayar Mühendisliği 0
1030516799 Kemal Aydın Bilgisayar Mühendisliği 3
1030510111 Ömer Faruk Uslu Bilgisayar Mühendisliği 0
1030516450 Mehmet Böyük Bilgisayar Mühendisliği 1
1030516786 Emrah Alkan Bilgisayar Mühendisliği 0
这是输出。
如何在不影响CompStudents变量的情况下对列表进行排序。谢谢。
答案 0 :(得分:0)
问题在于检查最大值。
if(li != NULL)
if (l < n && li->number > largesti->number){
largest = l;
largesti = li; // This is the what i missed
}
// If right child is larger than largest so far
if(ri != NULL)
if (r < n && ri->number > largesti->number)
largest = r;