【问题标题】:memory management using linked list使用链表进行内存管理
【发布时间】:2014-06-24 04:28:22
【问题描述】:

这是一个关于使用链表实现内存管理的家庭作业。
每个内存进程请求特定大小的内存,该内存必须连续大到足以容纳内存,然后分配进程。当作业终止时,其允许的内存变为空闲。
这是我为此编写的 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 循环没有被执行。这有什么问题?

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

【问题讨论】:

  • 对于第二个问题,使用调试器将帮助您识别问题。

标签: java memory-management linked-list


【解决方案1】:

评论作为答案:

你应该把它分成两个问题。您可以通过使用静态工厂方法来创建对象,而不是直接使用构造函数来摆脱对无用构造函数 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?我看到你用head 初始化temp,但我看不到你在哪里初始化它。

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

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多