Question

Assume a class exists named MyList that has contained in it a data attribute of type...

Assume a class exists named MyList that has contained in it a data attribute of type MyNode that is named head. The class MyList represents a singly-linked list. In the MyNode class, there is a getter method named getNext() that returns he next node in the linked list. Implement an instance method named print() that takes no formal parameters, returns nothing, and prints out the string representation of each node in the linked list from head to tail, with each node printed on a separate line.

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

Here is the code for getter

import java.util.*;
class MyNode
{
public int data;
public MyNode next = null;
public MyNode getNext(){
return this.next; // next stores the next node instance.
}
}
public class MyList{
MyNode head =null;
public void print(){
MyNode temp = head; // temp object is to iterate through the list
if(temp!=null){
System.out.println("The data in the linked list are:");
while(temp!=null){
System.out.println("Data in this Node is : "+temp.data);
temp = temp.getNext();
}
}
else{
System.out.println("No data to print");
}
}
   public static void main(String[] args) {
       MyList obj = new MyList();
      
       MyNode node1 = new MyNode();// Node1 is created and assigned.
       node1.data = 10;
      
       obj.head = node1;
       MyNode node2 = new MyNode(); //Node 2 is created and attached to the list
       node2.data = 15;
       node1.next = node2; // node 2 is attached to next field in node 1
      
       obj.print();
   }
}

For test purpose entire things are written

Add a comment
Know the answer?
Add Answer to:
Assume a class exists named MyList that has contained in it a data attribute of type...
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
  • 1. Create a class MLL, a singly linked, non-circular list where each node only has one...

    1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • can you solve it in java please Create the following: 1. Class Invoice ( the node...

    can you solve it in java please Create the following: 1. Class Invoice ( the node ) that includes three instance variables:     int No; // the Invoice No             String CustName; // the Customer name             int Amount; // the Invoice Amount Invoice next; // points to the next Invoice Default and overloaded constructors 2. Class Shop that includes three instance variables: Invoice head; Invoice Tail; Your class should have the following: • A method that initializes the instance variables....

  • Programming language: Java Design an application that has three classes. The first one, named Book describes...

    Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length,...

    WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • Complete with javadoc comments for constructor and setter/getter methods Overview The purpose of this lab is...

    Complete with javadoc comments for constructor and setter/getter methods Overview The purpose of this lab is to implement a generic Singly Linked Lists using a generic Node UML for Node<E> and SinglyLinked List< E> Node<E> Node<E> -data:E -next: Node<E> +Node( initialData: E, initialNext :Node<E> ) getData():E + getNext):Node<E> setData( newData : E ):void + setNext( newNext :Node<E>):void + SinglyLinkedList<E> SinglyLinkedList<E -head: Node< E> -tail: Node<E> -numElements : int SinglyLinkedList ( ) getSize():int appendList(newElement : E):void prependList(newElement E):void exists( target: E...

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