有頭單鏈表排序

2021-08-07 16:07:36 字數 1770 閱讀 3186

(1)有頭頭插單鏈表排序:

思路:遍歷比較原鍊錶,將最小的數取出插入新的鍊錶(按照尾插的順序插入)

#include #include typedef struct person

per;

per *head_list(per *one, int num) //有頭頭插

void show(per *head) //遍歷列印

while(head->next)

}per *sort(per *head) //排序

if(null == head->next->next)

per *min_pre = null;

per *new_list = null;

per *min = head->next; //因為是「 有頭」,指標指向 head->next

per *tmp = head->next;

per *tail_sort = null;

while(head->next)

tmp = tmp->next;

}if(min == head->next) //若最小的為第乙個

else

if(null == new_list) //按照尾插順序組成新鍊錶

else

}head->next = new_list; //遍歷比較完成,因為是有頭,則要給新組成的鍊錶的前面加個「 頭」

return new_list;

}int main()

show(&head);

printf("*****==after sort*****===\n");

sort(&head);

show(&head);

return 0;

}

(2)有頭尾插單鏈表排序

思路:遍歷比較原鍊錶,將最小的數取出插入新的鍊錶(按照頭插順序插入)

#include #include typedef struct person

per;

per *tail_list(per *one, int num) //尾插

one->next = temp;

return head;

}void show(per *head)

while(head->next)

}void *sort(per *head)

if(null == head->next->next)

per *min = head->next;

per *tmp = head->next;

per *new_list = null;

per *min_pre = null;

while(head->next)

tmp = tmp->next;

}if(min == head->next) //若第乙個就是最小

else

min->next = new_list;

new_list = min;

}head->next = new_list; //因為是有頭鍊錶,則要給新組成的鍊錶加個 「 頭」

return new_list;

}int main()

show(&head);

printf("*****===after sort*****====\n");

sort(&head);

show(&head);

return 0;

}

無頭單鏈表排序

1 無頭頭插單鏈表排序 思路 在原煉表中逐個比較大小,將最小的插入新的鍊錶 插入順序按照尾插的順序 include include typedef struct person per per head list per one,int num 頭插 void show per head 遍歷 whi...

47 有頭結點的單鏈表

typedef int elemtype typedef struct node struct node next headlist 有頭結點的單鏈表 include headlist.h include include include static headlist elemtype val,he...

有頭結點的單鏈表(java實現)

class node node int data class list public void insert int data node.next tmp.next tmp.next node public void delete int data tmp tmp.next public void ...