一、单向链表想实现正常的删除都需要获取前一个结点的地址,还不如直接使用双向链表,所以单向链表的应用场景非常少,本例是实现了一个增加和删除数字后仍然能从小到大排序的功能。
二、单向链表的查询效率极低,不像排序后的数组可以使用二分法来加快查询速度,只能从头开始遍历,要想快速查询,只能结合hash来实现了
三、代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _ULL unsigned long long
#define CNNUM 10000
/*definition of single link:each node can only visit it's next node, and visit it's pre node,the last link point to NULL*/
/*use scene:sort number. get the minnest number from R[0]~R[n-1] and swap with R[0]; compare with other sort method*/
/*use scene:all Chinese ID number and name information*/
//Link is a struct which has a ull type and a Link * pointer type; mylink is a new name of struct Link by use of typedef
typedef struct Link
{
_ULL value;
struct Link *next;
}mylink;
/*
quantity: init a mylink which has quantity number; add by tail;
https://blog.csdn.net/qq_52607834/article/details/115922172
*/
mylink* init_mylink()
{
_ULL i = 0;
mylink* head_node = (mylink*)malloc(sizeof(mylink)); //init a head node
head_node->value = -1; //this value is not valid value
printf("init finish...\n");
return head_node;
}
void free_mylink(mylink* head_node)
{
int i = 0;
mylink* p_cursor_node = head_node;
//mylink* new_head_node = NULL;
while(NULL != p_cursor_node)
{
head_node = head_node->next;
free(p_cursor_node);
//printf("free %dth inode success!\n", i++);
p_cursor_node = head_node;
}
printf("free finish...\n");
}
void print_mylink(mylink* head_node)
{
int i = 0;
if(NULL == head_node->next)
{
printf("empty link!\n");
return;