Question: Integer beeCount is read from input as the number of input values that follow. The node headBee is created with the value of beeCount. Use
Integer beeCount is read from input as the number of input values that follow. The node headBee is created with the value of beeCount. Use scnr.nextInt() to read beeCount integers. Insert a BeeNode for each integer at the end of the linked list.
Ex: If the input is 2 47 58, then the output is:
2 47 58
BeelinkedList.Java
import java.util.Scanner;
public class BeeLinkedList { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); BeeNode headBee = null; BeeNode currBee = null; BeeNode lastBee = null; int beeCount; int inputValue; int i;
beeCount = scnr.nextInt(); headBee = new BeeNode(beeCount); lastBee = headBee; /* Your code goes here */
currBee = headBee; while (currBee != null) { currBee.printNodeData(); currBee = currBee.getNext(); } } }
BeeNode.java
class BeeNode { private int babiesVal; private BeeNode nextNodeRef;
public BeeNode(int babiesInit) { this.babiesVal = babiesInit; this.nextNodeRef = null; }
public void insertAfter(BeeNode nodeLoc) { BeeNode tmpNext = null;
tmpNext = this.nextNodeRef; this.nextNodeRef = nodeLoc; nodeLoc.nextNodeRef = tmpNext; }
public BeeNode getNext() { return this.nextNodeRef; }
public void printNodeData() { System.out.println(this.babiesVal); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
