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:
Class name : repeat
Data members/instance variables:
St[] :
an array to hold a maximum of 100 integer elements
cap :
stores the capacity of the array
f :
to point the index of the front
r :
to point the index of the rear
Member functions:
Repeat(int m) :
constructor to initialize the data members cap=m, f=-1, r=-1 and to
create the integer array
void
pushvalue(int v) :to
add integers from the rear index if possible else display the
message
(“OVERFOW”)
int
popvalue() :
to remove and return element from the front. If array is empty
then
return -9999
void disp() :
display the elements present in the list
======================================================================
/**
* class repeat inserts and deletes elements from the arraylist.
*
* @author: Nitendra Verma
* @version: Sept 23, 2013
*/
import java.util.Scanner;
class repeat
{
protected int st[]; //array to hold elements
protected int cap; //stores the capacity of the array
protected int f,r; //to point to the first and last element
public repeat(int m) //constructor
{
cap=m;
f=-1;
r=-1;
st=new int[cap];
}
public boolean isEmpty() //function- returns true if array is empty
{
if(f==-1)
return true;
return false;
}
public void pushvalue(int v) //function- insert a new element at the rear of the array
{
if(r==-1)
{
f=0;
r=0;
st[r]=v;
}
else if(r+1<st.length)
st[++r]=v;
else
System.out.println("OVERFLOW OCCURES!!!");
}
public int popvalue() //function- deletes and returns the last(rear) element in the array
{
int elem;
boolean res;
res=isEmpty();
if(res==true)
return -9999;
else
{
elem=st[f];
if(f==r)
f=r=-1;
else
f++;
return elem;
}
}
public void disp() //display the elements in the array
{
boolean res;
res=isEmpty();
if(res==true)
System.out.println("No Element!!!");
else
{
System.out.println("Elements are:");
for(int i=f;i<=r;i++)
System.out.print(st[i]+"\t");
System.out.println("Total number of elements:"+(r-f+1));
}
}
}
public class test
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Eneter the capacity of array");
int cap=in.nextInt();
int ans;
repeat ob=new repeat(cap);
System.out.println("Eneter the value to be inserted");
int item=in.nextInt();
ob.pushvalue(item);
for(int i=0;i<10;i++)
{
System.out.println("Do you want to insert more item? Press 1 for Yes and 2 for No.");
ans=in.nextInt();
if(ans==1 && ans<cap)
{
System.out.println("Enetr item to be pushed");
int numnext=in.nextInt();
ob.pushvalue(numnext);
}
else
break;
}
System.out.println("Items in stack are:");
ob.disp();
int anspop,res;
System.out.println("Want to pop?Press 1 for Yes, 2 for No.");
anspop=in.nextInt();
if(anspop==1)
{
res=ob.popvalue();
if(res==0)
System.out.println("Underflow occurs");
System.out.println("Item deleted successfully");
System.out.println("Items after deletion:");
ob.disp();
}
}
}
/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in
/*
OUTPUT:
Eneter the capacity of array
4
Eneter the value to be inserted
1
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
3
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
5
Do you want to insert more item? Press 1 for Yes and 2 for No.
1
Enetr item to be pushed
7
Do you want to insert more item? Press 1 for Yes and 2 for No.
2
Items in stack are:
Elements are:
1 3 5 7 Total number of elements:4
Want to pop?Press 1 for Yes, 2 for No.
1
Item deleted successfully
Items after deletion:
Elements are:
3 5 7 Total number of elements:3
*/
No comments:
Post a Comment
Ur comments r most welcome...