Question: You need the following files - Node.java and LinkedList.java (provided at the end) Write a demo program with three static methods for interleave, reverse and

You need the following files - Node.java and LinkedList.java (provided at the end) Write a demo program with three static methods for interleave, reverse and chopSkip, as specified in the exercises 1, 2 and 3 given below. Note that they are all static methods and they are NOT to be added to the LinkedList class. Use the demo program to build two linked lists and show the operation of the interleave, reverse and chopSkip methods.

*I am using JGrasp so please make sure it is compatible *Please comment throughout code *FULL QUESTION IN IMAGES BELOW

You need the following files - Node.java and LinkedList.java (provided at the

//Node.java public class Node{ //private attributes private String data; private Node next; //constructor that takes in two parameters public Node(String d, Node n){ data = d; next = n; } //get methods public String getData(){ return data; } public Node getNext(){ return next; } //set methods public void setData(String d){ data = d; } public void setNext(Node n){ next = n; } //toString methods public String toString(){ return data + "-->"; } }

public class LinkedList{

private Node front;

private int count;

//constructor

public LinkedList(){

front = null;

count = 0;

}

//add a node to the front of the linked list

public void addToFront(String d){

Node n;

n = new Node(d, front);

front = n; count++;

}

//get the current size of the list

public int size(){

return count;

}

//check if the list is empty

public boolean isEmpty(){

return (count==0);

}

//clear the list

public void clear(){ front = null; count=0;

}

//get the content of the first node

public String getFrontData() {

if (front==null) return "Empty list";

else return front.getData();

}

/ew method added - get the first node

public Node getFront() {

return front; }

//scan the list and print contents

public void enumerate() {

Node curr = front; while (curr!=null) {

System.out.print(curr);

curr = curr.getNext(); }

System.out.println("."); }

}

You need the following files Nodejava and LinkedListjava Write a demo program with three static methods for interleave, reverse and chopSkip, as specified in the exercises 1,2 and 3 given below. Note that they are all static methods and they are NOT to be added to the LinkedList class. Use the demo program to build two linked lists and show the operation of the interleave, reverse and chopskip methods. Exercise 1 public static Linked List interleave (LinkedList list 1, LinkedList list2) that takes in two linked lists of Strings and creates a third linked list that contains the items in the first two lists interleaved. For example, if list A B C D E F and list2 W X Y Z, then that is, it adds the first item in list1, then the first item in list2, then the second item in list1, the second item in list2, etc. After it reaches the end of the shorter list, it just appends the remaining items from the longer list at the end. Exercise 2: public static void reverse (LinkedList list) that reverses a given linked list of Strings. Note that you should not create a new linked list but reverse the existing list. For example, if the input listis a- b- c- d then it should be changed to Hint: First write a swap method that swaps the contents of two nodes at indices x and y. (Note: do not swap the nodes themselves, but just the String data in the nodes. Then use the swap method in your reverse method. Exercise 3: public static void chopskip (LinkedList list) that removes every other node (keeping the first, third nodes, removing the second, fourth, from a list. The list ("aardvark", "bear "cougar", "dog", elephant "fox") should become ("aardvark", "cougar", elephant")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!