Question How can I called this method in the main and how ca

Question?
How can I called this method in the main, and how can I print it?
Code :
import java.util.LinkedList;
import java.util.Stack;
public class MergeSort {

class inode {
public int item;
public inode next;

public inode(int i, inode n){
item = i;
next = n;
}
public inode(int i){
item = i;
next = null;
}
}

public LinkedList<inode> mergeSortStacks(LinkedList<inode> list) {
Stack<LinkedList<inode>> stack1 = new Stack<LinkedList<inode>>();
Stack<LinkedList<inode>> stack2 = new Stack<LinkedList<inode>>();
/*******************
* push all elements on stack1
*******************/
for (int i = 0; i < list.size(); i++)
{
inode node = new inode(list.get(i).item, list.get(i).next);
LinkedList<inode> ll = new LinkedList<>();
ll.add(node);
stack1.push(ll);
}
/******************
* pop two elements from one stack
* merge them and push onto another stack
* repeat this until merged
*****************/
while (stack1.size()>1)
{
while (stack1.size()>1)
{
LinkedList<inode> ll1 = stack1.pop();
LinkedList<inode> ll2 = stack1.pop();
LinkedList<inode> mergedll=merge(ll1, ll2);
stack2.push(mergedll);
}
while (stack2.size()>1)
{
LinkedList<inode> ll1 = stack2.pop();
LinkedList<inode> ll2 = stack2.pop();
LinkedList<inode> mergedll=merge(ll1, ll2);
stack1.push(mergedll);
}
}
return stack1.isEmpty() ? stack2.pop() : stack1.pop();

}
private LinkedList<inode> merge(LinkedList<inode> ll1, LinkedList<inode> ll2) {
LinkedList<inode> mergedll = new LinkedList<inode>();
int i = 0 , j=0;
/***********************
* merge the elements until one or both of the lists reach their end
**********************/
while(ll1.get(i) !=null && ll2.get(j)!=null){
if(ll1.get(i).item < ll1.get(i).item){
mergedll.add(ll1.get(i));
i++;
}else{
mergedll.add(ll2.get(i));
j++;
}
}
/*****************************
* if ll2 was finised before ll1 then
* put all remaining elements of ll1 into mergelist
*****************************/
while(ll1.get(i)!=null){
mergedll.add(ll1.get(i));
i++;
}
/*****************************
* if ll1 was finised before ll2 then
* put all remaining

Solution


Hi Friend you have :

public class MergeSort {

   // main method is
   public LinkedList<inode> mergeSortStacks(LinkedList<inode> list) {

       ////
   }
}

Method \'mergeSortStacks\' is user facing method, any user can pass \'LinkedList\' parameter in this method and
receive the returned value from this method.

\'mergeSortStacks\' is instance method of the class MergeSort, so you need to create an object of class MergeSort in
main methos and call \'mergeSortStacks\' method by passing a list.

public static void main(String[] args) {
  
   // creating an object of MergeSort
   MergeSort obj = new MergeSort();

   // Suppose you have a list LinkedList<inode> list

   // callinf mergeSortStacks method
   obj.mergeSortStacks(list);
}

Question? How can I called this method in the main, and how can I print it? Code : import java.util.LinkedList; import java.util.Stack; public class MergeSort {
Question? How can I called this method in the main, and how can I print it? Code : import java.util.LinkedList; import java.util.Stack; public class MergeSort {

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site