Question

In: Computer Science

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)
   {
       this.head = new Node(gdata);
   }
  
   public void Insertend(int gdata)
   {
       Node current = this.head;

       while(current.next!= null)
       {
           current = current.next;
       }
      
       Node newnode = new Node(gdata);
       current.next = newnode;
      
   }
  
   public void Listprint()
   {
       Node current = this.head;

       while(current!= null)
       {
           System.out.print(current.data + " ");
           current = current.next;
       }
       System.out.println();
   }
  
   public void Removemin() {
   // Complete this method to remove the minimum value in a linkedlist
      
      
   }
  
   public static void main(String[] args) {
      
       LinkedList exlist = new LinkedList(8);
      
       exlist.Insertend(1);
       exlist.Insertend(5);
       exlist.Insertend(2);
       exlist.Insertend(7);
       exlist.Insertend(10);
       exlist.Insertend(3);
      
       exlist.Listprint();
       //output: 8 1 5 2 7 10 3
      
       exlist.Removemin();
      
       exlist.Listprint();
       //output should be: 8 5 2 7 10 3
      
      
   }
}

Solutions

Expert Solution

// do comment if any problem arises

//code

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) {

        this.head = new Node(gdata);

    }

    public void Insertend(int gdata) {

        Node current = this.head;

        while (current.next != null) {

            current = current.next;

        }

        Node newnode = new Node(gdata);

        current.next = newnode;

    }

    public void Listprint() {

        Node current = this.head;

        while (current != null) {

            System.out.print(current.data + " ");

            current = current.next;

        }

        System.out.println();

    }

    public void Removemin() {

        // find node containing minimum element

        Node temp = head;

        boolean flag = true;

        Node min = null;

        Node current_min = temp;

        while (temp.next != null) {

            if (current_min.data > (temp.next).data) {

                min = temp;

                current_min = temp.next;

            }

            temp = temp.next;

        }

        // delete that node

        if (min == null) {

            head = head.next;

        }

        // if minimum node is some other node then simply shift next of minimun to next

        // of next of minimum

        else {

            min.next = min.next.next;

        }

    }

    public static void main(String[] args) {

        LinkedList exlist = new LinkedList(8);

        exlist.Insertend(1);

        exlist.Insertend(5);

        exlist.Insertend(2);

        exlist.Insertend(7);

        exlist.Insertend(10);

        exlist.Insertend(3);

        exlist.Listprint();

        // output: 8 1 5 2 7 10 3

        exlist.Removemin();

        exlist.Listprint();

        // output should be: 8 5 2 7 10 3

    }

}

Output:


Related Solutions

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 ==...
get the minimum element from linked list c++
get the minimum element from linked list c++
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...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
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; } } }
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Using Doubly Linked List, create a java code that does the following Without using LinkedList from...
Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL. ( as a METHOD...
method to remove all elements frrom my linked list (java)
method to remove all elements frrom my linked list (java)
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT