Question: 1. 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

1. 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 1. You need the following files - Node.java and LinkedList.java (provided

//Node class public class Node { private String data; private Node next; public Node(String d, Node n) { data = d; next = n; } public String getData() { return data; } public Node getNext() { return next; } public void setData(String d) { data = d; } public void setNext(Node n) { next = n; } public String toString() { return data + "-->"; } } 

//LinkedList class

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() { if (front==null) return true; else return false; } //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("."); } //remove front node public void removeFront() { if (front==null) { System.out.println("Empty list"); } else { front = front.getNext(); count--; } } //add a node to the end public void addToEnd(String d) { Node n = new Node(d, null); Node curr = front; if (front==null) front = n; else { while (curr.getNext()!=null) curr = curr.getNext(); curr.setNext(n); } count++; } //remove last node public void removeLast() { if (front==null) { System.out.println("Empty list"); } else if (front.getNext()==null) { front = null; count--; } else { Node curr = front; while (curr.getNext().getNext()!=null) curr = curr.getNext(); curr.setNext(null); count--; } } //search for a given data and return the index of the node (first occurrence) //return -1 if not found public int contains(String d) { Node curr = front; boolean found = false; int index = -1; while (curr!=null&&!found) { index++; if (curr.getData().equals(d)) found=true; curr= curr.getNext(); } if (!found) return -1; else return index; } //add a node at a given index public void add(int index, String d) { if (indexsize()) System.out.println("Can't add. Index out of bounds"); else { if (index==0) addToFront(d); else { Node curr = front; for(int i=0; i=size()) System.out.println("Can't remove. Index out of bounds"); else if (index==0) removeFront(); else if (index==size()-1) removeLast(); else { Node curr = front; for(int i=0;i 

Exercise 1: public static LinkedList interleave (Linked List list1, 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 list 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

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!