Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My Q is very clear, if u cann\'t solve it don\'t do it ---> DON\'T WAIST MY TIME

ID: 3829379 • Letter: M

Question

My Q is very clear, if u cann't solve it don't do it ---> DON'T WAIST MY TIME BY COPING MY CODE THAT I MENTIONED DOWN

PLZ mention every Q above its own method ----> THANK U

i will include my linkedlist class & node class & stack class down below (STACK - JAVA)

For the main method i want these Qs below (1-11) to be solve

PS: U can call some methods from the linked list + use the stack class to solve for Qs (1-10)

1. A method to generate a number of elements between two given values and save them in a stack.

2. A method to print a stack (10 elements per line). The original stack should remain as is after the print.

3. A method to return the number of elements on the stack.

4. A method to return if the stack is empty or not.

5. A method to search for a value in a stack. The stack should remain the same after the search is finished.

6. A method to print the second element of the stack and leave the original stack unchanged

7. A method to count the number of elements in a stack that are larger than a given value and leave the original

stack unchanged.

8. A method peekLowestElement to return the smallest element. The original stack should remain unchanged.

9. A method peekHighestElement to return the largest element. The original stack should remain unchanged.

10. A method to inverse a stack.

11. A method to make a copy of a stack into another stack

• In the main method test all your methods including the following cases:

! Create 3 Stacks S1, S2, S3

! Insert 20 integers between 20 and 60 (do not insert duplicates) into S1

! Insert 30 integers between 10 and 80 (do not insert duplicates) into S2.

-----------------------------------------------------------------------------------------------------------------------------

CLASS STACK

public class STACK {
     MyLinkedList stack = new MyLinkedList();
    private int size;

        public void push(Comparable item) {
        stack.addToEnd(item);
    }

    public boolean empty() {
        if (stack.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public Comparable pop() {
        Comparable item = (Comparable) stack.deleteLastNode();
        return item;
    }

    public Comparable peak() {
        Comparable item = pop();
        push(item);
        return item;
    }

---------------------------------------------------------------------------------------------------------------------

CLASS LINKEDLIST

public class MyLinkedList {

    private Node head;
    //1. Insert a node at the front of the list

    public void addFirst(Comparable data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            newNode.setNext(head);
            head = newNode;
        }
    }
    //add a Node in the end

    public void addToEnd(Comparable data) {

        Node newNode = new Node(data);
        Node current = head;

        if (head == null) {
            head = newNode;
        } else {
            while (current.getNext() != null) {
                current = current.getNext();
            }
            current.setNext(newNode);
        }
    }
    //2. Insert a Node in place, assuming that the list is ordered in ascending order.

    public void insertAt(Comparable data, int pos) throws Exception {
        Node current = head;
        Node newNode = new Node(data);
        if (pos < 0 || pos> size()) {
            throw new Exception();
        }
        if (pos == 0) {
            newNode.setNext(head);
            head = newNode;
        } else {
            for (int i = 1; i <= pos - 1; i++) {
                current = current.getNext();
            }
            newNode.setNext(current.getNext());
            current.setNext(newNode);
        }
    }

    //3. Delete node in front
    public Node deleteFirstNode() {
        if (head == null) {
            return null;
        }
        Node temp = head;
        head = head.getNext();
        return temp;
    }

    //4. Delete last node
    public Node deleteLastNode() {
        Node current = head;
        Node prev = null;
        // case 1 ... list is empty
        if (head == null) {
            return null;
        }
        // case 2 .. list has only one element .. no previous
        if (size() == 1) {
            return deleteFirstNode();

        }
        // case 3 .. list has more than one element
        while (current.getNext() != null) {
            prev = current;
            current = current.getNext();
        }
        prev.setNext(null);
        return current;
    }
    //5. Method to return the size of the list

    public int size() {
        int size = 0;
        for (Node n = head; n != null; n = n.getNext()) {
            size++;
        }
        return size;
    }

    //6. Method to find the node with the minimum value in the list – getMinimum()
    public Comparable getMinimum() {
        Node current = head;
        Comparable minimum = current.getData();
        while (current != null) {
            if (current.getData().compareTo(minimum) < 0) {
                minimum = current.getData();
            }
            current = current.getNext();
        }
        return minimum;
    }
    //Insert a Node in it's right place & correct order

    public void insertInProperPlace(Comparable data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else if (head.getNext() == null) {
            if (head.getData().compareTo(data) < 0) {
                addToEnd(data);
            } else {
                addFirst(data);
            }
        } else {
            Node current = head;
            Node prev = null;

            while (current != null && current.getData().compareTo(newNode.getData()) < 0) {
                prev = current;
                current = current.getNext();
            }
            if (current != null) {
                newNode.setNext(current);
                prev.setNext(newNode);
            } else {
                addToEnd(data);
            }
        }
    }
    // search for a Node

    public boolean search(Comparable data) {
        Node current = head;
        while (current != null) {
            if (current.getData().compareTo(data) == 0) {
                return true;
            }
            current = current.getNext();
        }
        return false;
    }

    // Display the maximum Node in the Linked List
    public Comparable getMaximum() {
        Node current = head;
        Comparable largest = current.getData();
        while (current != null) {
            if (current.getData().compareTo(largest) > 0) {
                largest = current.getData();
            }
            current = current.getNext();
        }
        return largest;
    }
    //Sorting the Linked List in a Decending order

    public void sortInDesc() {
        Node current = this.head;
        Node i = head;
        current = current.getNext();

        while (current != null) {
            i = head;
            while (i != current) {
                if (i.getData().compareTo(current.getData()) < 0) {
                    Comparable temp = current.getData();
                    current.setData(i.getData());
                    i.setData(temp);
                } else {
                    i = i.getNext();
                }
            }
            current = current.getNext();
        }
    }
    ////Sorting the Linked List in an Aecending order

    public void sortInAsc() {
        Node current = this.head;
        Node i = head;
        current = current.getNext();

        while (current != null) {
            i = head;
            while (i != current) {
                if (i.getData().compareTo(current.getData()) > 0) {
                    Comparable temp = current.getData();
                    current.setData(i.getData());
                    i.setData(temp);
                } else {
                    i = i.getNext();
                }
            }
            current = current.getNext();
        }

    }
    //Delete all element in MyLinkedList

    public void deleteAll() {
        head = null;
    }

    //display the linked list
    public void Display() {
        Node current = head;
        while (current != null) {
            System.out.println(current.getData().toString());
            current = current.getNext();
        }
    }

    //Check if the MyLinkedList is Empty
    public boolean isEmpty() {
        if (head == null) {
            return true;
        }
        return false;
    }

-----------------------------------------------------------------------------

CLASS NODE

public class Node {

     
    private Node prev;
    private Comparable data;
    private Node next;

    public Node(Comparable newData) {
        this(newData, null);
    }

    public Node(Comparable newData, Node n) {
        data = newData;
        next = n;
    }

    public Node(Node p, Comparable newData, Node n) {
        prev = p;
        data = newData;
        next = n;
    }

    public Node getPrev() {
        return prev;
    }

    public void setPrev(Node prev) {
        this.prev = prev;
    }

    public Object getData() {
        return data;
    }

    public void setData(Comparable data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }  

Explanation / Answer

Hi, I have implemented first 6 Questions.

Please repost to get answer of other remaining part of questions.

Question is too long.

Please let me know in case of any issue in first 6 Part.

I have also made small changes in Node and MyLinkedList class.

######## Node.java ######

public class Node {

  

private Node prev;

private Comparable data;

private Node next;

public Node(Comparable newData) {

this(newData, null);

}

public Node(Comparable newData, Node n) {

data = newData;

next = n;

}

public Node(Node p, Comparable newData, Node n) {

prev = p;

data = newData;

next = n;

}

public Node getPrev() {

return prev;

}

public void setPrev(Node prev) {

this.prev = prev;

}

public Comparable getData() {

return data;

}

public void setData(Comparable data) {

this.data = data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

  

@Override

public String toString() {

   return data.toString();

}

}

########### MyLinkedList.java ###########

public class MyLinkedList {

   private Node head;

   //1. Insert a node at the front of the list

   public void addFirst(Comparable data) {

       Node newNode = new Node(data);

       if (head == null) {

           head = newNode;

       } else {

           newNode.setNext(head);

           head = newNode;

       }

   }

   //add a Node in the end

   public void addToEnd(Comparable data) {

       Node newNode = new Node(data);

       Node current = head;

       if (head == null) {

           head = newNode;

       } else {

           while (current.getNext() != null) {

               current = current.getNext();

           }

           current.setNext(newNode);

       }

   }

   //2. Insert a Node in place, assuming that the list is ordered in ascending order.

   public void insertAt(Comparable data, int pos) throws Exception {

       Node current = head;

       Node newNode = new Node(data);

       if (pos < 0 || pos> size()) {

           throw new Exception();

       }

       if (pos == 0) {

           newNode.setNext(head);

           head = newNode;

       } else {

           for (int i = 1; i <= pos - 1; i++) {

               current = current.getNext();

           }

           newNode.setNext(current.getNext());

           current.setNext(newNode);

       }

   }

   //3. Delete node in front

   public Comparable deleteFirstNode() {

       if (head == null) {

           return null;

       }

       Node temp = head;

       head = head.getNext();

       return temp.getData();

   }

   //4. Delete last node

   public Comparable deleteLastNode() {

       Node current = head;

       Node prev = null;

       // case 1 ... list is empty

       if (head == null) {

           return null;

       }

       // case 2 .. list has only one element .. no previous

       if (size() == 1) {

           return deleteFirstNode();

       }

       // case 3 .. list has more than one element

       while (current.getNext() != null) {

           prev = current;

           current = current.getNext();

       }

       prev.setNext(null);

       return current.getData();

   }

   //5. Method to return the size of the list

   public int size() {

       int size = 0;

       for (Node n = head; n != null; n = n.getNext()) {

           size++;

       }

       return size;

   }

   //6. Method to find the node with the minimum value in the list – getMinimum()

   public Comparable getMinimum() {

       Node current = head;

       Comparable minimum = current.getData();

       while (current != null) {

           if (current.getData().compareTo(minimum) < 0) {

               minimum = current.getData();

           }

           current = current.getNext();

       }

       return minimum;

   }

   //Insert a Node in it's right place & correct order

   public void insertInProperPlace(Comparable data) {

       Node newNode = new Node(data);

       if (head == null) {

           head = newNode;

       } else if (head.getNext() == null) {

           if (head.getData().compareTo(data) < 0) {

               addToEnd(data);

           } else {

               addFirst(data);

           }

       } else {

           Node current = head;

           Node prev = null;

           while (current != null && current.getData().compareTo(newNode.getData()) < 0) {

               prev = current;

               current = current.getNext();

           }

           if (current != null) {

               newNode.setNext(current);

               prev.setNext(newNode);

           } else {

               addToEnd(data);

           }

       }

   }

   // search for a Node

   public boolean search(Comparable data) {

       Node current = head;

       while (current != null) {

           if (current.getData().compareTo(data) == 0) {

               return true;

           }

           current = current.getNext();

       }

       return false;

   }

   // Display the maximum Node in the Linked List

   public Comparable getMaximum() {

       Node current = head;

       Comparable largest = current.getData();

       while (current != null) {

           if (current.getData().compareTo(largest) > 0) {

               largest = current.getData();

           }

           current = current.getNext();

       }

       return largest;

   }

   //Sorting the Linked List in a Decending order

   public void sortInDesc() {

       Node current = this.head;

       Node i = head;

       current = current.getNext();

       while (current != null) {

           i = head;

           while (i != current) {

               if (i.getData().compareTo(current.getData()) < 0) {

                   Comparable temp = current.getData();

                   current.setData(i.getData());

                   i.setData(temp);

               } else {

                   i = i.getNext();

               }

           }

           current = current.getNext();

       }

   }

   ////Sorting the Linked List in an Aecending order

   public void sortInAsc() {

       Node current = this.head;

       Node i = head;

       current = current.getNext();

       while (current != null) {

           i = head;

           while (i != current) {

               if (i.getData().compareTo(current.getData()) > 0) {

                   Comparable temp = current.getData();

                   current.setData(i.getData());

                   i.setData(temp);

               } else {

                   i = i.getNext();

               }

           }

           current = current.getNext();

       }

   }

   //Delete all element in MyLinkedList

   public void deleteAll() {

       head = null;

   }

   //display the linked list

   public void Display() {

       Node current = head;

       while (current != null) {

           System.out.println(current.getData().toString());

           current = current.getNext();

       }

   }

   //Check if the MyLinkedList is Empty

   public boolean isEmpty() {

       if (head == null) {

           return true;

       }

       return false;

   }

}

######### STACK.java #######

import java.util.Random;

public class STACK {

   MyLinkedList stack = new MyLinkedList();

   private int size;

   public void push(Comparable item) {

       stack.addToEnd(item);

   }

   public boolean empty() {

       if (stack.size() == 0) {

           return true;

       } else {

           return false;

       }

   }

   public Comparable pop() {

       Comparable item = stack.deleteLastNode();

       return item;

   }

   public Comparable peak() {

       Comparable item = pop();

       push(item);

       return item;

   }

   //1

   public void addNumbersInRange(int n, int min, int max){

      

       Random random = new Random();

       int i = 0;

       while(i < n){

           int rand = random.nextInt(max-min+1) + min; // in range min - max (inclusive)

           if(! stack.search(rand)){ // checking for duplicate

               stack.addFirst(rand);

               i++;

           }

       }

   }

  

   //2.

   public void display(){

      

       int size = stack.size();

       // storing data to get it back in stack

       Comparable[] arr = new Comparable[size];

       int i = 0;

       int count = 0;

       while(i < size){

           Comparable item = stack.deleteFirstNode();

           System.out.print(item+" ");

           count++;

           arr[i] = item;

           i++;

           if(count == 10){

               count = 0;

               System.out.println();

           }

       }

      

       // preserving the stack

       for(i = size-1; i>=0; i++)

           stack.addFirst(arr[i]);

   }

  

   //3

   public int size(){

       return stack.size();

   }

  

   //4

   public boolean isEmpty(){

       return stack.isEmpty();

   }

  

   //5

   public boolean search(Comparable item){

       return stack.search(item);

   }

  

   //6

   public void printSecondElement(){

       if(size() < 2){

           System.out.println("Stack has fewer element");

       }else{

           Comparable item = stack.deleteFirstNode();

           Comparable item2 = stack.deleteFirstNode();

          

           System.out.println("Second element : "+item2);

          

           // adding popped elements

           stack.addFirst(item2);

           stack.addFirst(item);

        }

   }

  

  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote