C語言雙向鏈表實(shí)現(xiàn)根據(jù)使用頻率安排元素位置的功能實(shí)例代碼
C語言雙向鏈表應(yīng)用
前言:
平時(shí)使用音樂播放器時(shí),播放列表中的歌曲可以很方便地進(jìn)行增添,刪除,去重等操作。但其本質(zhì)都可以抽象成一個(gè)雙向鏈表。為了更方便用戶的使用,我認(rèn)為還可以增加一個(gè)將最常播放的音樂放在播放列表的頭部的功能,那么如何實(shí)現(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;
/*鏈表為空時(shí)讓頭指向尾*/
(*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);
}
要實(shí)現(xiàn)這一功能,最重要的函數(shù)是locateorder(),其實(shí)現(xiàn)思路是:如果某個(gè)元素的使用頻率不為0,則定義一個(gè)向鏈表頭移動(dòng)的游標(biāo),尋找一個(gè)比該元素使用頻率高的元素,將該元素插到找到的元素之后即可。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
深入理解約瑟夫環(huán)的數(shù)學(xué)優(yōu)化方法
本篇文章是對約瑟夫環(huán)的數(shù)學(xué)優(yōu)化方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實(shí)現(xiàn)時(shí)間轉(zhuǎn)換及格式化
這篇文章主要為大家詳細(xì)介紹了C++中實(shí)現(xiàn)時(shí)間轉(zhuǎn)換及格式化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
linux c語言操作數(shù)據(jù)庫(連接sqlite數(shù)據(jù)庫)
linux下c語言操作sqlite數(shù)據(jù)庫實(shí)例方法,大家參考使用吧2013-12-12
C語言實(shí)現(xiàn)電話簿管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)電話簿管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
基于C語言實(shí)現(xiàn)的TCP服務(wù)器的流程分析
本文詳細(xì)介紹了如何使用C語言編寫一個(gè)簡單的TCP服務(wù)器,包括創(chuàng)建套接字、綁定IP和端口、監(jiān)聽連接請求、接受客戶端連接、數(shù)據(jù)接收與發(fā)送以及關(guān)閉套接字等步驟,最后通過一個(gè)簡單的示例展示了TCP服務(wù)器的基本實(shí)現(xiàn)過程2024-10-10

