Hey guys,
i jsut got this assignment and i have 1 week to implement it but i have no idea where to start. The link to the question is http://www.cs.sfu.ca/~mitchell/cmpt-126/a2/a2.pdf
and i have also attached it as a pdf.
the Stack class refered to in the assignment is

class Stack <T> {

  // top of stack
  private Node top ; 

  // Constructor
  public Stack(){ top = null ; }

  // Class for Nodes of Linked List 
  private class Node{
    private T  value ;
    private Node  next ;
    public Node( T value, Node next ){ 
      this.value=value; 
      this.next=next; 
    }
    public T getValue(){ return value; } 
    public Node getNext(){ return next; }
  }

  // returns true iff stack is empty
  public boolean empty(){ 
    return top==null; 
  }

  // pushes <newValue> onto the stack
  public void push( T newValue ){ 
    top = new Node( newValue, top ); 
  }

  // pops top off the stack and returns it
  public T pop(){ 
    T temp = top.getValue();
    top = top.getNext();
    return temp ;
  }

  // returns the top element without popping,
  // returns null if stack is empty
  public T peek(){
    if( !empty() ){
      return top.getValue();
    }else{
      return null ;
    }
  }

  // main method for testing Stack
  public static void main(String [] args){
    System.out.println("Stack Tests");     
    Stack <String> ss = new Stack<String>();
  
    System.out.print( ss.empty() ? "OK" : "not empty!");
    ss.push( "abc" );
    System.out.print( ss.empty() ? "empty!" : "OK" );
    System.out.print( ss.peek().equals("abc") ? "OK" : "top not ``abc''!" ); 
    ss.push( "def" );
    System.out.println( "\n" + ss );
    System.out.print( ss.peek().equals("def") ? "OK" : "top not ``def''!" ); 
    System.out.print( ss.pop().equals("def") ? "OK" : "popped not ``def''!" ); 
    System.out.print( ss.empty() ? "empty!!" : "OK" );
    System.out.print( ss.pop().equals("abc") ? "OK" : "popped not ``abc''!" ); 
    System.out.print( ss.empty() ? "OK" : "not empty!!" );
    System.out.print( ss.peek()==null ? "OK" : "peek not null!" );
    System.out.println( "\n" + ss );

    Stack <Integer> si = new Stack<Integer>();
    si.push(1);
    si.push(2);
    System.out.println( si );
  }

}

if you can gimme anything about how to start this i would be very thnak full.

Thanks in advance.

Recommended Answers

All 3 Replies

start by reading.
we're not going to do your homework for you.

i didnt want anyone to do the hw sorry if it sounded that way. The problem i have is trying to tokenize the entry. i get stuck since the user an type it as 23+5 or 23 + 5. i tried using the hasNext and soo on from the scanner function but it did not seem to work. jsut looking for an idea on how to go about this

split the string on the operator and strip whitespace from the resulting substrings.

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.