Categories
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;
        else
            System.out.println("WordPile is full");
    }
    public char popChar(){
        if(top == -1)
            return '\\';
        else
            return ch[top--];
    }
}

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

Leave a Reply