刚接触链表的大一新生 想套网上的公式弄冒泡排序 但程序直接崩了 有d a l a o能看看吗
这个是冒泡排序
void Sort(Node* head)
{
Node* p, * q, * t;
t = NULL;
while ((head->next->next) != t)
{
p = head;
q = head->next;
while (q->next != t)
{
if (q->data.num > q->next->data.num)
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
q = p->next;
}
q = q->next;
p = p->next;
}
t = q;
}
}
这个是用到排序的地方
void showAffair()
{
Node* p= head;
Sort(p);
if (p == NULL)
{
cout << "当前记录为空" << endl;
}
while (p !=NULL)
{
cout <<"名称:" << p->data.name << endl;
cout <<"内容:"<< p->data.content << endl;
cout << "类型:";
if (p->data.type == 1)
{
cout << "开会";
}
else if (p->data.type == 2)
{
cout << "上课";
}
else if (p->data.type == 3)
{
cout << "实验室管理";
}
cout << endl;
cout <<"地点:"<< p->data.place << endl;
cout <<"开始时间:"<< p->data.sta << endl;
cout <<"结束时间:"<< p->data.end << endl;
cout << "编号:" << p->data.num << endl;
cout << endl;
p = p->next;
}
system("pause");
system("cls");
}
这个是报错
看着没问题呀,是报错了吗,报错信息发一下
2022.11.1重新修改编辑,供参考:
void Sort(Node* head)
{
Node* p = NULL, * q = NULL, * t;
t = NULL;
while (head->next != t) //while ((head->next->next) != t) 修改
{
p = head;
q = head->next;
while (q->next != t)
{
if (q->data.num > q->next->data.num)
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
q = p->next;
}
q = q->next;
p = p->next;
}
t = q;
}
}