先按正常流程建立一个链表,再按照其某一个成员值进行冒泡排序(排序过程的交换,只交换链表指针以外的成员值)。
演示代码如下:(演示代码链表20个节点,成员值为随机值)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct slist
{
int a
struct slist *next
}SLIST
SLIST *init()//生成20个节点成员为随机数的链表
void showList(SLIST *slHead)//打印链表
void px(SLIST *slHead,int flag)//float=1:降序。=2升序
int main()
{
SLIST *slHead=NULL
slHead=init()
printf("排序前:\n")
showList(slHead)
printf("\n降序排序后:\n")
px(slHead,1)
showList(slHead)
printf("\n升序排序后:\n")
px(slHead,2)
showList(slHead)
return 0
}
void px(SLIST *slHead,int flag)//flag=1:降序。=2升序
{
SLIST *sl0=slHead->next,*sl1=NULL,slSave,*pSave=NULL
while(sl0)
{
sl1=sl0->next
while(sl1)
{
if((flag==1 &&sl0->a<sl1->a)||(flag==2 &&sl0->a>sl1->a))
{
slSave=*sl0
*sl0=*sl1
sl0->next=slSave.next
pSave=sl1->next
*sl1=slSave
sl1->next=pSave
}
sl1=sl1->next
}
sl0=sl0->next
}
}
void showList(SLIST *slHead)
{
int i=0
while(slHead->next)
{
printf("节点%d成员值:%d\n",++i,slHead->next->a)
slHead=slHead->next
}
printf("\n")
}
SLIST *init()
{
int num,cnt=20
static SLIST head
SLIST *slHead=&head,*slTail=NULL,*slNew=NULL
slHead->next=NULL
srand(time(NULL))
while(cnt--)
{
num=rand()%100
slNew=(SLIST *)malloc(sizeof(SLIST))
if(!slNew)return NULL
slNew->a=num
slNew->next=NULL
if(!slHead->next)
slHead->next=slNew
else
slTail->next=slNew
slTail=slNew
}
return slHead
}
不多说,直接看代码:
#include <stdio.h>#include <conio.h>
#include <stdlib.h>
#include <math.h>
typedef struct yinshu{
unsigned int yz
struct yinshu *next
}YSNode,*YinShu //定义链表节点类型YSNode,和指针YinShu
void InsertNode(unsigned int n, YinShu &L){
//在L链表的第一个节点之后插入一个节点,值为n
YinShu p
p=(YinShu)malloc(sizeof(YSNode)) //分配节点空间
p->yz=n
p->next=L->next
L->next=p
}
void YinShuFenJie(unsigned int n,YinShu &L){
//对n进行质因数分解,并存在链表L中
unsigned int j,k
k=(unsigned int)sqrt(n)
for(j=2j<=k++j){
if(n%j==0){ //遇到一个质因数j
InsertNode(j,L)
n/=j
k=(unsigned int)sqrt(n)
--j
}
}
InsertNode(n,L) //插入剩下的质因数n
}
int main(){
unsigned int n
YinShu L,p,q
scanf("%d",&n)
L=(YinShu)malloc(sizeof(YSNode))
L->yz=n
L->next=NULL//第一个节点存放n的值
YinShuFenJie(n,L)
p=L->next q=p->next
printf("%u=%u",L->yz,p->yz)
free(L) free(p) //释放第一、二个节点
while(q){
p=q
printf("*%u",p->yz)
q=p->next
free(p)
}
printf("\nFinished.\n")
getch()
return 0
}
希望能帮到你!
#include <stdio.h>#include <stdlib.h>
#include <time.h>
struct _Node {
int data
struct _Node *next
}
typedef struct _Node Node
// 交换两个结点的数据
void SwapNodeData(Node *p1, Node *p2) {
int temp = p1->data
p1->data = p2->data
p2->data= temp
}
// 冒泡排序对链表进行排序
void BubbleSort(Node *head) {
Node *pTemp
int maxIdx, idx
// 计算链表长度
maxIdx = 0
for (pTemp = headpTemp != NULLpTemp = pTemp->next)
++maxIdx
idx = 0
while (idx <maxIdx-1) {
for (pTemp = headidx <maxIdx-1pTemp = pTemp->next, ++idx) {
if (pTemp->data >pTemp->next->data)
SwapNodeData(pTemp, pTemp->next)
}
idx = 0
--maxIdx
}
}
int main(void)
{
Node *head = NULL, *temp = NULL, *p = NULL
int i
srand((unsigned int)time(NULL))
// 产生随机数链表
head = (Node *)malloc(sizeof(Node))
head->data = rand() % 40
p = head
for (i = 1i <20++i) {
temp = (Node *)malloc(sizeof(Node))
temp->data = rand() % 40
p->next = temp
p = p->next
}
p->next = NULL
// 输出随机数链表
for (p = headp != NULLp = p->next)
printf("%d ", p->data)
printf("\n")
// 对链表排序
BubbleSort(head)
// 输出以排序的链表
for (p = headp != NULLp = p->next)
printf("%d ", p->data)
printf("\n")
// 释放资源
for (p = head->nextp != NULLp = p->next) {
free(head)
head = p
}
free(head)
head = NULL
getchar()
return 0
}