useing linked lists, stacks/queues 1. Create a function divideMid, which divides
ID: 3635942 • Letter: U
Question
useing linked lists, stacks/queues 1. Create a function divideMid, which divides a list at its middle. Consider the example: list<int> l1; List<int> l2; Suppose l1 points to the list with elements 1 2 3 4 5 6 7 8. Then the statement divideMid( l1, l2 ); divides the list into two sublists: l1 will have 1 2 3 4, and l2 will have 5 6 7 8. Do not use any of the merge, sort or splice methods from the library. The above operation should be done by you completely. 2. Create a function, which reverses a list. Consider the example: list<int> l1; Suppose l1 points to the list with elements 1 2 3 4 5 6 7 8. Then the statement reverse( l1 ); reverses the elements of the l1 to 8 7 6 5 4 3 2 1. Do not use the reverse method from the library.Explanation / Answer
public class Assignment { static List l1=new ArrayList(); static List l2=new ArrayList(); public static void main(String[] args) { l1.add(1);l1.add(2); l1.add(3);l1.add(4);l1.add(5);l1.add(6); l1.add(7);l1.add(8); System.out.println("before divide"); System.out.println("l1==>"+l1); System.out.println("l2==>"+l2); divideMid(l1,l2); System.out.println("after divide"); System.out.println("l1==>"+l1); System.out.println("l2==>"+l2); System.out.println(" before reversal"); System.out.println("l1==>"+l1); reverse(l1); System.out.println("after reversal"); System.out.println("l1==>"+l1); } private static void reverse(List l1) { // TODO Auto-generated method stub List tempList=new ArrayList(); for(int i=l1.size()-1;i>=0;i--){ tempList.add(l1.get(i)); } l1.removeAll(tempList); for(int i=0;i l1.add(tempList.get(i)); } //System.out.println(l1+" this is temp list"); } private static void divideMid(List l1, List l2) { // TODO Auto-generated method stub int mid=l1.size()/2; int m=mid,size=l1.size(); for(int i=mid;i { l2.add(l1.get(m)); l1.remove(m); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.