C語言雙向鏈表實現(xiàn)根據(jù)使用頻率安排元素位置的功能實例代碼
C語言雙向鏈表應(yīng)用
前言:
平時使用音樂播放器時,播放列表中的歌曲可以很方便地進行增添,刪除,去重等操作。但其本質(zhì)都可以抽象成一個雙向鏈表。為了更方便用戶的使用,我認為還可以增加一個將最常播放的音樂放在播放列表的頭部的功能,那么如何實現(xiàn)呢?
請看代碼:
#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;
/*這一步很關(guān)鍵*/
(*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;
}
/*尾插法創(chuàng)建鏈表*/
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不是直接的前驅(qū)后繼關(guān)系
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("未經(jīng)過排序的鏈表為\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);
}
要實現(xiàn)這一功能,最重要的函數(shù)是locateorder(),其實現(xiàn)思路是:如果某個元素的使用頻率不為0,則定義一個向鏈表頭移動的游標,尋找一個比該元素使用頻率高的元素,將該元素插到找到的元素之后即可。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)之二叉樹的非遞歸后序遍歷算法
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之二叉樹的非遞歸后序遍歷算法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
Qt5連接并操作PostgreSQL數(shù)據(jù)庫的實現(xiàn)示例
本文主要介紹了Qt5連接并操作PostgreSQL數(shù)據(jù)庫的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
C語言數(shù)組學(xué)習(xí)之特殊矩陣的壓縮存儲
矩陣在計算機圖形學(xué)、工程計算中都占有舉足輕重的地位,本文將討論如何將矩陣更有效地存儲在內(nèi)存中,并且能夠方便地提取矩陣中的元素。感興趣的同學(xué)可以了解一下2021-12-12
關(guān)于C++對象繼承中的內(nèi)存布局示例詳解
這篇文章主要給大家介紹了關(guān)于C++對象繼承中內(nèi)存布局的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08

