Page 513 - Computer science 868 Class 12
P. 513

The above algorithms are implemented by the following Java programs.


                   Program 3     Define a class Repeat which allows the user to add elements from one end (rear) and remove
                                 elements from the other end (front) only. The following details of the class Repeat are given
                                 below.                                                                     [ISC 2010]
                                 Class name               :   Repeat
                                 Data Members
                                 st[]                     :   an array to hold a maximum of 100 integer elements
                                 cap                      :   to store the capacity of the array
                                 f                        :   to point to the index of the front
                                 r                        :   to point to the index of the rear
                                 Member Functions
                                 Repeat(int m)            :     constructor to initialise the data members cap = m, f = 0, r = 0
                                                              and create the integer array
                                 void pushvalue(int v)    :     to add integers from the rear index if possible else display the
                                                              message “overflow”
                                 int popvalue()           :     to remove  and  return element from the front, if array is
                                                              empty then return -9999
                                 void disp()              :   to display the elements present in the list
                                 Specify  the  class  Repeat  giving  details  of  the  constructor(int),  member  functions  void
                                 pushvalue(int), int popvalue() and void disp().
                   1       import java.util.*;

                   2       class Repeat
                   3       {

                   4       int st[]=new int[100];
                   5       int cap, f, r;

                   6       public Repeat (int m)
                   7       {

                   8       cap = m;
                   9       f=0;
                   10      r=0;

                   11      st = new int[cap];

                   12      }
                   13      void pushvalue (int v)
                   14      {

                   15      if (r == cap) //Queue full
                   16      {

                   17      System.out.println ("OVERFLOW");
                   18      }





                                                                                                                       511
                                                                                                       Data Structures  511
   508   509   510   511   512   513   514   515   516   517   518