起因
今天利用空余时间在九度做ACM的时候,需要对单链表进行排序,由于单链表不是随机存取结构,所以我不建议用快速排序,因此采用了冒泡排序!
带头节点的尾插法构建单链表
//初始化带头节点的链表
struct lnode *head, *s, *r, *p;
head = malloc(sizeof(struct lnode));
r = head;
for(i = 0; i < n; i ++)
{
scanf("%d", &d);
s = malloc(sizeof(struct lnode));
s -> data = d;
r -> next = s;
r = s;
}
r -> next = NULL;
冒泡排序——单链表
/**
* Description:单链表的冒泡排序
*/
void BubbleSort(struct lnode *head)
{
struct lnode *f, *p, *x, *y;
f = NULL;
//判断是否只有一个元素或者没有元素
if(head -> next == NULL || head -> next -> next == NULL)
{
return;
}
while(f != head->next->next