A simple generic class for constructing stacks in Java (later tham 1.5) and C# (later than 2.0)

The following is the specification of useful members of a Java (generic) stack handling class useful in developing translators. The closely equivalent C# (generic) version appears lower on this page.

The earlier versions of Java and C# did not have generic versions of these classes, but worked with elements of the Object class, which necessitated casting operations.

  import java.util.*;

  class Stack<E>
  // Class for constructing a stack of elements of type E in Java

    public Stack<E>()
    // Empty stack constructor

    public void push(E element)
    // Appends element to top of stack

    public E pop()
    // Retrieves an element from top of stack

    public E peek()
    // Returns element on top of stack without removing it

    public void clear()
    // Clears all elements from stack

    public int size()
    // Returns number of elements in stack

    public boolean empty()
    // Returns true if stack is empty

    public boolean contains(E element)
    // Returns true if element is in the stack

  } // Stack
Here is a sample program showing the use of this library
   import library.*;
   import java.util.*;

   class ShowGenericStack {
   // Demonstrate simple application of the generic Stack class in Java

     static class Entry {
       public String name;
       public int age;                                     // other fields could be added

       public Entry(String name, int age) {                // constructor
         this.name = name;
         this.age = age;
       }
     } // Entry

     static Stack<Entry> stack = new Stack<Entry>(); // global for simplicity here!

     public static void main (String[] args) {
     // Build a stack of people's names and ages
       IO.writeLine("Supply a list of people's names and ages.  CTRL-Z to terminate");
       do {
         String name = IO.readWord();
         int age = IO.readInt();
         IO.readLn();
         if (!IO.eof()) stack.push(new Entry(name, age));  // add to top of stack
       } while (!IO.eof());

       IO.writeLine(stack.size() + " items stored");       // report size of stack

       Entry patEntry = new Entry("Pat", 63);              // that fellow again!

       stack.push(patEntry);

       if (stack.contains(patEntry)) {                     // well, it must do!
         IO.writeLine("Pat is on top of it, as always!");
         IO.writeLine("Not bad for one who is only " + stack.peek().age);
       }

       while (!stack.empty())                              // names in reverse order
         IO.writeLine(stack.pop().name);

     } // main

   } // ShowGenericStack


The following is the specification of useful members of a C# 2.0 generic stack handling class useful in developing translators. A closely equivalent Java version appears earlier on this page.

The earlier versions of Java and C# did not have generic versions of these classes, but worked with elements of the Object class, which necessitated casting operations.

  using System.Collections.Generic;

  class Stack<E>
  // Class for constructing a stack of elements of type E

    public Stack<E>()
    // Empty stack constructor

    public void Push(E element)
    // Appends element to top of stack

    public E Pop()
    // Retrieves an element from top of stack

    public E Peek()
    // Returns element on top of stack without removing it

    public void Clear()
    // Clears all elements from stack

    public int Count { get; }
    // Returns number of elements in stack

    public boolean Contains(E element)
    // Returns true if element is in the stack

  } // Stack
Here is a sample program like the one given earlier showing the use of this library
   using Library;
   using System.Collections.Generic;
   using System.Text;

   class ShowGenericStack {
   // Demonstrate simple application of the generic Stack class (C# version 2.0)

     class Entry {
       public string name;
       public int age;                                     // other fields could be added

       public Entry(string name, int age) {                // constructor
         this.name = name;
         this.age = age;
       }
     } // Entry

     static Stack<Entry> stack = new Stack<Entry>();          // global for simplicity here!

     public static void Main (string[] args) {
     // Build a list of people's names and ages
       IO.WriteLine("Supply a list of people's names and ages.  CTRL-Z to terminate");
       do {
         string name = IO.ReadWord();
         int age = IO.ReadInt();
         IO.ReadLn();
         if (!IO.EOF()) stack.Push(new Entry(name, age));  // add to top of stack
       } while (!IO.EOF());

       IO.WriteLine(stack.Count + " items stored");        // report size of stack

       Entry patEntry = new Entry("Pat", 63);              // that fellow again!

       stack.Push(patEntry);

       if (stack.Contains(patEntry)) {                     // well, it must do!
         IO.WriteLine("Pat is on top of it, as always!");
         IO.WriteLine("Not bad for one who is only " + stack.Peek().age);
       }

       while (stack.Count != 0)                            // names in reverse order
         IO.WriteLine(stack.Pop().name);

     } // Main

   } // ShowGenericStack

Revised: 2014/03/13