【问题标题】:LinkedList issue printing and deletingLinkedList 问题打印和删除
【发布时间】:2013-04-21 22:45:42
【问题描述】:

好的,所以我一直在争论是否要问这个问题并保持这个问题,看看我是否能得到一些答案。我已经修复了大部分错误,但我对这个 LinkedList 有很大的问题。现在它一直在删除我在第一个节点后面的所有节点,并显示除最后一个节点之外的所有节点。我似乎无法弄清楚我的问题在哪里。我一直在使用 jgrasp 中的调试来提供帮助,但它并没有太大帮助

我已经包含了我正在处理的两个文件,供任何想要运行它并查看自己会发生什么的人使用

文件输入是一个文本文件,内容类似于:

 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;
}
}

提前致谢,不胜感激。

【问题讨论】:

  • 如果您多次看到此内容,我深表歉意。我一直优柔寡断并把它拉下来

标签: java data-structures linked-list nodes circular-list


【解决方案1】:

首先,您并没有像您说的那样使用虚拟节点。如果列表只包含一个节点,它实际上永远不会被删除,因为head.next 再次只是head。这也会使列表处于不一致的状态,其中size 为 0,但它仍然有一个节点。

find 如果您正在查找不在列表中的节点,则会给出不正确的结果。您可能应该检测到这一点并抛出异常。

【讨论】:

  • 我想如果你有 head.next 指向 head 那将是一个虚拟头节点。
  • 它会是,期望您使用“虚拟”来存储真实数据,而您的空列表中没有一个。真正的问题是您的LinkedList 使用默认构造函数。
  • 好的,那我必须解决这个问题。这也许可以解释为什么当我在 jgrasp 中调试时,我得到一个充满其他节点的节点
  • 如果你在纸上写出一些例子来弄清楚你想让你的列表做什么,这可能会有所帮助。你似乎对设计有点困惑。
  • 我开始画一些东西了。最让我困惑的是节点中有两个值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多