JavaScript realizes link list insert sort and chain list merge sort _jsp programming

Source: Internet
Author: User
Tags sorts

This article introduces the JavaScript implementation of the list of linked list insertion and list merge sort, the merge sort of the list is to merge the sort of each part, then merges together.

1. Linked list

1.1 Storage representation of a linked list

The storage of a linked list represents a
typedef int elemtype;
typedef struct LNODE
{
  elemtype data;
  struct Lnode *next;
} Lnode, *linklist;

1.2 Basic operations

To create a linked list:


 * * Create a linked list.
 * The parameter num is the length of the linked list, and the function returns the head pointer of the list.
 */
linklist creatlink (int num)
{
  int i, data;
 
  P points to the last node in the current list, Q points to the node to be inserted.
  linklist head = NULL, p = null, q;
 
  for (i = 0; i < num i++)
  {
    scanf ("%d", &data);
    Q = (linklist) malloc (sizeof (Lnode));
    Q->data = data;
    Q->next = NULL;
    if (i = = 0)
    {head
      = q;
    }
    else
    {
      p->next = q;
    }
    p = q;
  }
  return head;
}

Output list:


 * * Output linked list node value.
 * *
int printlink (linklist head)
{
  linklist p;
  for (p = head; p;p = p->next)
  {
    printf ("%-3d", P->data);
  }
  return 0;
}

2. List Insert Sort

Basic idea: Suppose the front n-1 a knot order, insert nth node to the right position of the front node, make these n nodes orderly.

Implementation method:

The first node in the list is removed to become a linked list containing a node (HEAD1), and the remaining nodes are naturally another list (head2), at which point Head1 contains

An ordered linked list of nodes;

The first node in the list head2 is removed and inserted into the appropriate position of the head1, so that the head1 is still orderly, at this time Head1 become an ordered chain containing two nodes;

In turn, remove a node from the list head2 and insert it into the head1 of the list until the linked list is head2 to an empty list. Finally, the linked list is head1 with all nodes, and the nodes are orderly.

To insert a sort code:


 /* * List Insert sort (from small to large).
 * Input: The head pointer of the linked list,
 * Output: Sort the head pointer of the linked list.
 * Implementation method: The original linked list is split into two parts: the chain table 1 still take head as the first pointer, linked list node order. List 2 with head2 as head pointer, chain table node disorder.
 * Insert the nodes in List 2 into the list 1 and keep the list 1 orderly.
 * The last link in table 1 contains all the nodes, and is in order.
 */
Linklist Linkinsertsort (linklist head)
{
  //current points to the node that is currently being inserted.
  linklist head2, Current, p, Q;
 
  if (head = = NULL) return head
    ;
 
  Split the first time.
  head2 = head->next;
  Head->next = NULL;
 
  while (head2)
  {current
    = head2;
    head2 = head2->next;
 
    Find the insertion position, where the insertion position is between node p and Q.
    for (p = NULL, q = head; Q && q->data <= current->data; p = q, q = q->next);
 
    if (q = = head)
    {
      //insert current to the front. Head
      = current;
    }
    else
    {
      p->next = current;
    }
    Current->next = q;
  }
  return head;
}

Complete source code:

/* * List insert sort, from small to large/#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> #define TOTAL 1
0////Chain table length//list storage represents typedef int ELEMTYPE;
  typedef struct LNODE {elemtype data;
struct Lnode *next;

}lnode, *linklist;
linklist creatlink (int num);
Linklist Linkinsertsort (linklist head);

int Printlink (linklist head);
 * * Create a linked list.
 * The parameter num is the length of the linked list, and the function returns the head pointer of the list.

  * * linklist creatlink (int num) {int i, data;
  P points to the last node in the current list, Q points to the node to be inserted.

  Linklist head = NULL, p = null, q;
    for (i = 0; i < num i++) {scanf ("%d", &data);
    Q = (linklist) malloc (sizeof (Lnode));
    Q->data = data;
    Q->next = NULL;
    if (i = = 0) {head = q;
    else {p->next = q;
  } p = q;
return head;
 /* * List Insert sort (from small to large).
 * Input: The head pointer of the linked list, * output: Sort the head pointer of the linked list. * Implementation method: The original linked list is split into two parts: the chain table 1 still take head as the first pointer, linked list node order.
 List 2 with head2 as head pointer, chain table node disorder.
 * Insert the nodes in List 2 into the list 1 and keep the list 1 orderly.
 * The last link in table 1 contains all the nodes, and is in order. * * linklist Linkinsertsort (linklist head) {//current points to whenThe node to insert before.

  Linklist head2, Current, p, Q;

  if (head = = NULL) return head;
  Split the first time.
  Head2 = head->next;

  Head->next = NULL;
    while (head2) {current = Head2;

    Head2 = head2->next;
    Find the insertion position, where the insertion position is between node p and Q.

    for (p = NULL, q = head; Q && q->data <= current->data; p = q, q = q->next);
      if (q = = head) {//insert current to the front.
    head = current;
    else {p->next = current;
  } current->next = q;
return head;
 }/* Output list node value.
  * * int Printlink (linklist head) {linklist p;
  for (p = head; p;p = p->next) {printf ("%-3d", p->data);
return 0;

  int main () {linklist head;
  printf ("Enter total number to create linked list: \ n");
  
  Head = Creatlink (total);
  Head = Linkinsertsort (head);
  printf (after "sorted: \ n");
  Printlink (head);
  Putchar (' \ n ');
return 0;

 }

3. Linked list Merge sort

Basic idea: If the list is empty or contains a node, the linked list is naturally orderly. Otherwise, the list is divided into two parts, each part is sorted separately, and the sorted two lists are merged together.

Merge sort code:


 * * List merge sort (from small to large).
 * Input: The head pointer of the linked list,
 * Output: Sort the head pointer of the linked list.
 * Recursive implementation method: The chain table head is divided into two parts, respectively, the merge sort, and then the two parts after the sorting merge together.
 * Recursive end Condition: The list of recursive sorts is empty or has only one node.
 */
Linklist Linkmergesort (linklist head)
{
  linklist head1, head2;
  if (head = = NULL | | head->next = NULL) return head
    ;
 
  Linksplit (Head, &head1, &head2);
  Head1 = Linkmergesort (head1);
  Head2 = Linkmergesort (head2);
  Head = Linkmerge (Head1, head2);
  return head;
}

The link table partition function is as follows, the basic idea is to use the Slow/fast pointer, the concrete realization method see annotation.

/
 * * Linked list partition function.
 * The linked list head is divided into two parts head1 and Head2, if the chain table length is odd, the more nodes from the first part.
 * Implementation method: First, the pointer slow/fast to the top of the chain,
 * and then the fast pointer to move forward two nodes, while the slow pointer to move forward a node,
 * Circular movement, until the fast pointer to the end of the chain. At the end, the slow points to the chain end of the linked list head1.
 * *
int linksplit (linklist head, linklist *head1, linklist *head2) {linklist slow
  , fast;
 
  if (head = = NULL | | head->next = null)
  {
    *head1 = head;
    *head2 = NULL;
    return 0;
  }
  slow = head;
  Fast = head->next;
  while (fast)
  {
    fast = fast->next;
    if (fast)
    {
      fast = fast->next;
      slow = slow->next;
    }
  }
  *head1 = head;
  *head2 = slow->next;
 
  Note: Be sure to empty the chain end of the list head1.
  slow->next = NULL;
  return 0;
}

There are two methods of recursive implementation and non-recursive implementation of linked list merging functions:

Non-recursive implementations:


 * * Linked list merge.
 * Merge two ordered lists together to make the total list orderly.
 * Input: Linked list head1 and linked list head2
 * Output: Merged linked list
 * Implementation method: The link in the linked list head2 into the head1 of the list in the appropriate position, so that the head1 is still an ordered list.
 */
Linklist Linkmerge (linklist head1, linklist head2)
{
  linklist p, q, t;
 
  if (!HEAD1) return
    head2;
  if (!head2) return
    head1;
 
  The initialization of the cyclic variable, Q points to the current node in the list Head1, and P is the precursor of Q.
  p = NULL;
  Q = head1;
  while (head2)
  {
    //t is the node to be inserted.
    t = head2;
    head2 = head2->next;
    Find the insertion position, where the insertion position is between P and Q.
    for (; q && q->data <= t->data; p = q, q = q->next);
    if (p = = NULL)
      head1 = t;
    else
      p->next = t;
    T->next = q;
    After the node T is inserted between p and Q, the P is pointed back to the precursor of Q.
    p = t;
  }
  return head1;
}

Recursive implementation:

Linklist LinkMerge2 (linklist head1, linklist head2)
{
  linklist result;
 
  if (!HEAD1) return
    head2;
  if (!head2) return
    head1;
 
  if (head1->data <= head2->data)
  {result
    = Head1;
    Result->next = Linkmerge (Head1->next, head2);
  }
  else
  {result
    = head2;
    Result->next = Linkmerge (Head1, Head2->next);
  }
  return result;
}

Complete source code:

* * List merge sort, from small to large. * * #define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> #define TOTAL 10//Chain list length//list storage
Represents a typedef int elemtype;
  typedef struct LNODE {elemtype data;
struct Lnode *next;

}lnode, *linklist;
linklist creatlink (int num);
Linklist Linkmergesort (linklist head);
Linklist Linkmerge (linklist head1, linklist head2);
Linklist LinkMerge2 (linklist head1, linklist head2);
int Linksplit (linklist head, linklist *head1, linklist *head2);

int Printlink (linklist head);
* * Create a linked list.
* The parameter num is the length of the linked list, and the function returns the head pointer of the list.

  * * linklist creatlink (int num) {int i, data;
  P points to the last node in the current list, Q points to the node to be inserted.

  Linklist head = NULL, p = null, q;
    for (i = 0; i < num i++) {scanf ("%d", &data);
    Q = (linklist) malloc (sizeof (Lnode));
    Q->data = data;
    Q->next = NULL;
    if (i = = 0) {head = q;
    else {p->next = q;
  } p = q;
return head;
}/* Output list node value. */int Printlink (linklist head) {linklistP
  for (p = head; p; p = p->next) {printf ("%-3d", p->data);
return 0;

  int main () {linklist head;
  printf ("Enter total number to create linked list: \ n");

  Head = Creatlink (total);
  Head = Linkmergesort (head);
  printf (after "sorted: \ n");
  Printlink (head);
  Putchar (' \ n ');
return 0;
 /* * List merge sort (from small to large).
 * Input: The head pointer of the linked list, * output: Sort the head pointer of the linked list.
 * Recursive implementation method: The chain table head is divided into two parts, respectively, the merge sort, and then the two parts after the sorting merge together.
 * Recursive end Condition: The list of recursive sorts is empty or has only one node.
  * * linklist Linkmergesort (linklist head) {linklist head1, head2;

  if (head = = NULL | | head->next = NULL) return head;
  Linksplit (Head, &head1, &head2);
  Head1 = Linkmergesort (HEAD1);
  Head2 = Linkmergesort (head2);    Head = Linkmerge (Head1, head2);  Non-recursive implementation//head = LinkMerge2 (Head1, head2);
Recursive implementation return head;
 /* * Linked list merge.
 * Merge two ordered lists together to make the total list orderly.
 * Input: Linked list head1 and linked list head2 * Output: Merged linked list * Implementation method: The link in the linked list head2 into the head1 of the list in the appropriate position, so that the head1 is still an ordered list.

  * * linklist Linkmerge (linklist head1, linklist head2) {linklist p, q, t;
  if (!HEAD1) return head2;if (!head2) return head1;
  The initialization of the cyclic variable, Q points to the current node in the list Head1, and P is the precursor of Q.
  p = NULL;
  Q = head1;
    while (head2) {//t is the node to be inserted.
    t = head2;
    Head2 = head2->next;
    Find the insertion position, where the insertion position is between P and Q.
    for (; q && q->data <= t->data; p = q, q = q->next);
    if (p = = NULL) Head1 = t;
    else P->next = t;
    T->next = q;
    After the node T is inserted between p and Q, the P is pointed back to the precursor of Q.
  p = t;
return head1;

  } linklist LinkMerge2 (linklist head1, linklist head2) {linklist result;
  if (!HEAD1) return head2;

  if (!head2) return head1;
    if (Head1->data <= head2->data) {result = Head1;
  Result->next = Linkmerge (Head1->next, head2);
    else {result = Head2;
  Result->next = Linkmerge (Head1, Head2->next);
return result;
 }/* List Split function.
 * The linked list head is divided into two parts head1 and Head2, if the chain table length is odd, the more nodes from the first part. * Implementation method: First, the pointer slow/fast to the top of the chain, * and then the fast pointer to move forward two nodes, while the slow pointer to move forward a node, * circular movement, until the fast pointer to the end of the chain.
 At the end, the slow points to the chain end of the linked list head1. * * int Linksplit (LinKlist Head, linklist *head1, linklist *head2) {linklist slow, fast;
    if (head = = NULL | | head->next = NULL) {*head1 = head;
    *head2 = NULL;
  return 0;
  } slow = head;
  Fast = head->next;
    while (fast) {fast = fast->next;
      if (fast) {fast = fast->next;
    slow = slow->next;
  } *head1 = head;

  *head2 = slow->next;
  Note: Be sure to empty the chain end of the list head1.
  Slow->next = NULL;
return 0;

 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.