{
P1, struct people * * p2, * p3;
P3=NULL;
For (p1=head, p2=p1 - & gt; Next; The p2!=NULL; P1=p1 - & gt; Next, the p2=p2 - & gt; Next)
If (STRCMP ((p1 - & gt; Name), (p1 - & gt; Next - & gt; The name) & gt; 0)
{
P3=p;
P1=p2;
The p2=p3;
Printf (" % s ", p1 - & gt; Name);
Printf (" % s ", p2 - & gt; Name);
}
The p2 - & gt; Next=NULL;
}
CodePudding user response:
#include#include
#include
Typedef struct People {
Char name [20].
Char id [20];
Struct People * next;
} People;
People * create (int n) {
If (n<1) return NULL;
int i; Int v.
V=rand () % 1000;
People * pHead=(People *) malloc (sizeof (People));
Sprintf (pHead - & gt; The name, the name "- % 3 d", v);
Sprintf (pHead - & gt; Id, "id - % 3 d", v);
People * p=pHead;
For (I=1; i
V=rand () % 1000;
Sprintf (p - & gt; Next - & gt; The name, "the name - % d", v);
Sprintf (p - & gt; Next - & gt; Id, "id - % 3 d", v);
P=p - & gt; Next;
}
P - & gt; Next=NULL;
Return pHead;
};
Void printList (People * pHead) {
Printf (" \ n -- -- -- -- -- -- -- -- -- People List -- -- -- -- -- -- -- -- -- \ n ");
If (pHead==NULL) return;
People * p=pHead;
While (p) {
Printf (" % s: % s \ n ", p - & gt; The name, p - & gt; Id);
P=p - & gt; Next;
}
}
People * sortList (People * pHead) {
If (NULL==pHead | | pHead - & gt; Next==NULL) return pHead;
People * pTH=(People *) malloc (sizeof (People));
PTH - & gt; Next=pHead;
People * pTail=NULL;
While (pTH - & gt; Next - & gt; Next!={pTail)
People * pPrior=pTH;
People * pCur=pTH - & gt; Next;
People * pNext=pCur - & gt; Next;
Do {
If (STRCMP (pCur - & gt; The name, pNext - & gt; Name) & gt; 0 {
PCur - & gt; Next=pNext - & gt; Next;
PNext - & gt; Next=pCur;
PPrior - & gt; Next=pNext;
People * pT=pNext;
PNext=pCur;
PCur=pT;
}
PNext=pNext - & gt; Next;
PCur=pCur - & gt; Next;
PPrior=pPrior - & gt; Next;
} while (pNext!=pTail);
PTail=pCur;
}
PHead=pTH - & gt; Next;
Free (pTH);
Return pHead;
};
Int main ()
{
Srand (time (NULL));
People * p=the create (10);
PrintList (p);
P=sortList (p);
PrintList (p);
return 0;
}