The Stack interface definition

  • Important facts:

    1. The stack only defines a behavior on the access of the data stored in a stack:

      • pop( ) must return the earliest item that was pushed

    2. The stack does not specify how the data must be stored

    3. There are different ways to implement the same behavior !!!

  • To accommodate different stack implementations, we can define a Stack interface

  • Then any class that implements the behavior (= methods) in the Stack interface can be used as a Stack "object":

      Stack a = new ClassThatImplementsStack() ;
    

A sample Stack interface definition

  • Sample Stack interface definition:

       interface MyStackInterface<E>
       {
          boolean isEmpty(); // returns true is stack is empty
          boolean isFull();  // returns true is stack is full
    
          void push(E e);    // Pushes elem e on the stack
    
          E pop();           // Remove the elem at the top
                             // of the stack and return it
    
          E peek();          // Return the elem at the top 
                             // without removing it
       }