Question

Q1. Add the following operation to the class stackType: void reverseStack (stackType<Type> &otherStack); This operation copie
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q-1
public class Main<T> {

private T data[];
private int top; // is the index of topmost element of stack

public Main(int capacity) {
data = (T[]) new Object[capacity];
top = -1;
}
public boolean isEmpty(){ //isEmpty Method
return (top == -1);
}
public int size(){ //size Method
return top + 1;
}
public T top() { //Top method
return data[top];
}
public void push(T elem) { //push Method
if(size()==data.length){
System.out.println(("Stack is full. Increasing the capacity."));
doubleCapacity();
}
top++;
data[top] = elem;
}
private void doubleCapacity() { //doubleCapacity Method
       T temp[] = data;
       data = (T[]) new Object[2 * temp.length];
       for(int i = 0; i <= top; i++){
           data[i] = temp[i];
       }
      
   }


public T pop() throws Exception { //pop Method
if(this.isEmpty()){
throw new Exception("Stack is empty.");
}
T temp = data[top];
top--;
return temp;

}
public void reverseStack(Main stack2)throws Exception { //reverseStack Method from stack1 to stack2
if(this.isEmpty()){
throw new Exception("Stack is empty.");
}
while(!this.isEmpty()){
stack2.push(this.pop());
}
}
public static void main(String[] args)throws Exception { //Main
Main<Integer> stack1= new Main<Integer>(20);   
for(int i = 1; i <= 20; i++){
stack1.push(i); //push 20 element into stack1
}
System.out.println("=====Stack1======");
if(stack1.isEmpty()){
throw new Exception("Stack is empty.");   
}
while(!stack1.isEmpty()){
System.out.println(stack1.pop()); //pop 20 element from stack1
}
for(int i = 1; i <= 20; i++){
stack1.push(i);
}

Main<Integer> stack2 =new Main<Integer>(20);
stack1.reverseStack(stack2);
System.out.println("=====Stack2======");
while(!stack2.isEmpty()){
System.out.println(stack2.pop()); //pop 20 element from stack2
}

}

}

OUTPUT:

=====Stack1====== 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 =====Stack2====== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Add a comment
Know the answer?
Add Answer to:
Q1. Add the following operation to the class stackType: void reverseStack (stackType<Type> &otherStack); This operation copies...
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
  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in...

    Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in Stack class and Queue interface. The Stack class is a generic class containing these methods: public void push(E value) - pushes a new value onto the Stack public E pop() - pops the next value off of the stack and returns it; throws EmptyStackExceptionl if stack is empty public E peek() - returns the next value on the Stack but does not pop it...

  • Dynamic MathStack The MathStack class shown in this chapter only has two member functions: add and...

    Dynamic MathStack The MathStack class shown in this chapter only has two member functions: add and sub . Write the following additional member functions: Function Description mult - Pops the top two values off the stack, multiplies them, and pushes their product onto the stack. div - Pops the top two values off the stack, divides the second value by the first, and pushes the quotient onto the stack. addAll - Pops all values off the stack, adds them, and...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

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

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