Linklist merge sort (Microsoft and other interview one hundred questions-linked list sorting) Code

Source: Internet
Author: User

Because you writeProgramThis problem has been fixed, so I wrote a handwriting + machine test. Although I was very careful, I still wrote a Bug During the machine test, therefore, sending this post makes your memory persistent.

The problem is as follows:

1. Write a method for sorting linked listsAlgorithm. Why do you choose to use this method?

CodeAs follows:

# Include <stdio. h> # include <stdlib. h> struct linknode {int data; linknode * pnext; linknode (INT _ DATA) {DATA = _ data; pnext = NULL ;}}; linknode * mergelink (linknode * pleftlink, linknode * prightlink) {linknode * pH = NULL, * P = NULL; while (pleftlink & prightlink) {If (pleftlink-> data <prightlink-> data) {If (pH = NULL) {pH = P = pleftlink; pleftlink = pleftlink-> pnext;} else {P-> pnext = pleftlink; P = p-> pnext; pleftl Ink = pleftlink-> pnext;} else {If (pH = NULL) {pH = P = prightlink; prightlink = prightlink-> pnext ;} else {P-> pnext = prightlink; P = p-> pnext; prightlink = prightlink-> pnext ;}} p-> pnext = (pleftlink = NULL )? Prightlink: pleftlink; return pH;} linknode * mergesort (linknode * phead, int Len) {If (LEN = 1) {phead-> pnext = NULL; return phead ;} // when The bug is written here. I first run mergesort (phead, Len/2), and then use the phead to request prightstart. Linknode * prightstart = phead; For (INT I = 0; I <Len/2; I ++) {prightstart = prightstart-> pnext;} linknode * plefthalf = mergesort (phead, len/2); linknode * prighthalf = mergesort (prightstart, len-len/2); linknode * pmergedlink = mergelink (plefthalf, prighthalf); Return pmergedlink ;} void printall (linknode * phead) {int I = 1; while (phead) {printf ("% d", phead-> data); phead = phead-> pnext; if (I ++ % 20 = 0) {printf ("\ n") ;}} int main () {linknode * phead = NULL; linknode * pcurrent = NULL; int flags [300] = {0}; For (INT I = 0; I <10;) {int num = rand () % 300; If (flags [num]) {continue;} else {flags [I] = 1; I ++; linknode * pnewnode = new linknode (Num); If (phead = NULL) {phead = pcurrent = pnewnode;} else {pcurrent-> pnext = pnewnode; pcurrent = pcurrent-> pnext ;}} printall (phead ); printf ("\ n"); phead = mergesort (phead, 10); printall (phead); Return 0 ;}

 

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.