In: Computer Science
Remove the Head element from the code below:
public class LinkedList
{
class Node{
int value;
Node nextElement;
public Node(int value) {
this.value = value;
this.nextElement = null;
}
}
public Node first = null;
public Node last = null;
public void addNewNode(int element) {
Node newValueNode = new Node(element);
if(first == null) {
first = newValueNode;
}
else {
last.nextElement = newValueNode;
}
last = newValueNode;
}
public void displayValues() {
Node recent = first;
if(first == null) {
System.out.println("List is Empty ");
return;
}
System.out.println("Nodes : ");
while(recent != null) {
System.out.print(recent.value + " ");
recent = recent.nextElement;
}
System.out.println();
}
public int getPeek(){
Node temp = first;
return temp.value;
}
public int lengthOfLinkedList()
{
Node temp = first;
int count = 0;
while (temp != null)
{
temp = temp.nextElement;
count++;
}
return count;
}
public void deleteValue(int key)
{
// Store the head node
Node temp = first, prev = null;
if (temp != null && temp.value == key)
{
first = temp.nextElement;
return;
}
// search for key to be deleted
while (temp != null && temp.value != key)
{
prev = temp;
temp = temp.nextElement;
}
if (temp == null) return;
prev.nextElement = temp.nextElement;
}
public static void main(String[] args) {
LinkedList valueList = new LinkedList();
valueList.addNewNode(1);
valueList.addNewNode(2);
valueList.addNewNode(3);
valueList.addNewNode(4);
valueList.addNewNode(5);
System.out.println("The LinkedList Consist Of The Following :" +
valueList.lengthOfLinkedList());
valueList.displayValues();
valueList.deleteValue(3);
System.out.println("After Deleting, The LinkedList Consist Of :" +
valueList.lengthOfLinkedList());
valueList.displayValues();
System.out.println("1st Item : " + valueList.getPeek());
}
}
CODE:
//Here the head element is 1.
public class LinkedList
{
class Node{
int value;
Node nextElement;
public Node(int value) {
this.value = value;
this.nextElement = null;
}
}
public Node first = null;
public Node last = null;
public void addNewNode(int element) {
Node newValueNode = new Node(element);
if(first == null) {
first = newValueNode;
}
else {
last.nextElement = newValueNode;
}
last = newValueNode;
}
public void displayValues() {
Node recent = first;
if(first == null) {
System.out.println("List is Empty ");
return;
}
System.out.println("Nodes : ");
while(recent != null) {
System.out.print(recent.value + " ");
recent = recent.nextElement;
}
System.out.println();
}
public int getPeek(){
Node temp = first;
return temp.value;
}
public int lengthOfLinkedList()
{
Node temp = first;
int count = 0;
while (temp != null)
{
temp = temp.nextElement;
count++;
}
return count;
}
public void deleteValue(int key)
{
// Store the head node
Node temp = first, prev = null;
if (temp != null && temp.value == key)
{
first = temp.nextElement;
return;
}
// search for key to be deleted
while (temp != null && temp.value != key)
{
prev = temp;
temp = temp.nextElement;
}
if (temp == null) return;
prev.nextElement = temp.nextElement;
}
public static void main(String[] args) {
LinkedList valueList = new LinkedList();
valueList.addNewNode(1);
valueList.addNewNode(2);
valueList.addNewNode(3);
valueList.addNewNode(4);
valueList.addNewNode(5);
System.out.println("The LinkedList Consist Of The Following :" + valueList.lengthOfLinkedList());
valueList.displayValues();
valueList.deleteValue(3);
System.out.println("After Deleting, The LinkedList Consist Of :" + valueList.lengthOfLinkedList());
//Deleting the head element from the linked list
valueList.deleteValue(1);
//Printing the result
System.out.println("After Deleting, The LinkedList Consist Of :" + valueList.lengthOfLinkedList());
valueList.displayValues();
System.out.println("1st Item : " + valueList.getPeek());
}
}