You need the following files - Node.java and LinkedList.java (provided at the en
ID: 3861985 • Letter: Y
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 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
//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();
}
//new 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")Explanation / Answer
Sorry. I could do only part of the code. Here is the code for you:
class InterleaveReverseChopskip
{
public static LinkedList interleave(LinkedList list1, LinkedList list2)
{
LinkedList newList = new LinkedList();
Node temp1 = list1.getFront();
Node temp2 = list2.getFront();
while(temp1 != null && temp2 != null)
{
newList.addToBack(temp1.getData());
newList.addToBack(temp2.getData());
temp1 = temp1.getNext();
temp2 = temp2.getNext();
}
while(temp1 != null)
{
newList.addToBack(temp1.getData());
temp1 = temp1.getNext();
}
while(temp2 != null)
{
newList.addToBack(temp2.getData());
temp2 = temp2.getNext();
}
return newList;
}
public static void reverse(LinkedList list)
{
int count = list.size() - 1;
LinkedList listNew = new LinkedList();
for(int i = 0; i < count; i++)
{
Node temp = list.removeFront();
listNew.addToFront(temp.getData());
}
list = new LinkedList();
for(int i = 0; i < count; i++)
{
Node temp = listNew.removeFront();
list.addToBack(temp.getData());
}
list.enumerate();
}
public static void main(String[] args)
{
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
list1.addToBack("A");
list1.addToBack("B");
list1.addToBack("C");
list1.addToBack("D");
list1.addToBack("E");
list1.addToBack("F");
list2.addToBack("W");
list2.addToBack("X");
list2.addToBack("Y");
list2.addToBack("Z");
list1.enumerate();
list2.enumerate();
LinkedList list3 = interleave(list1, list2);
list3.enumerate();
reverse(list3);
list3.enumerate();
}
}
And the modification of LinkedList.java is:
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();
}
//new 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("."); }
//add a node to the end of the linked list
public void addToBack(String d){
if(count == 0)
addToFront(d);
else
{
Node n;
n = new Node(d, null);
Node temp = front;
while(temp.getNext() != null)
temp = temp.getNext();
temp.setNext(n);
}
count++;
}
//new method added - get the last node
public Node getBack() {
if(front == null)
return null;
Node temp = front;
while(temp.getNext() != null)
temp = temp.getNext();
return temp;
}
//removes the first node of the linked list.
public Node removeFront()
{
Node temp = front;
front = front.getNext();
count--;
return temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.