Home > Back-end >  Add a judgment to sort is empty or there is only one element can't run.. Is there a big look
Add a judgment to sort is empty or there is only one element can't run.. Is there a big look

Time:09-21

 # include & lt; Stdio. H> 
#include
#include
#include
#include

Typedef struct Node
{
The int data;
Struct Node * next;
} LNode * LinkList;

Void QuickSort2 (LinkList head of LinkList tail) {
If (head==tail)//if the head pointer is null or a linked list is empty, direct return
return ;
//if (head - & gt; Next==tail | | head - & gt; Next - & gt; Next==tail)
//return;
int t;
LinkList p=head - & gt; next;
LinkList small=head;
While (p! Tail)={
If (p - & gt; The data & lt; The head - & gt; Data) {
Small=small - & gt; next;
T=small - & gt; The data;
Small - & gt; Data=https://bbs.csdn.net/topics/p -> data;
P - & gt; Data=https://bbs.csdn.net/topics/t;

}
P=p - & gt; next;
}
T=head - & gt; The data;
The head - & gt; Data=https://bbs.csdn.net/topics/small -> data;
Small - & gt; Data=https://bbs.csdn.net/topics/t;

QuickSort2 (head, small);
QuickSort2 (small - & gt; Next, tail);
}

Void create_list (LinkList * L) {
int i;
int n;
LinkList head, p, q;

* L=(struct Node *) malloc (sizeof (LNode));
P=* L;
The head=NULL;
P - & gt; Data=https://bbs.csdn.net/topics/-1;
P - & gt; Next=NULL;

Printf (" please enter you need to generate node number: ");
The scanf (" % d ", & amp; N);

for(i=0; I & lt; n; I++) {

Q=(struct Node *) malloc (sizeof (LNode));
If (NULL==q) exit (1);
Q - & gt; data=https://bbs.csdn.net/topics/rand () % 100;
Q - & gt; Next=NULL;
{if (NULL==p)
The head=q;
P=the head;
} else {
P - & gt; Next=q;
P=q;

}

}}


Void traverse_list LinkList (L) {
LinkList p=L - & gt; next;

While (NULL!=p) {
Printf (" % d ", p - & gt; The data);
P=p - & gt; next;
}
printf("\n");
return;
}

The main () {
LinkList La=NULL;
Clock_t start and finish;
Double duration;

Create_list (& amp; La);
//printf (" before ordering: \ n ");
Traverse_list (La);
//InitList (& amp; La);
//start=clock ();

QuickSort2 (La, NULL);

//finish=clock ();

//printf (" sorted: \ n ");
Traverse_list (La);

//duration=(double) (finish - start)/CLOCKS_PER_SEC;
//printf (" % f seconds \ n ", duration).

return 0;
}

Complete code all over this, want to change the function annotations is the line

CodePudding user response:

Because is leading the node of the linked list, so certainly not head==null judgments, so but how can change after the change program..

CodePudding user response:

You create the also have a problem? After creation can normal traversal?

CodePudding user response:

Simple - Soft references 2 floor response:
you create also has a problem? After creation can normal traversal?
can normal traversal.
  • Related