() void CreatOut( )∥建立有n個單詞的單向鏈表重復單詞只在鏈表中保留一個最後輸出頻度最高的k個單詞
{LinkedList la;
la=(LinkedList)malloc(sizeof(node));∥申請頭結點
la>next=null;∥鏈表初始化
for(i=;i<=n;i++)∥建立n個結點的鏈表
{scanf(%sa);∥a是與鏈表中結點數據域同等長度的字符數組
p=la>next;pre=p;∥p是工作指針pre是前驅指針
while(p!=null)
if(strcmp(p>dataa)==)
{p>freg++;∥單詞重復出現頻度增
pre>next=p>next;∥先將p結點從鏈表上摘下再按頻度域值插入到合適位置
pre=la; q=la>next;
while(q>freg>p>freg) (pre=q; q=q>next; )
pre>next=p; p>next=q;∥將p結點插入到合適位置
}
else {pre=p;p=p>next;}∥指針後移
if(p==null)∥該單詞沒出現過應插入到鏈表最後
{p=(LinkedList)malloc(sizeof(node));
strcopy(p>dataa);p>freg=;p>next=null;pre>next=p;
}∥if 新結點插入
}∥結束for循環建表
int ki=;
scanf(輸入要輸出單詞的個數%d&k);
p=la>next;
while (p && i<k)∥輸出頻度最高的k個單詞
{printf(第%d個單詞%s出現%d次\n++ip>datap>freg);
p=p>next;
}
if (!p)
printf(給出的%d值太大\nk);
}∥結束算法
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23318.html