What's so good?

Fewer Lines of Code (5x typical; 13x here)

STACK in Prolog:

    push( Item ) :- asserta( stack_item(Item) ).
    pop( Item ) :- retract( stack_item(Item) ).
    is_empty()  :- not( stack_item(_) ).

STACK in C++

(from Lippman "C++ Primer")
  //Stack.H
  #ifndef STACK_H
  #define STACK_H

  typedef int Type;
  const int BOS = -1;  // Bottom of Stack

  class Stack {
  public:
      Stack( in sz = stack_size );
      ^Stack;
      is_empty() { return top == BOS; }
      is_full()  { return top == size-1; }
      void push( Type value );
      Type pop();

  private:
      int top;
      int size;
      Type *array;
  };
  #endif

  //Stack.C
  #include 
  #include "Stack.H"
  #include 
  #include 

  Stack::Stack( int sz )
  {
      array = new Type[ sz ];
      assert( array != 0 );
      top = BOS;
      size = sz;
  }

  Stack::~Stack()
  {
      delete [] array;
  }

  void Stack::push( Type value )
  {
      if ( is_full() )
      {
           cerr << "?? pop on empty stack" << endl;
           exit( -1 );
      }
      array[++top] = value;
  }

  Type Stack::pop()
  {
      if ( is_empty() )
      {
          cerr << "?? pop on empty stack" << endl;
          exit( -1 );
      }
      return array[top--];
  }

Previous Next