Home > Back-end >  Establish a singly linked list the order of the input output order by size
Establish a singly linked list the order of the input output order by size

Time:10-27

Do you have any good method for a beginner, I well understand

CodePudding user response:

https://blog.csdn.net/qq_45861670/article/details/104324788

This might help

CodePudding user response:

#include
#include
#include


Typedef struct ListType {int n; Struct ListType * next; } the List;

//create a single necklace with a head node table
The List * CreateList (int n, int * v) {
if (n<=0) return NULL;
The List * pHead=(List *) malloc (sizeof (List));
The List * p=pHead;
PHead - & gt; N=n;//head node n, used to record the length of the list of
While (n -) {
p-> Next=(List *) malloc (sizeof (List));
p-> Next - & gt; N=* v++;
P=p - & gt; Next;
};
p-> Next=NULL;
Return pHead;
};

Void PrintList (List * pList) {
The List * p=pList;
While (p - & gt; Next) {
Printf (" % d ", p - & gt; Next - & gt; n);
P=p - & gt; Next;
}
printf("\n");
}

Void SortList (List * pList) {
If (NULL==pList) return;//empty
If (pList - & gt; N & lt; 2) return;//only one or is not, fart
Int x=0;
While (x & lt; PList - & gt; N) {
The List * p=pList - & gt; Next;
The List * pNext=p - & gt; Next;
While (p & amp; & p-> Next) {
If (p - & gt; N & gt; PNext - & gt; N) {
Int t=p - & gt; n;
p-> N=pNext - & gt; n;
PNext - & gt; N=t;
}
P=pNext; PNext=pNext - & gt; Next;
}
X++;
}
}
Int main () {
Int init []={3, 8, 4, 7, 6, 2};
The List * pList=CreateList (6, init);
PrintList (pList);
SortList (pList);
PrintList (pList);
return 0;
}

CodePudding user response:

Like ordinary singly linked list, just add a sort sort algorithm
  • Related