内存管理使用链表

时间:2014-06-24 04:28:23

标签: java memory-management linked-list

这是与使用链接列表的内存管理实现相关的家庭作业问题 每个内存处理特定大小内存的请求,这些内存必须连续大到足以适合内存,然后分配进程。当作业终止时,其允许的内存变为空闲。
这是我为此编写的java代码。

public class PartitionNode{
    int beginAddress;
    int endAddress;
    boolean holeFree;
    int processId;
    PartitionNode next;

    public PartitionNode(int begin,int end){
         beginAddress=begin;
         endAddress=end;
         holeFree=true;
         processId=-1;
         next=null;


    }
    public PartitionNode(){}
    public PartitionNode(int begin,int end,int i){
        beginAddress=begin;
        endAddress=end;
        holeFree=false;
        processId=i;

    }

}

public class Partition{
    private  PartitionNode head;
    public PartitionNode current;

public  int begin;
public int end;
public PartitionNode newPartition;
public Partition(int beginAddress,int endAddress,int a){
    head=new PartitionNode(beginAddress,endAddress);
    begin=beginAddress;
    end=endAddress;
    current=head;
}
public Partition(int beginAddress,int endAddress){
    current=new PartitionNode(beginAddress,endAddress);

}
public void addProcess(int size,int id){
    if((current.endAddress-current.beginAddress>=size)&& current.holeFree==true){
        newPartition=new PartitionNode(current.beginAddress,current.beginAddress+size-1,id);
        newPartition.next=refresh();

        System.out.println("beginAddress"+newPartition.beginAddress);
        System.out.println("endAddress"+newPartition.endAddress);

    }

}

 public void print(){
    System.out.println("beginAddress"+newPartition.beginAddress);
     System.out.println("endAddress"+newPartition.endAddress);
 }


public   PartitionNode refresh(){
    current=new PartitionNode(newPartition.endAddress+1,end);
        return current;

}
public void deleteProcess(int process){
    PartitionNode temp=head;

    while(temp.next!=null){
        System.out.println(temp.processId);
        temp=temp.next;

    }

}



public static void main (String args[]){
    Partition p=new Partition(300,3000,1);
    p.addProcess(500,1);
    p.addProcess(800,2);
    p.addProcess(400,3);
    p.deleteProcess(5);
    System.out.println(p.head.beginAddress);
}


}

我有两个问题 我必须有一个构造函数

public Partition(int beginAddress,int endAddress,int a){
        head=new PartitionNode(beginAddress,endAddress);
        begin=beginAddress;
        end=endAddress;
        current=head;
    }  

其中 int a 没有用。它就是为了确保这个构造函数的参数列表与

不同
 public Partition(int beginAddress,int endAddress){
        current=new PartitionNode(beginAddress,endAddress);

    }    

因为现在我必须打电话为Partition p=new Partition(300,3000,1); 1 无用。
我怎样才能摆脱这个问题。

我的下一个问题是实现删除进程的方法。

public void deleteProcess(int process){
        PartitionNode temp=head;

        while(temp.next!=null){
            System.out.println(temp.processId);
            temp=temp.next;

        }

    }  

while循环没有被执行。那有什么问题?

有人可以帮我纠正错误吗?

1 个答案:

答案 0 :(得分:0)

评论作为答案:

你应该把它分成两个问题。通过使用静态工厂方法来创建对象,而不是直接构造函数,可以摆脱对无用的concstructor arg的需求。它们允许您命名不同的方法来创建类的实例。在具有返回类的新实例的公共静态方法的同时使构造函数成为私有的。这将允许您为"构造函数"使用相同的参数,同时具有创建和返回唯一实例的能力:

class Example {
     public static void main(String[] args) {
          Object first = Object.createObject(1, 2);
          Object second = Object.createAndStore(1, 2);
     }
}

class Object {
     private int a, b;

     //private constructor ensures need for methods
     private Object(int a, int b) {
          //create node
     }

     //the factory methods
     public static Object createObject(int a, int b) {
          return new Object(a, b);
     }

      public static Object createAndStore(int a, int b) {
          Object ob = new Object();
          //store vars using ob
         return ob;
     }
}

至于循环部分,你在哪里初始化temp.next?我看到您使用temp初始化head,但我不知道您在哪里初始化它。

此外,我强烈建议更改此问题的标题以更适合问题