最近学了单链表,遇到了一些问题 ,和大家分享一下!
首先看一下带头指针的单链表示意图:
从中可以看到链表的每一个节点(除了尾指针)都有一个指针指向下一个节点(头指针只有只保存了该链表的地址),尾指针指向空。
所以我们要对链表中的某个节点进行操作的话,基本上要使用到该节点的前驱节点和后驱节点。(节点2的前驱节点是节点1,节点2的后驱节点是节点3;)
单链表的排序关键在交换,交换有两种方式 一种是节点值的交换,另一种是节点的交换。
先讲第一种节点值交换法:
1 #include <stdio.h> 2 #include <stdlib.h> 3 //定义student类型变量 包括了学号、分数 4 typedef struct student { 5 int number; 6 int score; 7 struct student *pnext; 8 }student; 9 10 student *create(int n) { 11 student *phead, *pend,*pnew=null; 12 int i; 13 phead=pend= (student*)malloc(sizeof(student)); 14 for (i = 0; i < n; i++) 15 { 16 pnew = (student*)malloc(sizeof(student)); 17 scanf("%d", &pnew->number); 18 scanf("%d", &pnew->score); 19 pnew->pnext = null; 20 pend->pnext = pnew; 21 pend = pnew; 22 23 } 24 return phead; 25 } 26 //输出链表 27 void print(student *p1) { 28 while (p1->pnext != null) { 29 p1 = p1->pnext; 30 printf("%d %dn", p1->number, p1->score); 31 } 32 } 33 //冒泡排序 节点值交换法 34 void sort(student *head) { 35 student *p, *q; 36 for (p = head->pnext; p != null; p = p->pnext) 37 for (q = p->pnext; q != null; q = q->pnext) 38 if (p->number > q->number)//根据学号从小到大排序 39 { 40 int t1 = p->number; p->number = q->number; q->number = t1; 41 int t2 = p->score; p->score = q->score; q->score = t2; 42 } 43 44 } 45 46 int main() { 47 int n; 48 student *p; 49 scanf("%d", &n); 50 p= create(n); 51 sort(p); 52 print(p); 53 }
根据算法不难看出节点值交换法适用于节点中没有字符串变量以及变量成员较少的情况下,但是实际问题往往是复杂的。
比如节点中增加学生的姓名,那么想通过交换节点值的方法是比较复杂的(之后会发博客)。所以我就想能不能想通过交换任意两个节点的位置排序呢?
之后写下了如下代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 //定义student类型变量 包括学号、姓名、分数 5 typedef struct student { 6 unsigned number; 7 char name[50]; 8 unsigned score; 9 struct student *pnext; 10 }student; 11 //寻找前驱节点 12 student *find_before(student* phead, student* p) 13 { 14 if (!p) return null; 15 student *pbefore = phead; 16 while (pbefore) 17 { 18 if (pbefore->pnext == p) 19 return pbefore; 20 pbefore = pbefore->pnext; 21 } 22 return null; 23 } 24 //冒泡排序 按照分数高低排序 交换任意的满足条件的节点 25 //head 为链表的头地址 26 void sort(student *head) { 27 student *p, *pbefore, *q, *qbefore, *t1, *t2; 28 student *phead = head; 29 for (p = head->pnext; p != null; p = p->pnext) 30 { 31 pbefore = find_before(phead, p); 32 for (q = p->pnext; q != null; q = q->pnext) 33 { 34 qbefore = find_before(phead, q); 35 //当p q节点不相邻 36 if (p->score < q->score && p->pnext != q) 37 { 38 t1 = pbefore->pnext; //保存p节点地址 39 pbefore->pnext = qbefore->pnext; //把q节点赋值给p节点 40 qbefore->pnext = t1; //p节点赋值给q节点 41 t2 = q->pnext; //保存q的后驱节点 42 q->pnext = p->pnext; //把p的后驱节点赋值给q的后驱节点 43 p->pnext = t2; //把q的后驱节点赋值给p的后驱节点 44 } 45 //当p q节点相邻 46 else if (p->score < q->score && p->pnext == q) 47 { 48 t1 = pbefore->pnext; 49 pbefore->pnext = p->pnext; 50 p->pnext = t1; 51 t1 = q->pnext; 52 } 53 54 } 55 56 } 57 58 }
上面的代码看似没有问题 但却忽略了一个问题–交换之后的p,q 将for语句的循环破坏了! 我们来看一下p q不相邻情况下的交换图:
如果按照for语句中执行语句q=q->pnext ,q更新后为节点3,而不是我们想要的节点5。所以这种交换任意节点排序的方法是错误的
之后我又换了个思路:(假设按分数从高到低为链表节点排序)在链表a中找到最高分节点q,然后将q节点接在b链表(空链表)的头指针后面,
再通过q的前后驱动节点将q从a链表中剔除,接下来又在a链表中寻找最高分节点q’,如此循环 直到a链表为空。
代码如下:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 //创建student类型变量 包括学号、姓名、成绩 5 typedef struct student { 6 int number; 7 char name[50]; 8 int score; 9 struct student *pnext; 10 }student; 11 //创建链表 12 student *create(int n) { 13 student *phead, *pend, *pnew = null; 14 int i; char str[100][50]; 15 phead = pend = (student*)malloc(sizeof(student)); 16 for (i = 0; i < n; i++) 17 { 18 pnew = (student*)malloc(sizeof(student)); 19 scanf("%d%s%d", &pnew->number, &str[i], &pnew->score); 20 strcpy(pnew->name, str[i]);//只有字符串初始化时才能使用“=”赋值 21 pnew->pnext = null; 22 pend->pnext = pnew; 23 pend = pnew; 24 25 } 26 return phead; 27 } 28 //寻找前驱节点 29 student *find_before(student* phead, student* p) 30 { 31 if (!p) return null; 32 student *pbefore = phead; 33 while (pbefore) 34 { 35 if (pbefore->pnext == p) 36 return pbefore; 37 pbefore = pbefore->pnext; 38 } 39 return null; 40 } 41 42 student *sort(student *head) { 43 student *phead,*pend,*q=null,*qbefore=null,*p=null; 44 int maxscore; 45 phead=pend = (student*)malloc(sizeof(student)); 46 while (head->pnext != null) 47 { 48 maxscore = head->pnext->score; 49 q = p = head->pnext; 50 //寻找最高分节点p 51 while (p->pnext!=null) 52 { 53 if(maxscore<p->pnext->score) 54 { 55 maxscore = p->pnext->score; q = p->pnext; 56 } 57 p = p->pnext; 58 } 59 pend->pnext = q; //将头指针指向q 60 q->pnext = null; //q节点指向空 61 pend = q; //更新尾节点 62 qbefore = find_before(head,q); //寻找q节点的前驱节点 63 qbefore->pnext = q->pnext; //将q的前驱节点指向q的后驱节点 从而将q节点从a链表中剔除 64 } 65 free(head);//释放head链表头节点 66 return phead; 67 } 68 void print(student *q) { 69 while (q->pnext != null) 70 { 71 q = q->pnext; printf("%sn", q->name); 72 } 73 free(q);//释放使用完的链表 74 } 75 int main() { 76 int n, i = 1; student *p,*q; 77 scanf("%d", &n); 78 p = create(n); 79 q=sort(p); 80 print(q); 81 }
如果有什么错误,请指正! 万分感谢!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/606081.html