1. 程式人生 > >資料結構-雙向連結串列-插入排序練習題

資料結構-雙向連結串列-插入排序練習題

/*

 若線性表中各結點的查詢概率不等,則可用如下策略提高順序查詢的效率:若找到指定的結點,則將該結點的fre域的值加1,
 使得經常被查詢的結點位於表的前端。設雙向連結串列的儲存結構有四個域:pre,data,next和fre,data域為字元型,fre域為整形。
 設計滿足該功能的程式。
要求:
(1)首先建立一個不少於4個結點的雙向連結串列;
(2)從鍵盤輸入一個字元,在連結串列中進行查詢。如果找到了,則將fre域的值加1,然後與左邊結點的fre域的值進行比較,
 比左邊結點的fre的值大,則交換結點,直至不大為止;如果沒有找到,則建立一個新結點,fre域的值為1,並且將新結點連結到連結串列的最後。
(3)輸入數字字元0,則程式結束。

 */
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct _DNode DNode; struct _DNode{ DNode * pre; char ch; int count; DNode * next; }; DNode * CreatDNode(char ch){ DNode * dNode=(DNode *)malloc(sizeof(DNode)); dNode->next=NULL; dNode->
pre=NULL; dNode->ch=ch; dNode->count=0; return dNode; } void AddDNode(DNode* dNode,char ch){ DNode * p=dNode; while (p->next!=NULL)p=p->next; DNode * dNode1=(DNode *)malloc(sizeof(DNode)); dNode1->ch=ch; dNode1->count=0; dNode1->pre=p; dNode1->
next=NULL; p->next=dNode1; } DNode * MoveSetp(DNode * head,DNode * B){ DNode * A=B->pre; DNode * Zero=A->pre; DNode * C=B->next; //Zero(or NULL)<==>B if(Zero!=NULL){ Zero->next=B; B->pre=Zero; } else{ B->pre=NULL; } //B<==>A B->next=A; A->pre=B; //B<==>C(or NULL) if(C!=NULL){ C->pre=A; A->next=C; } else{ A->next=NULL; } if(Zero==NULL){ return B; } else{ return head; } } DNode * sortOneself(DNode * head,DNode * p){ while (p->pre!=NULL&&p->count>(p->pre)->count){ head=MoveSetp(head,p); } return head; } DNode * sortDNode(DNode * head){ DNode * letter=head; DNode * lcp=head; // letter=sortOneself(letter,letter->next); // letter=sortOneself(letter,letter->next->next); // letter=sortOneself(letter,letter->next->next->next); // letter=sortOneself(letter,letter->next->next->next->next); // letter=sortOneself(letter,letter->next->next->next->next->next); int c=0; while (lcp->next!=NULL){ c++; lcp=lcp->next; letter=sortOneself(letter,lcp); } //printf("c=%d",c); return letter; } bool FindChar(DNode * dNode, char ch){ int flag=0; DNode * p=dNode; while (1){ if(p->ch==ch){ flag=1; break; } if(p->next==NULL){ break; } p=p->next; } if(flag==1){ (p->count)++; return true; } else{ return false; } } void ShowDNode(DNode * dNode){ DNode * dNode1=dNode; printf("|%c;%d|",dNode1->ch,dNode1->count); while (dNode1->next!=NULL){ dNode1=dNode1->next; printf("<=>|%c;%d|",dNode1->ch,dNode1->count); } printf("\n"); //printf("<=>|%c;%d|\n",dNode1->ch,dNode1->count); } void UI(DNode * head){ char ch; printf("請輸入查詢字元(0 to quit):"); scanf("%c",&ch); getchar(); while (ch!='0'){ if(FindChar(head,ch)){ printf("找到了\n"); head=sortDNode(head); ShowDNode(head); } else{ printf("沒找到\n"); AddDNode(head,ch); ShowDNode(head); } printf("請輸入查詢字元(0 to quit):"); scanf("%c",&ch); getchar(); } } int main(void){ DNode * letter =CreatDNode('a'); DNode * lcp=letter; //產生序列 for(char c='b';c<='h';c++){ AddDNode(letter,c); } ShowDNode(letter); //隨機查詢(a-z) // for(int i=0;i<100;i++){ // FindChar(letter,(char)('a'+rand()%25)); // } UI(letter); // letter=sortDNode(letter); // letter=MoveSetp(letter,letter->next); // ShowDNode(letter); return 0; }