Pages

Tuesday 10 March 2020

Queue implementation using linked list

// Queue implementation using linked list
import java.util.*;
class qulink
{
int info;
qulink next;
}
class rklinkqueue
   {
public static void main(String []args)
{
queue ql=new queue();
int ch,x;
Scanner sc=new Scanner(System.in);
while(true)
    {
System.out.printf("\n ***MENU***\n");
System.out.println("1.INSERT");
System.out.println("2.DELETE");
System.out.println("3.DISPLAY");
System.out.println("4.EXIT");
System.out.println("Enter Ur Choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter any Number into queue :");
x=sc.nextInt();
ql.insert(x);
break;
case 2: ql.delete();
break;
case 3: ql.display();
break;
case 4: System.exit(0);
default:System.out.println("Wrong choice");
       }
    }
  }
}


class queue
{
   qulink front,rear,start,tmp;
   void insert(int x)
    {
tmp=new qulink();
tmp.info=x;
tmp.next=null;
if(front == null)
front=tmp;
else
rear.next=tmp;
rear=tmp;
   }
   void delete()
    {
if(front == null)
  System.out.println("queue underflow");
else
{
tmp=front;
System.out.println("Deleted Element is:"+tmp.info);
front=front.next;
   }
  }
  void display()
  {
start=front;
if(front==null)
System.out.println("queue is empty");
else
{
  System.out.printf("queue linked list is :");
  while(start!=null)
  {
    System.out.printf(" %h -->%d ||",start,start.info);
  start=start.next;
    }
  System.out.printf(" %h",start);
}
 }
}

output:




No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...