In: Computer Science
Assume that a singly linked list is implemented with a
header node, but no tail node, and that it maintains only a pointer
to the header node. Write a class that includes methods to
a. return the size of the linked list
b. print the linked list
c. test if a value x is contained in the linked list
d. add a value x if it is not already contained in the
linked list
e. remove a value x if it is contained in the linked
list
All the methods are testes using switch case
Source Code
import java.util.*;
public class SinglyLinkedList {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public int size() {
Node current = head;
int c=0;
if(head == null) {
return 0;
}
while(current != null) {
c++;
current = current.next;
}
return c;
}
public void print() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public int test(int x) {
Node current = head;
int f=0;
while(current != null) {
if(current.data==x)
f=1;
current = current.next;
}
return f;
}
public void addtest(int x) {
Node newNode = new Node(x);
if(test(x)==0)
{
if(head==null)
{
head = newNode;
}
Node last = head;
while (last.next != null)
last = last.next;
last.next = newNode;
newNode.next=null;
}
else
{
System.out.println("Already present");
}
}
public void deleteNode(int key)
{
Node temp = head, prev = null;
if (temp != null && temp.data == key)
{
head = temp.next;
return;
}
while (temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
if (temp == null)
return;
prev.next = temp.next;
}
public static void main() {
SinglyLinkedList sl = new SinglyLinkedList();
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("Enter 1 to return size of linked list\nEnter 2 to print the linked list\nEnter 3 to test if value x is contained in linked list\nEnter 4 to add a value x if it is not already contained in linked list\nEnter 5 to remove a value x if it is contained in the list");
System.out.println("Enter 0 to exit");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 0:System.exit(0);
case 1:System.out.println(sl.size());break;
case 2:sl.print();break;
case 3:System.out.println("Enter value to test");
int x=sc.nextInt();
int f=sl.test(x);
if(f==1)
System.out.println("Value "+ x +" is contained in the linked list");
else
System.out.println("Value is not contained in the linked list");
break;
case 4:System.out.println("Enter value to add");
int a=sc.nextInt();
sl.addtest(a);break;
case 5:System.out.println("Enter value to remove");
int b=sc.nextInt();
sl.deleteNode(b);break;
default:System.out.println("Wrong Input");break;
}
}
}
}
Output
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
0
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
List is empty
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
4
Enter value to add
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
Nodes of singly linked list:
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
4
Enter value to add
2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
Nodes of singly linked list:
1 2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
3
Enter value to test
1
Value 1 is contained in the linked list
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
3
Enter value to test
3
Value is not contained in the linked list
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
3
Enter value to test
2
Value 2 is contained in the linked list
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
5
Enter value to remove
2
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
2
Nodes of singly linked list:
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
1
1
Enter 1 to return size of linked list
Enter 2 to print the linked list
Enter 3 to test if value x is contained in linked list
Enter 4 to add a value x if it is not already contained in linked list
Enter 5 to remove a value x if it is contained in the list
Enter 0 to exit
Enter your choice
0