Build off Java code provided at bottom: Create a LinkedNode data type that inclu
ID: 3604463 • Letter: B
Question
Build off Java code provided at bottom:
Create a LinkedNode data type that includes the following data along with the appropriate getters/setters/constructors/toString:
name (String)
age (int)
favColor (String)
next (LinkedNode)
Create a testing program that uses only the methods you coded in LinkedNode and does the following (in this specific order):
1.create a reference variable to indicate the beginning of your list
2.create and link 4 new nodes so that you basically have a singly linked list
3.display the list (create a method that uses a loop to do this)
4.add 1 more node
5.display the list
6.delete node 3
7.display the list
8.delete the last node
9.display the list
Draw your final list on paper
public class LinkedNode {
private String name;
private int age;
private String favColor;
private LinkedNode next;
LinkedNode(){
}
LinkedNode( String name, int age, String favColor){
this.name = name;
this.age = age;
this.favColor = favColor;
this.next = null;
}
/**
*
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the favColor
*/
public String getFavColor() {
return favColor;
}
/**
* @param favColor the favColor to set
*/
public void setFavColor(String favColor) {
this.favColor = favColor;
}
/**
* @return the next
*/
public LinkedNode getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(LinkedNode next) {
this.next = next;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "LinkedNode [name=" + name + ", age=" + age + ", favColor=" + favColor + ", next=" + next + "]";
}
}
////test class
public class TestLinkedNode {
/**
* @param args
*/
public static void main(String[] args) {
LinkedNode temp = new LinkedNode("Bob", 20, "red");
LinkedNode node1 = new LinkedNode("James", 22, "green");
LinkedNode node2 = new LinkedNode("Luke", 55, "blue");
LinkedNode node3 = new LinkedNode("Ted", 77,"pink" );
LinkedNode node4 = new LinkedNode("John", 5, "yellow");
temp.setNext(node1);
node1.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
System.out.println(temp.toString());
}
public static void display(LinkedNode g) {
while(g.getNext()!=null)
{
System.out.println(g.toString());
g = g.getNext();
}
}
}
Explanation / Answer
package chegg;
import java.util.Scanner;
class Node {
protected int age;
protected String favColor;
protected Node next;
public Node(int age, String favColor, Node next) {
super();
this.age = age;
this.favColor = favColor;
this.next = next;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFavColor() {
return favColor;
}
public void setFavColor(String favColor) {
this.favColor = favColor;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size ;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void insertAtStart(int age, String favColor)
{
Node nptr = new Node(age, favColor, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setNext(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void insertAtEnd(int age, String favColor)
{
Node nptr = new Node(age, favColor, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setNext(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(int age, String favColor, int pos)
{
Node nptr = new Node(age, favColor, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getNext();
ptr.setNext(nptr);
nptr.setNext(tmp);
break;
}
ptr = ptr.getNext();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getNext();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getNext();
}
end = t;
end.setNext(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getNext();
tmp = tmp.getNext();
ptr.setNext(tmp);
break;
}
ptr = ptr.getNext();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getNext() == null)
{
System.out.println(start.getAge()+"-----"+start.getFavColor());
return;
}
Node ptr = start;
System.out.print(start.getAge()+"-"+start.getFavColor()+ "->");
ptr = start.getNext();
while (ptr.getNext() != null)
{
System.out.print(ptr.getAge()+"-"+ptr.getFavColor()+ "->");
ptr = ptr.getNext();
}
System.out.print(ptr.getAge()+"-"+ptr.getFavColor()+ " ");
}
}
/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Singly Linked List Operations ");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
System.out.println("7. Display");
System.out.println("Enter your choice ");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter age ");
int ages= scan.nextInt();
System.out.println("Enter favcolor");
String fcolor=scan.next();
System.out.println("Enter integer element to insert");
list.insertAtStart(ages,fcolor);
break;
case 2 :
System.out.println("Enter age");
int ags= scan.nextInt();
System.out.println("Enter favcolor");
String fclor=scan.next();
list.insertAtEnd( ags,fclor );
break;
case 3 :
System.out.println("Enter age");
int aggs= scan.nextInt();
System.out.println("Enter favcolor");
String fcclor=scan.next();
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position ");
else
list.insertAtPos(aggs,fcclor, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position ");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" ");
break;
case 7 :
System.out.println("Wrong Entry ");
break;
default:
System.out.println("selected wrong choice");
break;
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
output:
Singly Linked List Test
Singly Linked List Operations
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display
Enter your choice
1
Enter age
22
Enter favcolor
green
Enter integer element to insert
Singly Linked List = 22-----green
Do you want to continue (Type y or n)
y
Singly Linked List Operations
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display
Enter your choice
7
Wrong Entry
Singly Linked List = 22-----green
Do you want to continue (Type y or n)
y
Singly Linked List Operations
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display
Enter your choice
3
Enter age
33
Enter favcolor
black
Enter position
4
Invalid position
Singly Linked List = 22-----green
Do you want to continue (Type y or n)
y
Singly Linked List Operations
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display
Enter your choice
7
Wrong Entry
Singly Linked List = 22-----green
Do you want to continue (Type y or n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.