1. 程式人生 > >LSD:低關鍵字優先;鏈式基數排序;lst.remove();取出和刪除表頭元素

LSD:低關鍵字優先;鏈式基數排序;lst.remove();取出和刪除表頭元素

有種靜態方法static{內容}是在類例項化時就生成
*/
@SuppressWarnings("unchecked")
static void raidSort(LinkedList<Card> lst){
//對低關鍵字進行分組
LinkedList[] rd=new LinkedList[10];//假設值從1到10
for(int i=0;i<rd.length;i++)rd[i]=new LinkedList();//每個值具體有多少個不確定
//吧lst中元素拆下來放入對應的組中
while (lst.size()>0) {
Card t=lst.remove();//刪除的表頭元素
rd[t.getPoint()-1].add(t);//新增到該組的連結串列尾部,point從1到10,陣列從0到9
}

//收集
for (int i = 0; i < rd.length; i++) {
lst.addAll(rd[i]);
}
//對高關鍵字進行分組
rd=new LinkedList[4];
for(int i=0;i<rd.length;i++)rd[i]=new LinkedList();
while (lst.size()>0) {
Card t=lst.remove();//刪除的表頭元素
rd[t.getType()].add(t);//新增到該組的連結串列尾部,point從1到10,陣列從0到9
}
//收集
for (int i = 0; i < rd.length; i++) {
lst.addAll(rd[i]);

}
}
/**
* @param args
*/
public static void main(String[] args) {
LinkedList<Card> lst=new LinkedList<Card>();
lst.add(new Card(typeEnum.valueOf(typeEnum.block), 5));
lst.add(new Card(typeEnum.valueOf(typeEnum.red), 5));
lst.add(new Card(typeEnum.valueOf(typeEnum.block), 7));
lst.add(new Card(typeEnum.valueOf(typeEnum.block),3));

lst.add(new Card(typeEnum.valueOf(typeEnum.block), 5));
lst.add(new Card(typeEnum.valueOf(typeEnum.spade), 5));
lst.add(new Card(typeEnum.valueOf(typeEnum.spade), 9));
lst.add(new Card(typeEnum.valueOf(typeEnum.red), 5));
lst.add(new Card(typeEnum.valueOf(typeEnum.plum_blossom), 6));
System.out.println(lst);
raidSort(lst);
System.out.println(lst);
}


}