D Microsoft Word CSC22M Inbox (1,444) kabinadxM Inbox (538) nebiyouta Kira/Downl
ID: 3883578 • Letter: D
Question
D Microsoft Word CSC22M Inbox (1,444) kabinadxM Inbox (538) nebiyouta Kira/Downloads/CSC228-Fall2017-Project-1-List-DynamicArray pdf Consider the implementation of the List class using dynamic arrays as discussed in the class/ slides/ lecture code. Extend the code for the List class with each of the following functions. Each function should be implemented independent of each other. Analyze the asymptotic time complexity of each of the implementations with 'n' considered as the size of the List. i) Add a member function called print) to the List class that prints the contents of the List from index 0 to endOf Array with a space between each element. void print O (ii) Add a member function called subList(int fromIndex, int tolndex) to the List class that returns an object of class List itself. The returned List object is a sub list of the List object on which the subList function is called. The sublList should basically create a new List object, copy the contents of the List (fromlndex to tolndex) on which the function is called to the new List and return the sub list to the main function. You should not create any other function and just make use of one or more of the existing functions in the current version of the List class. List subl.ist(int fromlndex, int tolndex (ii) Add a member function called rotatel ist(int rotationSpace) to the List class that rotates the in the List by 'r r example, if the contents of the List are (before rotation): 10 12 5 7 19 21, then if the List is rotated to the left with a rotationSpace of 2, then the rotated List should be: 5 7 19 21 10 12. Note that the original List should be updated with the rotated contents so that after calling the rotatel ist function on the original List object, if one were to rchExplanation / Answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class List{
ArrayList<Integer> list;
public List(int i) {
list=new ArrayList<Integer>(i);
}
public void insertAtIndex(int index,int element){
list.add(index, element);
}
public void print(){
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
}
public List subList(int fromIndex,int toIndex){
ArrayList<Integer> sublist=new ArrayList<Integer>();
int temp=fromIndex;
for (int i = 0; i < (toIndex-fromIndex); i++) {
sublist.add(i, list.get(temp));
temp++;
}
List list=new List(1);
list.list=sublist;
return list;
}
public void rotateList(int rotationSpace){
Collections.rotate(list, rotationSpace);
}
public void reverseList(){
Collections.reverse(list);
}
public void segregateEvenOdd(){
int left = 0, right = list.size() - 1;
while(left < right){
while(list.get(left)%2 == 0 && left < right)
{
left++;
}
while(list.get(right)%2 != 0 && left < right)
{
right--;
}
if(left < right){
int temp=list.get(left);
list.set(left,list.get(right));
list.set(right, temp);
left++;
right--;
}
}
}
}
public class DynamicListArray {
public static void main(String[] args) {
int listSize;
Scanner input=new Scanner(System.in);
System.out.println("Enter list size: ");
listSize=input.nextInt();
List integerList=new List(1);
for(int i=0;i<listSize;i++){
int value;
System.out.println("Enter element # "+i+" : ");
value=input.nextInt();
integerList.insertAtIndex(i,value);
}
System.out.println("Original List: ");
integerList.print();
List sublist=integerList.subList(2, 4); //here we have to give toIndex value <list.size()
System.out.println("Sub list from 2 to 4: ");
sublist.print();
integerList.rotateList(2);
System.out.println("After rotation(2 elements to the left): ");
integerList.print();
integerList.reverseList();
System.out.println("After reverse: ");
integerList.print();
integerList.segregateEvenOdd();
System.out.println("After even/odd segregation: ");
integerList.print();
}
}
We can use Collections.reverse(list) to reverse a list and Collections.rotate(list, rotatespace) to rotate a list with given rotatespace
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.