Question

In: Computer Science

Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from...

Java Data Structures (Stack and Recursion)

Using the CODE provided BELOW (WITHOUT IMPORTING any classes from Java Library) modify the classes and add the following methods to the code provided below.

1. Add a recursive method hmTimes() to the CODE BELOW that states how many times a particular value appears on the stack.

2. Add a recursive method insertE() to the CODE BELOW that allows insert a value at the end of the stack.

3. Add a recursive method popLast() to the CODE BELOW that allows removal of the last node from the stack.

4. Add a recursive method hmNodes() to CODE BELOW that states how many nodes does the stack have.


Prove that every method works, MULTIPLE TIMES, in the MAIN StackWithLinkedList2.

CODE:

class Node {

  int value;

  Node nextNode;

  

  Node(int v, Node n)

  {

    value = v;

nextNode = n;

  }

  

  Node (int v)

  {

     this(v,null);

  }

}

class Stack {

  protected Node top;

  

  Stack()

  {

    top = null;

  }

  

  boolean isEmpty()

  {

    return( top == null);

  }

  void push(int v)

  {

    Node tempPointer;

    tempPointer = new Node(v);

tempPointer.nextNode = top;

top = tempPointer;

  }

  

  int pop()

  {

    int tempValue;

tempValue = top.value;

top = top.nextNode;

return tempValue;

  }   

  

  void printStack()

  {

    Node aPointer = top;

String tempString = "";

while (aPointer != null)

{

tempString = tempString + aPointer.value + "\n";

aPointer = aPointer.nextNode;

}

System.out.println(tempString);

  }

  

  boolean hasValue(int v)

  {

    if (top.value == v)

{

return true;

}

else

{

return hasValueSubList(top,v);

}

  }

  

  boolean hasValueSubList(Node ptr, int v)

  {

    if (ptr.nextNode == null)

{

return false;

}

else if (ptr.nextNode.value == v)

{

return true;

}

else

{

return hasValueSubList(ptr.nextNode,v);

}

  }

}

public class StackWithLinkedList2{

  public static void main(String[] args){

    int popValue;

    Stack myStack = new Stack();

myStack.push(5);

myStack.push(7);

myStack.push(9);

    System.out.println(myStack.hasValue(11));

  }

}

Solutions

Expert Solution

Recursion is the concept of forming a loop using a call of function within its own function body.

as we are calling a function again and again in its body it forms a loop and a memory stack as we use that function.

the code and solutioin that is asked in the question is provided below and is teested with several test cases

CODE:

class Node {

    int value;

    Node nextNode;



    Node(int v, Node n)
    {
        value = v;
        nextNode = n;
    }



    Node(int v)
    {
        this(v, null);
    }
}

class Stack {
    protected Node top;

    Stack()
    {
        top = null;
    }



    boolean isEmpty()
    {
        return (top == null);
    }

    void push(int v)
    {
        Node tempPointer;
        tempPointer = new Node(v);
        tempPointer.nextNode = top;
        top = tempPointer;
    }



    int pop()
    {
        int tempValue;
        tempValue = top.value;
        top = top.nextNode;
        return tempValue;
    }



    void printStack()
    {
        Node aPointer = top;
        String tempString = "";
        while (aPointer != null)
        {
            tempString = tempString + aPointer.value + "\n";
            aPointer = aPointer.nextNode;
        }
        System.out.println(tempString);
    }
    void hmNodes(){
        coutt(top,0);
    }
    void coutt(Node ptr,int cout){
        if (ptr.nextNode==null){
            System.out.println("the number of elements in stack are "+ cout);
            return;
        }
        cout++;
        coutt(ptr.nextNode,cout);
    }
    void popLast(){
        poped(top);
    }
    void poped(Node ptr){
        if (ptr.nextNode.nextNode==null){
            System.out.println(ptr.nextNode.value+"is the poped element from last");
            ptr.nextNode=null;
            return;
        }
        poped(ptr.nextNode);
    }
    void insertE(int val)
    {
        insertatend(top,val);
    }
    
    void insertatend(Node tra,int val){
        
        if(tra.nextNode==null){
            tra.nextNode=new Node(val);
            return;
        }
        insertatend(tra.nextNode,val);
    }
    
    void hmTimes(int val){
        int count = TIMES(top,val,0);
        System.out.println("the value "+val+" is present "+count+" times in stack");
    }
    
    int TIMES(Node ptr, int vall,int coun)
    {
        if(ptr.value==vall){
            coun++;
        }
        if (ptr.nextNode==null){
            return coun;
        }
        return TIMES(ptr.nextNode,vall,coun);
    }
    
    boolean hasValue(int v)
    {
        if (top.value == v)
        {
            return true;
        } 
        else
        {
            return hasValueSubList(top, v);
        }
    }


    boolean hasValueSubList(Node ptr, int v)
    {
        if (ptr.nextNode == null)
        {
            return false;
        } 
        else if (ptr.nextNode.value == v)
        {
            return true;
        } 
        else
        {
            return hasValueSubList(ptr.nextNode, v);
        }

    }

}

public class StackWithLinkedList2 {

    public static void main(String[] args) {

        int popValue;

        Stack myStack = new Stack();

        myStack.push(5);
        myStack.push(5);
        myStack.push(7);
        myStack.push(5);
        myStack.push(9);
        myStack.push(7);
        myStack.push(5);
        myStack.push(7);
        myStack.push(5);
        myStack.push(7);
        myStack.push(5);
        myStack.push(7);
        myStack.insertE(8);
        myStack.hmNodes();
        myStack.insertE(8);
        myStack.hmNodes();
        myStack.insertE(8);
        myStack.hmNodes();
        myStack.insertE(8);
        myStack.hmNodes();
        myStack.insertE(8);
        myStack.hmNodes();
        myStack.hmTimes(8);
        myStack.popLast();
        myStack.hmTimes(8);
        myStack.hmNodes();
    }

}

OUTPUT:


Related Solutions

Java code for creating an Inventory Management System of any company - designing the data structure(stack,...
Java code for creating an Inventory Management System of any company - designing the data structure(stack, queue, list, sort list, array, array list or linked list) (be flexible for future company growth Java code for creating an initial list in a structure. Use Array (or ArrayList) or Linkedlist structure whichever you are confident to use. - Implement Stack, Queue, List type structure and proper operation for your application. Do not use any database. All data must use your data structures....
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a linked list-based queue ADT that implements the provided QueueInterface.java You may not make any changes to StackInterface.java or QueueInterface.java The classes must be generic The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class Your implementations cannot throw any exceptions Each ADT must include a toString( ) method: The...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application also creates a "DisplayStackElement" and "DisplayQueueElement" routine. The application must be menu driven (with an option to terminate the application) and provide the following features. Allow insertion of a "Circle" object/structure in the Stack data structures. The Circle contains a "radius" data member. The Circle also uses functions/methods "setRadius", "getRadius" and calculateArea (returns a double data type). Allow insertion of a "Circle" object/structure in...
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...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from...
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from some input text file: Total number of characters used,without counting spaces and punctuation, total number of words used; Number of words, counting each word only once; Total number of punctuation characters;Number of words that are of size six or more;Number of words that are used only once
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS:...
JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS: a private double that holds the radius. CONSTRUCTORS a no argument constructor that sets the radius to zero a constructor that takes a single argument of type double which is the radius. METHODS a method called "getRadius" which returns the current radius a method called "setRadius" which takes a single parameter of type double containing the new radius a method called "getArea" which returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT