在該鏈表結點有data數據,而且還有cpu,分給cpu隨機的時間片,根據時間片大小進行結點data的排序java
class LinkNode{//結點的創建 private int data; private int cpu; public LinkNode next; public LinkNode(int data) { this.data=data; } public int getCpu() { return cpu; } public void setCpu(int cpu) { this.cpu = cpu; } public int getData() { return data; } public void setData(int data) { this.data = data; }
class Linklist{ private LinkNode front; private LinkNode current; public Linklist() { current=front=new LinkNode(0);//單鏈表頭節點必須對象化,不然會致使空指針異常 } public void add(int data) {//鏈表的添加 LinkNode Node = new LinkNode(data); while(current.next!=null) { current=current.next; } current.next=Node; } public void print() {//鏈表的打印 LinkNode node =front.next; while(node!=null) { System.out.println(node.getData()+" "+node.getCpu()); node=node.next; } System.out.println("======================================"); } public void sort(int temp) {//對鏈表進行排序 int a; int b; for(int i=0;i<temp-1;i++) { LinkNode now=front.next; for(int j=0;j<temp-i-1;j++) { if(now.getCpu()>now.next.getCpu()) { a=now.getData(); b=now.next.getData(); now.setData(b); now.next.setData(a); a=now.cpu; now.cpu=now.next.cpu; now.next.cpu=a; System.out.println(); } now=now.next; } } } public void delete(int num) {//鏈表的刪除 int i=1; LinkNode node=front; while(i<num) { node=node.next; i++; } node.next=node.next.next; } }