Edit and complete this linked list code to ASK and DISPLAY, how many members, 1st, middle, lastname, area code, tel number, gender, age
import java.io.*;
public class FinalNode {
public int data;
public FinalNode next;
public static FinalNode firstNode;
public static FinalNode lastNode=null;
public FinalNode(int d,FinalNode n)
{
data=d;
next=n;
}
public static void main(String[] args) throws IOException{
int numnodes, num;
BufferedReader s= new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many nodes to input? ");
numnodes = Integer.parseInt(s.readLine());
for(int i=0;i<numnodes;i++){
System.out.print("Enter node"+(i+1)+":");
num = Integer.parseInt(s.readLine());
FinalNode n =new FinalNode(num, null);
if(lastNode!=null)
{
lastNode.next=n;
lastNode=n;
}
else
{
firstNode=n;
lastNode=n;
}
}
System.out.print("entered nodes: ");
FinalNode n=firstNode;
while(n!=null)
{
System.out.print(n.data+"\t");
n=n.next;
}
System.out.println();
}
}
import java.io.*;
public class FinalNode {
public String lastName;
public String areaCode;
public String telNumber;
public String gender;
public int age;
public FinalNode next;
public static FinalNode firstNode;
public static FinalNode lastNode = null;
public FinalNode(String lastName, String areaCode, String telNumber, String gender, int age) {
this.lastName = lastName;
this.areaCode = areaCode;
this.telNumber = telNumber;
this.gender = gender;
this.age = age;
next = null;
}
@Override
public String toString() {
return "FinalNode{" +
"lastName='" + lastName + '\'' +
", areaCode='" + areaCode + '\'' +
", telNumber='" + telNumber + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) throws IOException {
int numNodes;
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many nodes to input? ");
numNodes = Integer.parseInt(s.readLine());
for (int i = 0; i < numNodes; i++) {
System.out.println("Enter node" + (i + 1) + ":");
System.out.print("Last name: ");
String lastName = s.readLine();
System.out.print("Area code: ");
String areaCode = s.readLine();
System.out.print("Tel number: ");
String telNumber = s.readLine();
System.out.print("Gender: ");
String gender = s.readLine();
System.out.print("Age: ");
int age = Integer.parseInt(s.readLine());
FinalNode n = new FinalNode(lastName, areaCode, telNumber, gender, age);
if (lastNode != null) {
lastNode.next = n;
} else {
firstNode = n;
}
lastNode = n;
}
System.out.println("Total: " + numNodes);
System.out.println("First: " + firstNode);
System.out.println("Entered nodes: ");
int i = 0;
for (FinalNode n = firstNode; n != null; n = n.next) {
if (numNodes / 2 == i++) {
System.out.println("The next node is a middle node:");
}
System.out.println(n);
}
}
}
Comments
Leave a comment