Question

Data Structures

Java Language

Q2) Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, popinclude also (Test class ) to test the methods

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

// If u have any doubt feel free to ask in the comments

// I will be very happy to solve them

// Standard libraries
import java.util.*; 
import java.util.Scanner; 
public class Main 
{ 
        Queue<Integer> q = new LinkedList<Integer>(); 
        // Push operation 
        void push(int val) 
        { 
                // get previous size of queue 
                int size = q.size(); 
                
                // Add current element 
                q.add(val); 
                // Removing all previous elements and adding them after this element 
                for (int i = 0; i < size; i++) 
                { 
                        int x = q.remove(); 
                        q.add(x); 
                } 
        }
        // Removes the top element i.e pop() 
        int pop() 
        { 
                if (q.isEmpty()) 
                { 
                        System.out.println("No elements"); 
                        return -1; 
                } 
                int x = q.remove(); 
                return x; 
        } 
    void peekMethod()
    {
      if(q.isEmpty())
          System.out.println("Underflow");
      else
          System.out.println("Topmost element is without removing it : "+q.peek());}

        // Returns top of stack 
        int top() 
        { 
                if (q.isEmpty()) 
                        return -1; 
                return q.peek(); 
        } 
        // Returns true if Stack is empty else false 
        boolean isEmpty() 
        { 
                return q.isEmpty(); 
        } 
        // Test class to test all above methods
        public static void main(String[] args) 
        { 
                Main s = new Main(); 
                s.push(11); // calling push function for inserting a new element
                s.push(26); 
        s.peekMethod();
                System.out.println("Top element :" + s.top()); 
                s.pop(); 
                s.push(98); 
                s.pop(); 
                System.out.println("Top element :" + s.top()); 
        } 
} 

SCREENSHOTS:

8 14 Main.java 1 // Standard Libraries 2 import java.util.*; 3 import java.util.Scanner; 4 public class Main 5-{| 6 Queue Int

31 32 33 34 35 36 37 38 39 40 41 42 43 return x; } // Returns top of stack int top() { if (q.isEmpty()) return -1; return q.OUTPUT:

Top element :26 Top element :11 .Program finished with exit code 0 Press ENTER to exit console.

Complexity Analysis::::::::

Time Complexity-----

push() function is taking O(n) time while pop() and top() functions require constant time only i.e. O(1). Because for pushing an element we remove and add the number of elements which were already present in it. This makes the operation to run in polynomial time.

Space Complexity-----

O(n) because we are using space to store n number of elements.

Add a comment
Know the answer?
Add Answer to:
Data Structures Java Language include also (Test class ) to test the methods Q2) 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...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

  • In Java language: 1. The complement of a queue is a stack. It uses first-in, last-out...

    In Java language: 1. The complement of a queue is a stack. It uses first-in, last-out accessing and is often likened to a stack of plates. The first plate put on the table is the last plate used. Create a stack class called Stack that can hold characters. Call the methods that access the stack push( ) and pop( ). Allow the user to specify the size of the stack when it is created. Keep all other members of the...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Please Answer this question using the language of C++. I provide you with the picture of figure 1...

    Please Answer this question using the language of C++. I provide you with the picture of figure 18_02. Thank you. I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • 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...

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

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