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
  • Write a Client class with a main method that tests the data structures as follows: For...

    Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...

  • Develop a generic stack ADT class by once again implementing the StackInterface.java and use Queue object...

    Develop a generic stack ADT class by once again implementing the StackInterface.java and use Queue object as underlying ADT(implement stack using queue) Include two constructors: one that sets the capacity at 100 elements, and the other that allows the application level to provide a capacity value as an actual parameter with the constructor call. Also include a toString method. StackInterface : package interfaces; public interface StackInterface<E> { void push(E element); // add an element to the stack - always at...

  • Create a new Java Application that test MyStack by having the following methods in main class:...

    Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element 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 exchange the first element and the last element in a stack 4. A Boolean method to search for a value...

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

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

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

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