Question
Link is an entity which can hold a maximum of 100 integers . Link enables the user to add elements from the rear end and remove integers from the front end of the entity. Define a class Link with the following details:
Classname:Link
Data members / instance variables:
lnk[ ]: entity to hold the integer elements
max:stores the maximum capacity of the entity
begin: to point to the index of the front end
end: to point to the index of the rear end
Member functions:
Link(int mm):constructor to initialize,max=mm,begin=0,end=0
void addlink(int v):to add an element from the rear index if possible otherwise display the message “OUT OF SIZE…”
int dellink():to remove and return an element from the front index , if possible otherwise display the message “EMPTY…” and return -99
void display():displays the elements of the entity
(a) Specify the class Link giving details of the constructor(int) void addlink(int), int dellink() and void display().
THE MAIN() FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class Link
{
int lnk[]=new int[100];
int max,begin,end;
Link(int mm)
{
max=mm;begin=0;end=0;
lnk=new int[max];
}
void addlink(int v)
{
if(end< max)
{
lnk[end]=v;
end=end+1;
}
else
{
System.out.println("OUT OF SIZE...");
}
}
int dellink( )
{
int v;
if(begin!=end)
{
v=lnk[begin];
begin=begin+1;
return v;
}
else
{
System.out.println("EMPTY...");
return -99;
}
}
void display()
{
for(int i=begin;i< end;i++)
{
System.out.println(lnk[i]);
}
}
}