我有一个具有链表堆栈数据结构的程序,任务是对该结构进行排序并按特定顺序输出。 我已经用“名称”,“发行”,“长度”和“艺术家”元素填充了结构,我需要按“发行日期”对它进行排序并输出排序结果。
我只是想使用气泡排序比较值并以这种方式对它们排序,但是我不知道如何在堆栈中实现它,然后以正确的顺序输出它们。 (或者只是使用push \ pop来代替?)
#define in 5
#define na 25
#define re 5
#define le 5
#define ar 25
using namespace std;
struct KNOMERCY {
char index[in]; // Album's index
char name[na]; // Album's name
char release[re]; // Albums date release
char length[le]; // Album's length
char artist[ar]; // Album's artist
struct KNOMERCY *next;
};
struct KNOMERCY *savetop=NULL;
struct KNOMERCY *top=NULL;
和排序功能
void sortReleasedInput()
{
KNOMERCY *tmp;
tmp = top;
string year = string(tmp->release);
string reld;
int k;
for (savetop = top; savetop != NULL; savetop = savetop->next) {
tmp = string(savetop->release);
for (k = 0; k < 4; k++) {
year = tmp;
}
if (year == reld) {
break;
}
}
for (savetop = top; savetop != NULL; savetop = savetop->next) {
tmp = string(savetop->release);
for (k = 0; k < 4; k++) {
year = tmp;
}
if (year >= reld) {
outputCertainRelease();
cout << "************************************" << endl;
}
}
system("pause");
}
void outputCertainRelease() {
cout << "|ID | " << savetop->index << endl;
cout << "|Name | " << savetop->name << endl;
cout << "|Released | " << savetop->release << endl;
cout << "|Length | " << savetop->length << endl;
cout << "|Artist | " << savetop->artist << endl;;
cout << "\n" << endl; }
从文件加载的结构:
1 LowBudgetButter 1997 46:04 Various
2 TimeBeing 2017 50:56 Cyne
3 TheColdVein 2001 1:14:04 CannibalOx
4 FucnrusherPlus 2002 1:14:04 CompanyFLow
5 Oneirology 2011 50:15 CunninLynguists
6 3rdEyeVision 1998 1:12:15 Hieroglyphics
7 TheBothSidesOfTheBrain 2000 1:13:42 DelTheFunkyHomosapien
8 PleemixVolumeOne 2005 1:06:52 A-Plus
9 StrangeJourneyVolumeThree 2014 53:53 CunninLynguists
10 FullCircle 2003 55:33 Hieroglyphics
我的代码崩溃了,因为出了点问题。我会很高兴获得帮助。