Question

In: Computer Science

Remove the Head element from the code below: public class LinkedList {    class Node{ int...

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());
}
}

Solutions

Expert Solution

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());

}

}



Related Solutions

Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp
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: isEmpty() checks if the linked list is empty or not. (return type is boolean) printList() prints all data in the linked list. (void method) insertFirst(int newData) add newData at the head of the linked list. (void method) insertLasL(int newData) add newData at...
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item;...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT