C語言雙向鏈表實(shí)現(xiàn)根據(jù)使用頻率安排元素位置的功能實(shí)例代碼
C語言雙向鏈表應(yīng)用
前言:
平時(shí)使用音樂 器時(shí), 列表中的歌曲可以很方便地進(jìn)行增添,刪除,去重等操作。但其本質(zhì)都可以抽象成一個(gè)雙向鏈表。為了更方便用戶的使用,我認(rèn)為還可以增加一個(gè)將最常 的音樂放在 列表的頭部的功能,那么如何實(shí)現(xiàn)呢?
請(qǐng)看代碼:
#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è)比該元素使用頻率高的元素,將該元素插到找到的元素之后即可。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:C語言中g(shù)etch()函數(shù)詳解及簡(jiǎn)單實(shí)例
欄 目:C語言
本文標(biāo)題:C語言雙向鏈表實(shí)現(xiàn)根據(jù)使用頻率安排元素位置的功能實(shí)例代碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1703.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什