Question

Software Data Structure & Algorithms 1- Write a java method that reversing an array using a...

Software Data Structure & Algorithms

1- Write a java method that reversing an array using a stack. Use ArrayStack().

2- Write a java method that implement matching delimiters. Use LinkedStack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The java code for java method that reverses an array using a stack which is implemented by the array is as follows.

import java.util.Arrays;

public class Arrstack {
   public static void main(String args[]){
       // Taking a array with already assigned values
       int[] myArray = {1,2,3,4,5};
       Arrstack arrstack = new Arrstack();
       // Calling the reverse array fucntion
       arrstack.reverse_array(myArray);
   }
    void reverse_array(int arr[]){


        System.out.println("The array elements before reversing::"+ Arrays.toString(arr));
        //Defining the stack using array implementation
        Stack stack = new Stack();

        for(int i = 0 ;i<arr.length;i++){
            // pushing each element of array into stack
            stack.push(arr[i]);
        }

        int reverse_arr[] = new int[arr.length];
        for (int i = 0 ;i<arr.length;i++){
            reverse_arr[i]=stack.pop();
        }
        System.out.println("The array elements after reversing by using arraystack::"+ Arrays.toString(reverse_arr));
    }
}
class Stack
{
    static final int MAX = 1000;
    int top;
    int arr[] = new int[MAX]; // Maximum size of Stack

    boolean isEmpty()
    {
        return (top < 0);
    }
    Stack()
    {
        top = -1;
    }

    boolean push(int x)
    {
        if (top >= (MAX-1))
        {
            System.out.println("Stack Overflow");
            return false;
        }
        else
        {
            arr[++top] = x;
            System.out.println(x + " pushed into stack");
            return true;
        }
    }

    int pop()
    {
        if (top < 0)
        {
            System.out.println("Stack Underflow");
            return 0;
        }
        else
        {
            int x = arr[top--];
            return x;
        }
    }
}

The output of the program is:

Kindly can you ask the second question of matching delimiters separately? As the coding for that has to be done separately but not in the same program.

I hope you got the answer to the first question and understand it.

Thank you:):)

Add a comment
Know the answer?
Add Answer to:
Software Data Structure & Algorithms 1- Write a java method that reversing an array using a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • ---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers,...

    ---Using Java to solve--- Write the code for three sort algorithms. Sort an array of Integers, strings and an array of Cars using each technique. At least seven items in each case.

  • STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, ...

    STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, in which they arrive. Suppose during execution, an element of the stack becomes least important in terms of priority and has to be but at the bottom of the stack. Method Signature: public void decreasePriority(<AnyType> el) Write an operator that places a certain element of the stack at the bottom of the stack.

  • ========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so...

    ========================Data Structure C++============================ 1. Write a CLIENT program that creates a stacks fo type int (so one stack instance):  aStack. Ask the user for 5 ints and push them onto aStack. Find a way to print the values in aStack in from the bottom to the top (so display the top LAST) - so if aStack was 1,2,3,4,5 with 5 at the top and 1 at the bottom you want it to display 1,2,3,4,5 (you don't want it to display 5,4,3,2,1...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • 1. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both...

    1. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both array Queue and linked list stack. Explain the steps then use pseudocode to explain the implementation. Then use C++, C# or Java to implement the method and test it. Use diagrams to explain the logic to return a reversed Queue. Provide solutions for both array Queue and linked list stack. Explain the steps then use pseudocode to explain the implementation. Then use C++ to...

  • 3) Stacks vs Queues: a) A Circular Array is a common data structure for a buffer...

    3) Stacks vs Queues: a) A Circular Array is a common data structure for a buffer (a type of queue). Why is this advantageous over a regular array? (Hint: think about common queue operations and what they do to an array). b) Show a traversal of the following tree using a stack: 26 1 25 2 17 3 2. (19 7 1 (The numbers under the bubbles are a node number, not the data. ) c) Why would using a...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • Please use Java. Write a non-recursive method that uses a stack to check whether its string...

    Please use Java. Write a non-recursive method that uses a stack to check whether its string parameter is a palindrome. Write a program to test and demo your method. Fibonacci Numbers Write a method that uses a stack to determine the desired Fibonacci number. Write a program to test and demo your method. Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT