IN JAVA: Create a LinkedNode data type that includes the following data along wi
ID: 3595436 • Letter: I
Question
IN JAVA:
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
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)