Linked list of Redis data types

Source: Internet
Author: User

Implementation of the chain table for Redis data types

The underlying implementation of the Redis list is a doubly linked list, with the source code under SRC adlist.h and adlist.c

Node data structure of linked list

    /*     * 双端链表节点     */    typedefstruct listNode {        // 前置节点        struct listNode *prev;        // 后置节点        struct listNode *next;        // 节点的值        void *value;    } listNode;

Linked list data structure

/ * Double-ended linked list structure */typedef struct List{//Table header nodeListNode *head;//Footer nodeListNode *tail;//Node value copy function    void* (*dup) (void*PTR);//Node value deallocation function    void(* Free)(void*PTR);//Node value comparison function    int(*match) (void*ptr,void*key);the number of nodes included in the// list    unsigned LongLen;}List;
The specific features of the linked list Redis
    1. List key
    2. Publish and subscribe
    3. Slow query
    4. Monitor
Time complexity of some functions
//Returns the number of nodes included in the given list//T = O (1)#define LISTLENGTH (L) ((l)->len)//Returns the table header node for the given list//T = O (1)#define LISTFIRST (L) ((l)->head)//Returns the footer node of the given list//T = O (1)#define LISTLAST (L) ((l)->tail)//Returns the predecessor node of a given node//T = O (1)#define LISTPREVNODE (n) ((n)->prev)//Returns the backend node of a given node//T = O (1)#define LISTNEXTNODE (n) ((n)->next)//Returns the value of the given node//T = O (1)#define LISTNODEVALUE (n) ((n)->value)//Set the value copy function of the linked list L to M//T = O (1)#define LISTSETDUPMETHOD (L,m) ((l)->dup = (m))//Set the value release function of the linked list L to M//T = O (1)#define LISTSETFREEMETHOD (L,m) ((l)->free = (m))//Set the comparison function of the list to M//T = O (1)#define LISTSETMATCHMETHOD (L,m) ((l)->match = (m))//Returns the value of the given list copy function//T = O (1)#define LISTGETDUPMETHOD (L) ((l)->dup)//Returns the value deallocation function for a given list//T = O (1)#define LISTGETFREE (L) ((l)->free)//Returns the value comparison function for a given list//T = O (1)#define LISTGETMATCHMETHOD (L) ((l)->match)
/ * * Create a new linked list * * Create a successful return list, Failure returns NULL. * * T = O (1) */List *Listcreate (void) {structList *List;//Allocate memory    if((List =Zmalloc (sizeof (*List)))== NULL)return NULL;//Initialize Properties    List -Head= List -Tail= NULL;List -Len= 0;List -Dup= NULL;List -Free= NULL;List -Match = NULL;return List;}

Note that the allocation of the malloc function here is self-fulfilling zmalloc

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linked list of Redis data types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.