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-28

This how do ah, bosses consult
I am of the new

CodePudding user response:

Build list,
Head method to insert data,
Bubble sort,

CodePudding user response:

Simpler way then the other to build a list, first search the original data in the list, the best data and location are recorded, so you just need to turn to find the original list of nodes, a copy to the new list, of course with the data directly by size in order to create a new list faster, is can't do that

Normal operation is directly modifying the original list, sorting, and array is the same, you need extra implementation is nodes exchange, data search

CodePudding user response:

Study a few days didn't also
Do you have any other method

CodePudding user response:

Just bubble
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>


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 - & gt; Next=(List *) malloc (sizeof (List));
P - & gt; Next - & gt; N=* v++;
P=p - & gt; next;
};
P - & gt; 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 - & gt; Next) {
If (p - & gt; N & gt; PNext - & gt; N) {
Int t=p - & gt; n;
P - & gt; 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;
}
  • Related