【发布时间】: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