Question

JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

JAVA

you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class:

printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc).

Create a LinkedListDemo class. Use a Scanner Class to read in city names and store each name in the LinkedList by adding to the front of the list. Read in names until * is entered.
Print out the LinkedList.

Details

Sample Input
Truro Halifax Pictou Trinty Moncton *

Sample Output
Moncton–>Trinty–>Pictou–>Halifax–>Truro–>
Even nodes:
Moncton–>Pictou–>Truro–>

//class LinkedList (only the first few methods are given here)
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();
}

}
//ADD NEW METHOD
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

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();
       }

   }
   // ADD NEW METHOD removeMiddle

   public void printsEvenNodes(){
       Node curr = front;
       while (curr != null) {
           //prints current node data
           System.out.print(curr);
           //moving to next node
           curr = curr.getNext();
           // if is it not last node, go to even node
           if(curr!=null)
               curr=curr.getNext();
       }
   }

}

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

public class LinkedListDemo {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       LinkedList list = new LinkedList();
       while(true){
           System.out.println("Enter name");
           String s =sc.nextLine();
           if(s.equals("*"))
               break;
       list.addToFront(s);
       }
          
       // printing the list
       System.out.println();
       list.enumerate();
       System.out.println("\nEven Nodes : ");
       list.printsEvenNodes();
   }
}

Add a comment
Know the answer?
Add Answer to:
JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT