Implement the linkedList as a Queue.(FIFO)
Do not copy paste and make comments on each line write in java
Expert Answer
Nde.java
import java.util.*;
class Nde
{
protected int dat;
protected Nde lnk;
public Nde()
{
lnk = null;
dat = 0;
}
public Nde(int datas,Nde l)
{
dat = datas;
lnk = l;
}
public void setLnk(Nde l)
{
lnk = l;
}
public void setDat(int datas)
{
dat = datas;
}
public Nde getLnk()
{
return lnk;
}
public int getDat()
{
return dat;
}
}
linkQueue.java
import java.util.*;
class Nde
{
protected int dat;
protected Nde lnk;
public Nde()
{
lnk = null;
dat = 0;
}
public Nde(int datas,Nde l)
{
dat = datas;
lnk = l;
}
public void setLnk(Nde l)
{
lnk = l;
}
public void setDat(int datas)
{
dat = datas;
}
public Nde getLnk()
{
return lnk;
}
public int getDat()
{
return dat;
}
}
class linkQue
{
protected Nde fn, back;
public int sz;
public linkQue()
{
fn = null;
back = null;
sz = 0;
}
public boolean isKhaali()
{
return fn == null;
}
public int getSz()
{
return sz;
}
public void ins(int dat)
{
Nde ndePtr = new Nde(dat, null);
if (back == null)
{
fn = ndePtr;
back = ndePtr;
}
else
{
back.setLnk(ndePtr);
back = back.getLnk();
}
sz++ ;
}
public int rm()
{
if (isKhaali() )
throw new NoSuchElementException(“Underflow Exception”);
Nde ndptr = fn;
fn = ndptr.getLnk();
if (fn == null)
back = null;
sz– ;
return ndptr.getDat();
}
public int peeking()
{
if (isKhaali() )
throw new NoSuchElementException(“Underflow Exception”);
return fn.getDat();
}
public void show()
{
System.out.print(“nQueue = “);
if (sz == 0)
{
System.out.print(“Emptyn “);
return ;
}
Nde ndptr = fn;
while (ndptr != back.getLnk() )
{
System.out.print(ndptr.getDat()+” “);
ndptr = ndptr.getLnk();
}
System.out.println();
}
}
public class LQImp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
linkQue lque = new linkQue();
System.out.println(“Linked Queue Testn”);
char ch;
do
{
System.out.println(“nQueue Operations”);
System.out.println(“1. Inserting “);
System.out.println(“2. Removing”);
System.out.println(“3. Peeking”);
System.out.println(“4. Check Empty”);
System.out.println(“5. Size”);
int choice = sc.nextInt();
switch (choice)
{
case 1 :
System.out.println(“Enter integer element to ins”);
lque.ins( sc.nextInt() );
break;
case 2 :
try
{
System.out.println(“Removed Element = “+ lque.rm());
}
catch (Exception e)
{
System.out.println(“Error : ” + e.getMessage());
}
break;
case 3 :
try
{
System.out.println(“Peek Element = “+ lque.peeking());
}
catch (Exception e)
{
System.out.println(“Error : ” + e.getMessage());
}
break;
case 4 :
System.out.println(“Empty status = “+ lque.isKhaali());
break;
case 5 :
System.out.println(“Size = “+ lque.getSz());
break;
default :
System.out.println(“Wrong Entry n “);
break;
}
lque.show();
System.out.println(“nDo you want to continue (Type y or l) n”);
ch = sc.next().charAt(0);
} while (ch == ‘Y’|| ch == ‘y’);
}
}