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);...

  • public class LinkedList {          // The LinkedList Node class    private class Node{...

    public class LinkedList {          // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)    {        this.head = new Node(gdata);    }...

  • 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++ Given this linked list class: class LinkedList { private: Node * head; public: LinkedList(); void...

    C++ Given this linked list class: class LinkedList { private: Node * head; public: LinkedList(); void prepend(int val); int sum(); }; Implement a sum method which will return the sum of the values stored in the linked list.

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

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

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