前言:
平时使用音乐播放器时,播放列表中的歌曲可以很方便地进行增添,删除,去重等操作。但其本质都可以抽象成一个双向链表。为了更方便用户的使用,我认为还可以增加一个将最常播放的音乐放在播放列表的头部的功能,那么如何实现呢?
请看代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int status;
typedef int elemtype;
typedef struct node{
elemtype data;
int freq;
struct node * next;
struct node * prior;
}node;
typedef struct node* dlinklist;
status visit(elemtype c){
printf ( "%d " ,c);
}
/*双向链表初始化*/
status initdlinklist(dlinklist * head,dlinklist * tail){
(*head)=(dlinklist) malloc ( sizeof (node));
(*tail)=(dlinklist) malloc ( sizeof (node));
if (!(*head)||!(*tail))
return ERROR;
/*这一步很关键*/
(*head)->prior=NULL;
(*tail)->next=NULL;
(*head)->freq=0;
(*tail)->freq=0;
/*链表为空时让头指向尾*/
(*head)->next=(*tail);
(*tail)->prior=(*head);
}
/*判定是否为空*/
status emptylinklist(dlinklist head,dlinklist tail){
if (head->next==tail)
return TRUE;
else
return FALSE;
}
/*尾插法创建链表*/
status createdlinklist(dlinklist head,dlinklist tail,elemtype data){
dlinklist pmove=head,qmove=tail,pinsert;
pinsert=(dlinklist) malloc ( sizeof (node));
if (!pinsert)
return ERROR;
else {
pinsert->data=data;
pinsert->prior=pmove;
pinsert->next=pmove->next;
pmove->next->prior=pinsert;
pmove->next=pinsert;
}
}
/*正序打印链表*/
status traverselist(dlinklist head,dlinklist tail){
dlinklist pmove=head->next;
while (pmove!=tail){
visit(pmove->data);
pmove=pmove->next;
}
printf ( "\\n" );
}
status traverselist2(dlinklist head,dlinklist tail){
dlinklist pmove=head->next;
while (pmove!=tail){
visit(pmove->freq);
pmove=pmove->next;
}
printf ( "\\n" );
}
/*在链表头插入元素*/
status inserthead(dlinklist head,dlinklist tail,elemtype data){
dlinklist pinsert;
pinsert=(dlinklist) malloc ( sizeof (node));
pinsert->data=data;
pinsert->next=NULL;
pinsert->prior=NULL;
tail->prior->next=pinsert;
pinsert->prior=tail->prior;
pinsert->next=tail;
tail->prior=pinsert;
return OK;
}
/*按使用频次排序*/
status locateorder(dlinklist head,dlinklist tail,elemtype data){
dlinklist pmove=head->next,qmove;
while (pmove!=tail&&pmove->data!=data)
pmove=pmove->next;
if (pmove==tail){
printf ( "未找到\\n" );
return ERROR;
}
else {
pmove->freq++;
qmove=pmove->prior;
while (qmove!=head&&qmove->freq<pmove->freq) //向前寻找比pmove->freq大的qmove->freq
qmove=qmove->prior;
if (qmove->next!=pmove){ //如果找到的qmove和pmove不是直接的前驱后继关系
pmove->next->prior=pmove->prior;
pmove->prior->next=pmove->next; //将pmove取下
pmove->prior=qmove;
pmove->next=qmove->next;
qmove->next->prior=pmove;
qmove->next=pmove; //插到qmove之后
}
}
}
int main( void ){
dlinklist head,tail,pmove=head;
int i=0;
initdlinklist(&head,&tail);
for (i=0;i<10;i++)
inserthead(head,tail,i);
printf ( "未经过排序的链表为\\n" );
traverselist(head,tail);
printf ( "在按使用频率排序后的链表为:\\n" );
locateorder(head,tail,5);
for ( int i=0;i<3;i++){
locateorder(head,tail,6);
}
traverselist(head,tail);
printf ( "各元素使用频率为\\n" );
traverselist2(head,tail);
}
|
要实现这一功能,最重要的函数是locateorder(),其实现思路是:如果某个元素的使用频率不为0,则定义一个向链表头移动的游标,寻找一个比该元素使用频率高的元素,将该元素插到找到的元素之后即可。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 28
-
2025-05-27 28
-
2025-05-29 100
-
2025-05-27 95
-
2025-05-29 64
热门评论