One-step write algorithm (two-way linked list)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

The previous blog introduced one-way linked list. The two-way linked list we introduced today, as its name implies, is that the data itself has two-way pointers on the left and right. Compared with a one-way linked list, a two-way linked list has the following features:

 

(1) Two-way pointer in the Data Structure

 

(2) when inserting data, consider operations in the front and back directions.

 

(3) Similarly, operations that delete data also need to be performed in the frontend and backend directions.

 

So what should a non-circular two-way linked list be like? We can try it by ourselves:

 

(1) define the basic structure of a two-way linked list

 

 

Typedef struct _ DOUBLE_LINK_NODE

{

Int data;

Struct _ DOUBLE_LINK_NODE * prev;

Struct _ DOUBLE_LINK_NODE * next;

} DOUBLE_LINK_NODE;

Typedef struct _ DOUBLE_LINK_NODE

{

Int data;

Struct _ DOUBLE_LINK_NODE * prev;

Struct _ DOUBLE_LINK_NODE * next;

} DOUBLE_LINK_NODE; (2) create a bidirectional linked list node DOUBLE_LINK_NODE * create_double_link_node (int value)

{

DOUBLE_LINK_NODE * pDLinkNode = NULL;

PDLinkNode = (DOUBLE_LINK_NODE *) malloc (sizeof (DOUBLE_LINK_NODE ));

Assert (NULL! = PDLinkNode );

 

Memset (pDLinkNode, 0, sizeof (DOUBLE_LINK_NODE ));

PDLinkNode-> data = value;

Return pDLinkNode;

}

DOUBLE_LINK_NODE * create_double_link_node (int value)

{

DOUBLE_LINK_NODE * pDLinkNode = NULL;

PDLinkNode = (DOUBLE_LINK_NODE *) malloc (sizeof (DOUBLE_LINK_NODE ));

Assert (NULL! = PDLinkNode );

 

Memset (pDLinkNode, 0, sizeof (DOUBLE_LINK_NODE ));

PDLinkNode-> data = value;

Return pDLinkNode;

} (3) deleting a two-way linked list

 

 

Void delete_all_double_link_node (DOUBLE_LINK_NODE ** pDLinkNode)

{

DOUBLE_LINK_NODE * pNode;

If (NULL = * pDLinkNode)

Return;

 

PNode = * pDLinkNode;

* PDLinkNode = pNode-> next;

Free (pNode );

Delete_all_double_link_node (pDLinkNode );

}

Void delete_all_double_link_node (DOUBLE_LINK_NODE ** pDLinkNode)

{

DOUBLE_LINK_NODE * pNode;

If (NULL = * pDLinkNode)

Return;

 

PNode = * pDLinkNode;

* PDLinkNode = pNode-> next;

Free (pNode );

Delete_all_double_link_node (pDLinkNode );

}

(4) Search for data in a two-way linked list

 

 

DOUBLE_LINK_NODE * find_data_in_double_link (const DOUBLE_LINK_NODE * pDLinkNode, int data)

{

DOUBLE_LINK_NODE * pNode = NULL;

If (NULL = pDLinkNode)

Return NULL;

 

PNode = (DOUBLE_LINK_NODE *) pDLinkNode;

While (NULL! = PNode ){

If (data = pNode-> data)

Return pNode;

PNode = pNode-> next;

}

Return NULL;

}

DOUBLE_LINK_NODE * find_data_in_double_link (const DOUBLE_LINK_NODE * pDLinkNode, int data)

{

DOUBLE_LINK_NODE * pNode = NULL;

If (NULL = pDLinkNode)

Return NULL;

 

PNode = (DOUBLE_LINK_NODE *) pDLinkNode;

While (NULL! = PNode ){

If (data = pNode-> data)

Return pNode;

PNode = pNode-> next;

}

Return NULL;

} (5) insert data into a two-way linked list

 

 

STATUS insert_data_into_double_link (DOUBLE_LINK_NODE ** ppDLinkNode, int data)

{

DOUBLE_LINK_NODE * pNode;

DOUBLE_LINK_NODE * pIndex;

 

If (NULL = ppDLinkNode)

Return FALSE;

 

If (NULL = * ppDLinkNode ){

PNode = create_double_link_node (data );

Assert (NULL! = PNode );

* PpDLinkNode = pNode;

(* PpDLinkNode)-> prev = (* ppDLinkNode)-> next = NULL;

Return TRUE;

}

 

If (NULL! = Find_data_in_double_link (* ppDLinkNode, data ))

Return FALSE;

 

PNode = create_double_link_node (data );

Assert (NULL! = PNode );

 

PIndex = * ppDLinkNode;

While (NULL! = PIndex-> next)

PIndex = pIndex-> next;

 

PNode-> prev = pIndex;

PNode-> next = pIndex-> next;

PIndex-> next = pNode;

Return TRUE;

}

STATUS insert_data_into_double_link (DOUBLE_LINK_NODE ** ppDLinkNode, int data)

{

DOUBLE_LINK_NODE * pNode;

DOUBLE_LINK_NODE * pIndex;

 

If (NULL = ppDLinkNode)

Return FALSE;

 

If (NULL = * ppDLinkNode ){

PNode = create_double_link_node (data );

Assert (NULL! = PNode );

* PpDLinkNode = pNode;

(* PpDLinkNode)-> prev = (* ppDLinkNode)-> next = NULL;

Return TRUE;

}

 

If (NULL! = Find_data_in_double_link (* ppDLinkNode, data ))

Return FALSE;

 

PNode = create_double_link_node (data );

Assert (NULL! = PNode );

 

PIndex = * ppDLinkNode;

While (NULL! = PIndex-> next)

PIndex = pIndex-> next;

 

PNode-> prev = pIndex;

PNode-> next = pIndex-> next;

PIndex-> next = pNode;

Return TRUE;

} (6) delete data from a two-way linked list

 

 

STATUS delete_data_from_double_link (DOUBLE_LINK_NODE ** ppDLinkNode, int data)

{

DOUBLE_LINK_NODE * pNode;

If (NULL = ppDLinkNode | NULL = * ppDLinkNode)

Return FALSE;

 

PNode = find_data_in_double_link (* ppDLinkNode, data );

If (NULL = pNode)

Return FALSE;

 

If (pNode = * ppDLinkNode ){

If (NULL = (* ppDLinkNode)-> next ){

* PpDLinkNode = NULL;

} Else {

* PpDLinkNode = pNode-> next;

(* PpDLinkNode)-> prev = NULL;

}

 

} Else {

If (pNode-> next)

PNode-> next-> prev = pNode-> prev;

PNode-> prev-> next = pNode-> next;

}

 

Free (pNode );

Return TRUE;

}

STATUS delete_data_from_double_link (DOUBLE_LINK_NODE ** ppDLinkNode, int data)

{

DOUBLE_LINK_NODE * pNode;

If (NULL = ppDLinkNode | NULL = * ppDLinkNode)

Return FALSE;

 

PNode = find_data_in_double_link (* ppDLinkNode, data );

If (NULL = pNode)

Return FALSE;

 

If (pNode = * ppDLinkNode ){

If (NULL = (* ppDLinkNode)-> next ){

* PpDLinkNode = NULL;

} Else {

* PpDLinkNode = pNode-> next;

(* PpDLinkNode)-> prev = NULL;

}

 

} Else {

If (pNode-> next)

PNode-> next-> prev = pNode-> prev;

PNode-> prev-> next = pNode-> next;

}

 

Free (pNode );

Return TRUE;

} (7) count the number of data in a two-way linked list

 

 

Int count_number_in_double_link (const DOUBLE_LINK_NODE * pDLinkNode)

{

Int count = 0;

DOUBLE_LINK_NODE * pNode = (DOUBLE_LINK_NODE *) pDLinkNode;

 

While (NULL! = PNode ){

Count ++;

PNode = pNode-> next;

}

Return count;

}

Int count_number_in_double_link (const DOUBLE_LINK_NODE * pDLinkNode)

{

Int count = 0;

DOUBLE_LINK_NODE * pNode = (DOUBLE_LINK_NODE *) pDLinkNode;

 

While (NULL! = PNode ){

Count ++;

PNode = pNode-> next;

}

Return count;

} (8) print data in a two-way linked list

 

Void print_double_link_node (const DOUBLE_LINK_NODE * pDLinkNode)

{

DOUBLE_LINK_NODE * pNode = (DOUBLE_LINK_NODE *) pDLinkNode;

 

While (NULL! = PNode ){

Printf ("% d \ n", pNode-> data );

PNode = pNode-> next;

}

}

Void print_double_link_node (const DOUBLE_LINK_NODE * pDLinkNode)

{

DOUBLE_LINK_NODE * pNode = (DOUBLE_LINK_NODE *) pDLinkNode;

 

While (NULL! = PNode ){

Printf ("% d \ n", pNode-> data );

PNode = pNode-> next;

}

} Note:

 

The two-way linked list discussed today is non-circular. You can consider how to write it if it is changed to a circular two-way linked list? What should I do if it is an ordered circular two-way linked list?

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.