One-step write algorithm (cyclic one-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]

 

 

 

 

In the previous blog, we used to talk about one-way linked list. So what is the difference between the linked list discussed today and the linked list discussed last time? The focus is on this "loop. With a loop, we can start to work from any linked list node, set the root user to any linked list node, and access data from any linked list node. This is the advantage of a loop.

 

So what are the differences between circular one-way linked lists during implementation?

 

1) print linked list data

 

 

Void print_data (const LINK_NODE * pLinkNode)

{

LINK_NODE * pIndex = NULL;

If (NULL = pLinkNode)

Return;

 

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

PIndex = pLinkNode-> next;

While (pLinkNode! = PIndex ){

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

PIndex = pIndex-> next;

}

}

Void print_data (const LINK_NODE * pLinkNode)

{

LINK_NODE * pIndex = NULL;

If (NULL = pLinkNode)

Return;

 

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

PIndex = pLinkNode-> next;

While (pLinkNode! = PIndex ){

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

PIndex = pIndex-> next;

}

} In the past, we found that the end of data printing is to judge whether the pointer is NULL. Here, it is changed because it is a circular linked list. Original condition (NULL! = PLinkNode) is also modified here (pLinkNode! = PIndex ). The find function and count statistical function also need to be modified.

 

 

 

 

2) insert data

 

 

STATUS insert_data (LINK_NODE ** ppLinkNode, int data)

{

LINK_NODE * pNode;

If (NULL = ppLinkNode)

Return FALSE;

 

If (NULL = * ppLinkNode ){

PNode = create_link_node (data );

Assert (NULL! = PNode );

 

PNode-> next = pNode;

* PpLinkNode = pNode;

Return TRUE;

}

 

If (NULL! = Find_data (* ppLinkNode, data ))

Return FALSE;

 

PNode = create_link_node (data );

Assert (NULL! = PNode );

 

PNode-> next = (* ppLinkNode)-> next;

(* PpLinkNode)-> next = pNode;

Return TRUE;

}

STATUS insert_data (LINK_NODE ** ppLinkNode, int data)

{

LINK_NODE * pNode;

If (NULL = ppLinkNode)

Return FALSE;

 

If (NULL = * ppLinkNode ){

PNode = create_link_node (data );

Assert (NULL! = PNode );

 

PNode-> next = pNode;

* PpLinkNode = pNode;

Return TRUE;

}

 

If (NULL! = Find_data (* ppLinkNode, data ))

Return FALSE;

 

PNode = create_link_node (data );

Assert (NULL! = PNode );

 

PNode-> next = (* ppLinkNode)-> next;

(* PpLinkNode)-> next = pNode;

Return TRUE;

} The insert function changes in two places:

 

A) if there are no nodes in the original linked list, the linked list node must point to itself.

 

B) if a linked list node exists, you only need to add a data entry to the current linked list node and modify the pointers in both directions.

 

 

 

 

3) delete data

 

 

STATUS delete_data (LINK_NODE ** ppLinkNode, int data)

{

LINK_NODE * pIndex = NULL;

LINK_NODE * prev = NULL;

If (NULL = ppLinkNode | NULL = * ppLinkNode)

Return FALSE;

 

PIndex = find_data (* ppLinkNode, data );

If (NULL = pIndex)

Return FALSE;

 

If (pIndex = * ppLinkNode ){

If (pIndex = pIndex-> next ){

* PpLinkNode = NULL;

} Else {

Prev = pIndex-> next;

While (pIndex! = Prev-> next)

Prev = prev-> next;

 

Prev-> next = pIndex-> next;

* PpLinkNode = pIndex-> next;

}

} Else {

Prev = pIndex-> next;

While (pIndex! = Prev-> next)

Prev = prev-> next;

Prev-> next = pIndex-> next;

}

 

Free (pIndex );

Return TRUE;

}

STATUS delete_data (LINK_NODE ** ppLinkNode, int data)

{

LINK_NODE * pIndex = NULL;

LINK_NODE * prev = NULL;

If (NULL = ppLinkNode | NULL = * ppLinkNode)

Return FALSE;

 

PIndex = find_data (* ppLinkNode, data );

If (NULL = pIndex)

Return FALSE;

 

If (pIndex = * ppLinkNode ){

If (pIndex = pIndex-> next ){

* PpLinkNode = NULL;

} Else {

Prev = pIndex-> next;

While (pIndex! = Prev-> next)

Prev = prev-> next;

 

Prev-> next = pIndex-> next;

* PpLinkNode = pIndex-> next;

}

} Else {

Prev = pIndex-> next;

While (pIndex! = Prev-> next)

Prev = prev-> next;

Prev-> next = pIndex-> next;

}

 

Free (pIndex );

Return TRUE;

} Like adding data, deleting data must also be changed in two aspects:

 

A) if there is only one data left in the current linked list node, it must be set to NULL after deletion.

 

 

B) When deleting data, you first need the previous data of the current data. At this time, you can traverse the deleted data.

 

C) When deleting a table, you need to check whether the deleted data is the head node data of the 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.