Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. 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

3. 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 BELOWimage text in transcribed

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 + "-->"; } } 
//class LinkedList 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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions