c语⾔关于使⽤链表排序(选择排序、冒泡排序)
链表
⽐较两个相邻的元素⼤⼩,每⼀趟会把较⼤(或较⼩)的数放在往后移。链表冒泡排序思想:设置两个指针,⼀个是当前指针,⼀个是尾指针,当前的指针指向头节点,将尾指针赋为空,当当前的指针不等于尾指针是⼀次循环,第⼀次将当前⼀个节点的值与下⼀个节点的值相⽐较,直到下⼀个节点和尾指针相等(即为空),结束本次循环,最后当前的指针赋给尾指针,当前指针重新指向头结点,再两两相⽐较,把最⼤(或最⼩)的数放在最后
1//冒泡排序的主要思想两两相性⽐较,每⽐较⼀次会把⼀轮最⼤或最⼩的数放在最后
2void BubbleSort(struct student *head){
3 struct student *cur,*tail;
4 cur=head;
5 tail=NULL;
6 if(cur==NULL||cur->next==NULL){
7  return;
8 }
9 while(cur!=tail){
10  while(cur->next!=tail){
11  if(cur->score>cur->next->score){
12    int temp=cur->score;
13    cur->score=cur->next->score;
14    cur->next->score=temp;
15    long temp1=cur->num;
16    cur->num=cur->next->num;
17    cur->next->num=temp1;
18  }
19  cur=cur->next;
20  }
21  tail=cur;
22  cur=head;
23 }
24
25}
选择排序:
的思想将当前的元素和后⾯的元素相⽐较,每循环⼀次把最⼤或最⼩的元素赋值给当前的元素。
1//选择排序
2void SelectedSort(struct student *head){
3 struct student *cur,*p=NULL,*q=NULL;
4 cur=head;
5 int score=0,cscore=0;
6 if(cur==NULL||cur->next==NULL){
7  return;
8 }
9 while(cur!=NULL){
10  p=cur->next;
11  q=cur->next;
12  score=cur->score;
13  cscore=score;
14  while(p!=NULL){
15  if(cscore>p->score){
16    cscore=p->score;
17    q=p;
18  }
19  p=p->next;
20  }
21  if(score!=cscore){
22  q->score=score;
23  cur->score=cscore;
24  long temp=cur->num;
25  cur->num=q->num;
26  q->num=temp;
27  }
28  cur=cur->next;
29 }
30}
链表排序的源代码(可测):
1#include <stdio.h>
2#include <malloc.h>
3#define LEN sizeof(struct student)
4
5struct student{
6 long num;
7 int score;
8 struct student *next;
9};
linux命令大全查看日志10
11int main(){
12 struct student *create();
13 void print(struct student *head);
14 void BubbleSort(struct student *head);
15 void SelectedSort(struct student *head);
16 struct student *head;
17 head=create();
18 print(head);
19 //BubbleSort(head);
20 SelectedSort(head);
21 print(head);
22
23}
24int n;
25struct student *create(){
srcset属性
26 n=0;
27 struct student *head,*p1,*p2;
28 p1=p2=(struct student *)malloc(LEN);
29 scanf("%ld,%d",&p1->num,&p1->score);英国硕士distinction难吗
30 head=NULL;
32  n=n+1;
33  if(n==1){forwards怎么读
34  head=p1;
35  }else{
36  p2->next=p1;
37  }
38  p2=p1;
39  p1=(struct student *)malloc(LEN);
40  scanf("%ld,%d",&p1->num,&p1->score);
41 }
42 p2->next=NULL;
43 return(head);
44}
45void print(struct student *head){
46 struct student *p;
47 p=head;
48 printf("%d名学⽣的成绩分别是:\n",n);
49 if(head!=NULL){
50  do{
卡盟文章站源码
51  printf("%ld,%d\n",p->num,p->score);
52  p=p->next;
53  }while(p!=NULL);
54 }else{
55  printf("链表为空");
56 }
57}
58//冒泡排序的主要思想将前后两两相⽐较,每⽐较⼀次会把⼀轮最⼤或最⼩的数放在最后59void BubbleSort(struct student *head){
60 struct student *cur,*tail;
61 cur=head;
62 tail=NULL;
63 if(cur==NULL||cur->next==NULL){
64  return;
65 }
66 while(cur!=tail){
67  while(cur->next!=tail){
68  if(cur->score>cur->next->score){
冒泡排序代码c语言
69    int temp=cur->score;
70    cur->score=cur->next->score;
71    cur->next->score=temp;
72    long temp1=cur->num;
73    cur->num=cur->next->num;
74    cur->next->num=temp1;
75  }
76  cur=cur->next;
77  }
78  tail=cur;
79  cur=head;
80 }
81
82}
83//选择排序
84void SelectedSort(struct student *head){
85 struct student *cur,*p=NULL,*q=NULL;
86 cur=head;
87 int score=0,cscore=0;
88 if(cur==NULL||cur->next==NULL){
89  return;
90 }
91 while(cur!=NULL){
92  p=cur->next;
93  q=cur->next;
94  score=cur->score;
95  cscore=score;
97  if(cscore>p->score){
98    cscore=p->score;
99    q=p;
100  }
101  p=p->next;
102  }
103  if(score!=cscore){ 104  q->score=score; 105  cur->score=cscore; 106  long temp=cur->num; 107  cur->num=q->num; 108  q->num=temp;
109  }
110  cur=cur->next;
111 }
112}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。