java - memory management using linked list -
java - memory management using linked list -
this homework question in relation memory management implementation using linked lists. each memory process requests of particular size of memory must contiguously big plenty fit memory , allocate process.when job terminates,its allowed memory becomes free. java code wrote this.
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); homecoming 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); } }
i have 2 questions. have have constructor
public partition(int beginaddress,int endaddress,int a){ head=new partitionnode(beginaddress,endaddress); begin=beginaddress; end=endaddress; current=head; }
where int a of no use.it there create sure constructor's argument list different from
public partition(int beginaddress,int endaddress){ current=new partitionnode(beginaddress,endaddress); }
because of have phone call partition p=new partition(300,3000,1);
1 beingness useless. how can rid of problem.
my next question implementing method delete process.
public void deleteprocess(int process){ partitionnode temp=head; while(temp.next!=null){ system.out.println(temp.processid); temp=temp.next; } }
the while loop doesn't executed.what's wrong that?
can please help me right mistakes?
comment-as-answer:
you should split 2 questions. can rid of need useless concstructor arg using static mill methods create object, rather constructor directly. allow name different ways create instance of class. create constructor private while having public static methods homecoming new instance of class. allow utilize same parameters "constructor", while having ability create , homecoming unique instance:
class illustration { public static void main(string[] args) { object first = object.createobject(1, 2); object sec = object.createandstore(1, 2); } } class object { private int a, b; //private constructor ensures need methods private object(int a, int b) { //create node } //the mill methods public static object createobject(int a, int b) { homecoming new object(a, b); } public static object createandstore(int a, int b) { object ob = new object(); //store vars using ob homecoming ob; } }
as loop part, init temp.next
? see initialize temp
head
, don't see initialize it.
also, highly suggest changing title of question more fit issues
java memory-management linked-list
Comments
Post a Comment