Class 12

WordPile Stack Program ISC 2015

WordPile is an entity which can hold maximum of 20 characters. The restriction is that a character can be added or removed from one end only.

Some of the members of the class are given below:

Class name: WordPile
Data members/instance variables:
ch[]: character array to hold the character elements.
capacity: integer variable to store the maximum capacity.
top: to point to the index of the topmost element.
Methods/Member functions:
WordPile(int cap): constructor to initialize the data member capacity = cap, top = -1 and create the WordPile.
void pushChar(char v): adds the character to the top of WordPile if possible, otherwise output a message “WordPile is full”
char popChar(): returns the deleted character from the top of the WordPile if possible, otherwise it returns ‘\\’.

  1. Specify the class WordPile giving the details of the constructor, void pushChar(char) and char popChar(). The main function and algorithm need not be written.
  2. What is the name of the entity described above and state one of its applications.
class WordPile{
    private char ch[];
    private int capacity;
    private int top;
    public WordPile(int cap){
        capacity = cap;
        if(capacity > 20)
            capacity = 20;
        top = -1;
        ch = new char[capacity];
    public void pushChar(char v){
        if(top + 1 < capacity)
            ch[++top] = v;
            System.out.println("WordPile is full");
    public char popChar(){
        if(top == -1)
            return '\\';
            return ch[top--];

The name of the entity described above is Stack. One of its applications is to reverse a string.

By admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.