In: Computer Science
Laboratory Tasks
public class LinkedList {
Node head;
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
}
Complete the above java program by adding the following methods:
Part1:
Hello, I am putting the whole code below. please go through it and if you have any doubt, feel free to ask in comment and If you like this answer, give me a thumb up.
Thank you, Enjoy you answer.
// Java code
class Main
{
Node head; // head of list
class Node
{
int data;
Node next;
Node(int d){
data = d;
next = null; }
}
/* Inserts a new Node at front of the list.
*/
public void insertFirst(int new_data)
{
Node new_node = new
Node(new_data);
// Make next of new Node as
head
new_node.next = head;
// Move the head to point to new
Node
head = new_node;
}
/* Inserts a new node after the given prev_node.
*/
public void insertAfter(Node prev_node, int
new_data)
{
// Check if the given Node is
null
if (prev_node == null)
{
System.out.println("The given previous node cannot be null");
return;
}
Node new_node = new Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
// insertLasts a new node at the end.
public void insertLast(int new_data)
{
Node new_node = new
Node(new_data);
// Check if linked list is null or not
if (head == null)
{
head = new
Node(new_data);
return;
}
// As we are inserting at last, next node will be null
new_node.next = null;
Node last = head;
// If list is not empty, We have to
go at last node
while (last.next != null)
last =
last.next;
// when we reach at last, Add it there
last.next = new_node;
return;
}
/* This function prints contents of linked list
starting from
the given node */
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode =
tnode.next;
}
System.out.println();
}
void printMiddle()
{
Node slow_ptr = head;
Node fast_ptr = head;
if (head != null)
{
while (fast_ptr != null && fast_ptr.next != null)
{
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
System.out.println("The middle element is " + slow_ptr.data);
}
}
boolean isEmpty()
{
// if we found the head as null, which means it is empty list
if(head == null){
return true;
}
return false;
}
void deleteFirst()
{
// Move the head pointer to the next node
Node temp = head;
head = head.next;
}
public int getLength()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
public static void main(String[] args)
{
/* Start with the empty list
*/
Main llist = new Main();
// As List is empty, It will return true
System.out.println("isEmpty? :" + llist.isEmpty());
llist.insertFirst(7);
// Link list is: 7
llist.insertLast(4);
// Link list is: 7 -> 4
llist.insertFirst(1);
// Link list is: 1 -> 7 ->
4
llist.insertLast(6);
// Link list is: 1 -> 7 -> 4
-> 6
llist.insertLast(10);
// Link List is: 1 -> 7 -> 4 -> 6 -> 10
System.out.println("\nCreated Linked list is: ");
llist.printList();
// Trying to print middle
llist.printMiddle();
// will delete first
llist.deleteFirst();
System.out.println("\nCreated
Linked list is: ");
llist.printList();
System.out.println("Length of Linked list is: "+
llist.getLength());
}
}