1

わかりましたので、この質問を続けて、答えが得られるかどうかを確認するかどうかについて議論してきました. ほとんどのバグを修正しましたが、この LinkedList に大きな問題があります。現在、最初のノードの後ろにあるすべてのノードを削除し続け、最後のノードを除くすべてを表示します。自分の問題がどこにあるのかわからないようです。jgrasp でデバッグを使用して支援してきましたが、あまり役に立ちませんでした

実行して何が起こるかを自分で確認したい人のために、私が取り組んでいる 2 つのファイルを含めました。

ファイル入力は、次のようなテキスト ファイルです。

 1,10
 5,16
 2,7
 4,12
 3,19
 6,25
 9,13
 7,21
 8,4

時間を表す int も必要です。5本使っています

これは私のメインファイルです:

import java.util.*;
import java.io.*;

public class Test_RoundRobin{

public static void main(String args[]) throws IOException {
    LinkedList list=new LinkedList();
    String file=args[0];
    int cpuTime=Integer.parseInt(args[1]);

    Scanner fin=new Scanner(new FileReader(file));
    String pattern=",";
    fin.useDelimiter(pattern);
    while(fin.hasNext()){
        String s=fin.nextLine();
        String[] array=s.split(pattern);
        int pid=Integer.parseInt(array[0]);
        int time=Integer.parseInt(array[1]);
        list.add(pid, time);
    }
    fin.close();
    System.out.println(list);
    list.sortList();
    System.out.println(list);
    int count=1;
    while(list.size!=0){
        list.timeSlice(cpuTime);
        System.out.println("Run " + count + ": " + list);
        count++;
    }
    System.out.println(list);
}
}

これは私が持っている LinkedList ファイルです。私はダミーのヘッド ノードを使用しており、それは循環的な単一リンク リストです。

public class LinkedList{
private class Node{
    private int pid;
    private int time;
    private Node next;

    public Node(int pid, int time){
        this.pid=pid;
        this.time=time;
    }
}//end Node

int size;
Node head;

public void add(int pid, int time) {
    Node curr=head;
    Node newNode=new Node(pid, time);
    //empty list
    if(head==null){
        head=newNode;
        newNode.next=head;
    }//end if
    else{
        while(curr.next!=head){
            curr=curr.next;
        }//end while
        curr.next=newNode;
        newNode.next=head;
    }//end else
    size++;
}//end add

public void delete(Node curr){
    if(size==0){
        return;
    }
    else if(head==curr){
        head=curr.next;
    }
    else{
        Node prev=find(curr);
        prev.next=curr.next;
    }
    size--;
}

public Node find(Node curr){
    for(Node prev=head; prev.next!=curr; prev=prev.next){
        return prev;
    }
    return curr;
}

public void sortList(){
    Node position;  // Position to fill...
    Node start;            // Where to start looking for the smallest...

    if(size>=0){
        for(position=head; position.next!=head; position=position.next){
            Node smallest=position;

            for(start=position.next; start!=head; start=start.next){
                if (start.pid<smallest.pid){
                    smallest = start;
                }//end if
            }//end for
            int tempPID=position.pid;
            int tempTime=position.time;
            position.pid=smallest.pid;
            position.time=smallest.time;
            smallest.pid=tempPID;
            smallest.time=tempTime;
        }//end for
    }//end if
}//end sortList

public void timeSlice(int cpuTime){
    for(Node curr=head; curr.next!=head; curr=curr.next){
        curr.time=curr.time-cpuTime;
        System.out.print("<" + curr.pid + ", " + curr.time +">" + " ");
        //if the time remaining <= 0 then remove the node
        if(curr.time<=0){
            System.out.println("Process " + curr.pid + " has finished, and is now being terminated");
            delete(curr);
        }
    }
}

public String toString(){
    String s="";
    for(Node curr=head; curr.next!=head; curr=curr.next)
        s=s+"<"+curr.pid+", "+curr.time+"> ";

    return s;
}
}

よろしくお願いします。

4

1 に答える 1

1

まず、あなたが言ったようにダミーノードを使用していません。リストにノードが 1 つしか含まれていない場合は、実際には削除されませhead.nexthead。これにより、リストは一貫性のない状態になり、size0 になりますが、まだノードがあります。

findリストにないノードを探している場合、誤った結果が返されます。おそらくこれを検出して、例外をスローする必要があります。

于 2013-04-21T22:55:07.360 に答える