Single linked list merge sort implementation

Source: Internet
Author: User

The original question is to achieve two sorted single-linked list after merging or sorted, but I looked on the internet a lot of can not be directly implemented. For beginners to give an algorithm is not much use, the following gives the complete code. The main idea is to get the first order. And the general book is the direct start of the situation if...else if ... else nested sort. more complex.

/*key: Two sequential single-linked list of the merger: in fact, this program can achieve any two single-linked list of the combined sort, the idea is * *. Build two linked List 2. Merge two linked list 3. Sort the linked list 4. Print * Key functions: Linkdlist directly connected to two linked lists; Selectsort Select sort for single-linked list*/#define_crt_secure_no_warnings#include"stdio.h"#include"malloc.h"#include"windows.h"//System () is locatedtypedefCharElemtype; typedefstructdnode{//declaring a single-linked table node typeElemtype data;//data field, note that this single-linked table node type is a character type. Is there any way to generalize ?    structDnode *next;//pointer Field}lnode, *linklist;//Define this struct variable struct dnode A or lnode A; A pointer to the struct Dnode *p or Lnode *p or linklist p is indistinguishable//This function is useless for this program, just to correspond with the createdlisttail in the back.linklist Createdlisthead (linklist head) {Elemtype temp;    Linklist p; printf ("Please enter a node value.");    Fflush (stdin); scanf ("%c", &temp);  while(temp!='0')    {        if(('A'<= temp&&temp <='Z') || ('a'<= temp&&temp <='Z') ) {p= (linklist)malloc(sizeof(Lnode)); P->data =temp; P->next = head->Next; Head->next =p; } printf ("Please enter a node value (end of input 0):");        Fflush (stdin); scanf ("%c", &temp); }    returnHead;} Linklist createdlisttail (linklist Tail)//The tail interpolation method to establish the linked list{Elemtype temp;    Linklist S, R; printf ("Please enter a node value.");    Fflush (stdin); scanf ("%c", &temp); R=Tail;  while(temp!='0')    {        if(('A'<= temp&&temp <='Z') || ('a'<= temp&&temp <='Z') ) {s= (linklist)malloc(sizeof(Lnode)); S->data =temp; R->next =s; R=s; } printf ("Please enter a node value (end of input 0):");        Fflush (stdin); scanf ("%c", &temp); } R->next =NULL; returnTail;}voidprintdlist (linklist head) {linklist p; inti =0; P= head->next;//p points to the first node. The node behind the original head of this step will go to P     while(P! =NULL) {i++; printf ("the first%d elements are:", i); printf ("%c\n", p->data); P= p->Next; } printf ("\ n");}voidlinkdlist (linklist S, linklist T) {/*Connect 2 single-linked lists*/linklist Temp=S;  while(Temp->next! =NULL) Temp= temp->Next; Temp->next = t->Next; Printdlist (S);}voidSelectsort (linklist A) {//to sort DescendingElemtype t; Linklist p, Q, S;//q,s Comparison    if((A->next) && (A->next->next))//the list is empty or there is only one node without sorting.{p= a->Next;  while(p->next) {Q= p->Next; S=p;  while(q) {if(Q->data > s->data) {T= q->data; Q->data = s->data; S->data =T; } q= q->Next; }//Endwhile Exchange S node and p node datap = p->Next; }//Endwhile}//endifprintdlist (A);}//EndselectsortvoidMain () {linklist A= (linklist)malloc(sizeof(Lnode)); Linklist B= (linklist)malloc(sizeof(Lnode));    Createdlisttail (A);    Printdlist (A);    Createdlisttail (B);    Printdlist (B);    Linkdlist (A, B);    Selectsort (A); System ("PAUSE");  Free(A);  Free(B);}

Code through VS + Win 7 64; Compile through can run, static analysis failed, main hint memory security, API does not return value.

This program only realizes the function of the topic, it is not perfect at all. The beginner can run and thank goodness.

In addition, the main part of the code should be simplified, but I will not. Please advise your predecessors.

Single linked list merge sort implementation

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.