Page 499 - Computer science 868 Class 12
P. 499

Let us illustrate the push and pop operations in Stack with the following Java programs.


                   Program 1     Stack is a kind of data structure which can store elements with the restriction that an element
                                 can be added or removed from the top only. The details of class Stack are given below.
                                 Class name               :   Stack
                                 Data Members
                                 a[]                      :   an array to hold numbers
                                 size                     :   the maximum capacity of the string array
                                 top                      :   the index of the topmost element of the stack
                                 Member Functions
                                 Stack(int x)             :   constructor to initialise size = x and top = -1
                                 void push (int n)        :     to push number ‘n’ into the stack. If the stack is full, display
                                                              the message “overflow”
                                 void pop()               :     to remove an element from the top of the stack and display
                                                              it. If the stack is empty, display the message “underflow”
                                 void display()           :   to display the elements of the stack
                                 Specify class Stack giving details of the constructor(), void push (int n), void pop() and void
                                 display().


                   1       import java.util.*;
                   2       class Stack

                   3       { int a[];
                   4         int size,top;

                   5         Stack(int x)//constructor
                   6         { size=x;

                   7           top=-1;
                   8           a=new int[size];

                   9         }
                   10        void push(int n) // push
                   11        { if(top==size-1)  // if stack is full

                   12              System.out.println("Stack full.........overflow");

                   13          else
                   14             a[++top]=n;  // increase top by 1 and push n to stack
                   15        }

                   16        void pop() //pop
                   17        { if(top<0)  // if stack is empty

                   18           { System.out.println("Stack empty....underflow");
                   19                 }

                   20          else




                                                                                                                       497
                                                                                                       Data Structures  497
   494   495   496   497   498   499   500   501   502   503   504