Programming Algorithm-code for inserting an ordered double-loop linked list (C)

Source: Internet
Author: User

Programming Algorithm-code for inserting an ordered double-loop linked list (C)
Code for inserting an ordered double-loop linked list (C)

 

 

 

Ordered double-cycle linked listInsert, you need to find the Insert Location, you can use,Two pointers, One in front, one in the back.

Ensure that the preceding values are less than or equal to the inserted values, and the latter values are greater than or equal to the inserted values.

 

Special Cases, Insert at the beginning and end (greater than or less than the entire linked list) or a single node. If the judgment condition is "pointer to the first node", insert directly.

Insert the linked list header.Adjust the linked list header Node.

 

22 lines of code.

 

Code:

 

/* * main.cpp * *  Created on: 2014.9.18 *      Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include 
 
  #include 
  
   using namespace std;struct ListNode {ListNode (int v) {value = v;prev = NULL;next = NULL;}int value;ListNode* prev;ListNode* next;};ListNode* CreateLists() {ListNode* head = new ListNode(0);ListNode* node1 = new ListNode(1);ListNode* node2 = new ListNode(2);ListNode* node4 = new ListNode(4);ListNode* node5 = new ListNode(5);head->prev = node5;head->next = node1;node1->prev = head;node1->next = node2;node2->prev = node1;node2->next = node4;node4->prev = node2;node4->next = node5;node5->prev = node4;node5->next = head;return head;}ListNode* CreateLists2() {ListNode* head = new ListNode(0);head->prev = head;head->next = head;return head;}void Print(ListNode* head) {ListNode* node = head;printf(%d , node->value);node = node->next;while (node != head) {printf(%d , node->value);node = node->next;}printf();}ListNode* InsertNode(ListNode* head, ListNode* node) {if (head == NULL || node == NULL)return NULL;ListNode* prevNode = head;ListNode* nextNode = head->next;while(1) {prevNode = prevNode->next;nextNode = prevNode->next;if ((prevNode->value < node->value && nextNode->value >= node->value)|| (nextNode == head)){prevNode->next = node;node->prev = prevNode;nextNode->prev = node;node->next = nextNode;if (node->value < head->value) {head = node;}break;}}return head;}int main (void){ListNode* head = CreateLists();Print(head);ListNode* node = new ListNode(3);ListNode* newList = InsertNode(head, node);Print(newList);return 0;}
  
 

Output:

 

 

0 1 2 4 5 0 1 2 3 4 5 


 

 

 

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.