单向链表(非循环)的增、删、查、释放、写入文件

一、单向链表想实现正常的删除都需要获取前一个结点的地址,还不如直接使用双向链表,所以单向链表的应用场景非常少,本例是实现了一个增加和删除数字后仍然能从小到大排序的功能。

二、单向链表的查询效率极低,不像排序后的数组可以使用二分法来加快查询速度,只能从头开始遍历,要想快速查询,只能结合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;
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单向链表是由若干个节点组成的,每个节点都包含一个数据域和一个指向下一个节点的指针域。在单向链表中,只能从头节点开始遍历链表,每个节点只能访问其后继节点。下面是用单向链表实现代码实现: ```python # 定义单向链表节点类 class ListNode: def __init__(self, val): self.val = val self.next = None # 定义单向链表类 class LinkedList: def __init__(self): self.head = None # 添加节点 def add_node(self, val): new_node = ListNode(val) if not self.head: self.head = new_node else: curr = self.head while curr.next: curr = curr.next curr.next = new_node # 除节点 def delete_node(self, val): if not self.head: return if self.head.val == val: self.head = self.head.next else: curr = self.head while curr.next: if curr.next.val == val: curr.next = curr.next.next return curr = curr.next # 修改节点 def update_node(self, old_val, new_val): curr = self.head while curr: if curr.val == old_val: curr.val = new_val return curr = curr.next # 找节点 def search_node(self, val): curr = self.head while curr: if curr.val == val: return True curr = curr.next return False ``` 在上面的代码中,我们定义了一个单向链表节点类 ListNode ,包含一个数据域 val 和一个指向下一个节点的指针域 next 。然后定义了单向链表类 LinkedList ,包含一个头节点 head 。在 LinkedList 中,我们实现了 add_node() 方法用于添加节点,delete_node() 方法用于除节点,update_node() 方法用于修改节点,search_node() 方法用于找节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值