Two-way linked list)

Source: Internet
Author: User
I. Introduction to two-way linked list as a common data structure, two-way linked list provides a detailed description in Yan Weimin's Data Structure book. Each data node of the two-way linked list has two pointers, it points to the successor and the precursor nodes respectively. Therefore, you can easily access any node in the two-way linked list. Ii. Two-way linked list data knot in Redis

I. Introduction to two-way linked list as a common data structure, two-way linked list provides a detailed description in Yan Weimin's Data Structure book. Each data node of the two-way linked list has two pointers, it points to the successor and the precursor nodes respectively. Therefore, you can easily access any node in the two-way linked list. Ii. Two-way linked list data knot in Redis

1. Introduction to two-way linked list

As a common data structure, the two-way linked list provides a detailed description in the Yan Weimin Data Structure book. Each data node of the two-way linked list has two pointers pointing to the successor and the front node respectively, therefore, you can easily access any node in the two-way linked list, including its predecessor and successor nodes.

Ii. Two-way linked list data structure and related macro definitions in Redis
Typedef struct listNode {struct listNode * prev; // The first pointer struct listNode * next; // The value of the subsequent pointer void * value; // The value of the node} listNode; typedef struct listIter {// listNode * next; int direction; // traversal direction} listIter; typedef struct list {// listNode * head; // listNode * tail in the linked list header; // void * (* dup) (void * ptr) at the end of the linked list; // copy function pointer void (* free) (void * ptr ); // release the memory function pointer int (* match) (void * ptr, void * key); // compare the function pointer unsigned long len; // chain table length} list;

Macro name

Function

ListLength

Returns the length of a linked list.

ListFirst

Obtain the first pointer of a linked list

ListLast

Get the end pointer of the linked list

ListPrevNode

Obtains the front-end node pointer of the current node.

ListNextNode

Obtains the successor node pointer of the current node.

ListNodeValue

Obtains the value stored by the current node.

ListSetDupMethod

Set the replication function of the linked list node value

ListSetFreeMethod

Sets the memory release function for the linked list node value.

ListSetMatchMethod

Set the comparison function of the linked list node value

ListGetDupMethod

Returns the value of a linked list node.

ListGetFree

Returns the memory release function of the linked list node value.

ListGetMatchMethod

Get the comparison function of the linked list node value

Iii. Introduction to the two-way linked list API in Redis

Name

Function

Complexity

ListCreate

Create a new two-way linked list

O (1)

ListRelease

Releases a two-way linked list and node memory.

O (N)

ListAddNodeHead

Add a node to the table header of the linked list

O (1)

ListAddNodeTail

Add a node to the end of the linked table

O (1)

ListInsertNode

Add a node after or before a given Node

O (1)

ListDelNode

Delete a given Node

O (1)

ListGetIterator

An iterator for generating a two-way linked list

O (1)

ListReleaseIterator

Release an iterator for a two-way linked list

O (1)

ListNext

Get the next node through the iterator

O (1)

ListDup

Create a copy of a given linked list

O (N)

ListSearchKey

Search nodes with the same value as the given key

O (N)

ListIndex

Returns the corresponding node based on the given index value.

O (N)

ListRewind

Reinitialize the iterator, starting from the beginning to the end of the iteration

O (1)

ListRewindTail

Reinitialize the iterator from the end to the header

O (1)

ListRotate

Remove the End Node of the linked list and insert it to the header.

O (1)

Iv. Redis two-way linked list Performance Analysis

The two-way linked list in Redis may be the simplest and easiest data structure to implement in Redis. It is very simple and easy to say for APIs. The following is a brief analysis of the performance of the two-way linked list.

ListNode has prev and next pointers, so the iterator can easily traverse the linked list from start to end or from end to header;

List has header pointers and tail as pointers. the time complexity for inserting nodes in the head or tail of the linked list is O (1 ), efficient operations of some commands in Redis;

The list contains the len field that saves the length of the linked list, so that the time complexity of calculating the length of the linked list is O (1 ).

V. Summary

Two-way linked list has two main functions: As one of the underlying implementation methods of the Redis list data type; As a general data structure, it can be used by other functional modules.

The implementation of two-way linked list is simple. Redis transformed the two-way linked list, added fields for saving the node length, and implemented its own iterative pointer, making some data operations easy.

Finally, I would like to thank Huang jianhong (huangz1990) for its Redis design and implementation and other comments on the Redis2.6 source code for my help in studying the Redis2.8 source code.

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.