Ok so I have to write a reverse method of a stack and I am not quite sure how to do it. I understand that i need to pop the strings from one stack into another but whenever I try to do this errors arise. Specifically it says it cannot find the pop method which is clearly defined above. Can anyone help with this problem?

import java.io.*;
import java.util.*;
  
  public class MyStack<E> implements MyStackInterface<E> {
    
    private List<E> theData;
    
    public MyStack() {
      theData = new ArrayList<E>();
    }
    
    Stack<String> theDataReversed = new Stack<String>();
    
    public E push(E o) {
    theData.add(o);
    return o;
  }
  
  public E peek() {
    if(empty()){
      throw new EmptyStackException();
    }
    return theData.get(theData.size() - 1);
  }
  
  public E pop() {
    if(empty()) {
      throw new EmptyStackException();
    }
      return theData.remove(theData.size() - 1);
    }
    
  public boolean empty() {
    return theData.size() == 0;
  }
  
  public MyStack<E> reverse() {

    while(theData.size() != 0){
      theDataReversed.add(0, theData.pop());
    }
    }
  }

theData is a List. You should just need to call pop()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.