Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Puffin Rock Characters Plush Toy Set
anil1961
Updated:
Saturday at 3:51 PM
Toyota Vitz scp10 For Sale
Lakmal Bandara
Updated:
Wednesday at 10:50 AM
Kandy
Welcome all my all new pet shop Bark.lk
dilumrane
Updated:
Nov 2, 2026
Windows RDP Only Rs.500 a month
Blogerwiki
Updated:
Nov 1, 2026
Ad icon
V2ray VPN for Tunneling
hu KANNA
Updated:
Oct 22, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Jobs & Employment
Programming interview - written tests
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="Anonymous_Abstract" data-source="post: 24659406" data-attributes="member: 568286"><p><span style="font-size: 15px"> Circular Queue in Java</span></p><p><span style="color: Blue"><span style="font-size: 15px"></span></span></p><p><span style="color: Blue"><span style="font-size: 15px">package com.benz.test;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"></span></span></p><p><span style="color: Blue"><span style="font-size: 15px">import java.util.Scanner;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"></span></span></p><p><span style="color: Blue"><span style="font-size: 15px">public class Cqueue {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> </span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> private int size;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> private int rear,front;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> private int[] cqueu=null;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> private boolean exit=false;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> Scanner sc=null;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> public Cqueue(int size,int rear,int front)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> this.size=size;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> this.front=front;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> this.rear=rear;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> cqueu=new int[this.size];</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> public void show()</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> while(exit==false)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> sc= new Scanner(System.in);</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("1. Enqueue");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("2. Dequeue");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("3. Display");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("4. Exit");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> </span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("Enter your choice");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> int x = sc.nextInt();</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> </span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> switch(x)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> case 1:System.out.println("Enter a Value");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> enqueue(sc.nextInt());break;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> case 2:dequeue();break;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> case 3:display();break;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> case 4:exit();break;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> default:System.out.println("Invalid Input");break;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> public void exit()</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> </span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("Program is Exited");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> exit = true;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> </span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> public void enqueue(int value)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> if(rear==size-1 && front==0 || front==rear+1)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("Queue is Overflow");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> else if(rear==-1 && front==-1)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> front=rear=0;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> cqueu[rear]=value;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println(value +" is inserted");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }else if(rear==size-1 && front !=0)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> rear=0;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> cqueu[rear]=value;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println(value +" is inserted");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }else {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> rear++;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> cqueu[rear]=value;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println(value +" is inserted");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> public void dequeue()</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> if(front==-1 && rear==-1)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("Queue is Empty");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> else if(front==rear)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> int del=cqueu[front];</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println(del+" is deleted");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> front=rear=-1;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }else if(front==size-1)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> int del=cqueu[front];</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println(del+" is deleted");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> front=0;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> </span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }else {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> int del=cqueu[front];</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println(del+" is deleted");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> front +=1;</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> }</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> public void display()</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> if(rear==-1 && front==-1)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.println("Queue is Empty");</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> else if(rear >=front)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> for(int i=front;i<=rear;i++)</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> {</span></span></p><p><span style="color: Blue"><span style="font-size: 15px"> System.out.print(cqueu<em>+"\t");</em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em> }</em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em> System.out.print("\n");</em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em> }else {</em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em> for(int i=front;i<size;i++)</em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em> {</em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em> System.out.print(cqueu<em>+"\t");</em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em> }</em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em> </em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em> for(int i=0;i<=rear;i++)</em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em> {</em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em> System.out.print(cqueu<em>+"\t");</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> }</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> </em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> System.out.print("\n");</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> }</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> }</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> public static void main(String[] args)</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> {</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> Cqueue que= new Cqueue(6,-1,-1);</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> que.show();</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> }</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> </em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em> </em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em>}</em></em></em></span></span></p><p><span style="color: Blue"><span style="font-size: 15px"><em><em><em></em></em></em></span></span></p></blockquote><p></p>
[QUOTE="Anonymous_Abstract, post: 24659406, member: 568286"] [SIZE="4"] Circular Queue in Java[/SIZE] [COLOR="Blue"][SIZE="4"] package com.benz.test; import java.util.Scanner; public class Cqueue { private int size; private int rear,front; private int[] cqueu=null; private boolean exit=false; Scanner sc=null; public Cqueue(int size,int rear,int front) { this.size=size; this.front=front; this.rear=rear; cqueu=new int[this.size]; } public void show() { while(exit==false) { sc= new Scanner(System.in); System.out.println("1. Enqueue"); System.out.println("2. Dequeue"); System.out.println("3. Display"); System.out.println("4. Exit"); System.out.println("Enter your choice"); int x = sc.nextInt(); switch(x) { case 1:System.out.println("Enter a Value"); enqueue(sc.nextInt());break; case 2:dequeue();break; case 3:display();break; case 4:exit();break; default:System.out.println("Invalid Input");break; } } } public void exit() { System.out.println("Program is Exited"); exit = true; } public void enqueue(int value) { if(rear==size-1 && front==0 || front==rear+1) System.out.println("Queue is Overflow"); else if(rear==-1 && front==-1) { front=rear=0; cqueu[rear]=value; System.out.println(value +" is inserted"); }else if(rear==size-1 && front !=0) { rear=0; cqueu[rear]=value; System.out.println(value +" is inserted"); }else { rear++; cqueu[rear]=value; System.out.println(value +" is inserted"); } } public void dequeue() { if(front==-1 && rear==-1) System.out.println("Queue is Empty"); else if(front==rear) { int del=cqueu[front]; System.out.println(del+" is deleted"); front=rear=-1; }else if(front==size-1) { int del=cqueu[front]; System.out.println(del+" is deleted"); front=0; }else { int del=cqueu[front]; System.out.println(del+" is deleted"); front +=1; } } public void display() { if(rear==-1 && front==-1) System.out.println("Queue is Empty"); else if(rear >=front) { for(int i=front;i<=rear;i++) { System.out.print(cqueu[i]+"\t"); } System.out.print("\n"); }else { for(int i=front;i<size;i++) { System.out.print(cqueu[i]+"\t"); } for(int i=0;i<=rear;i++) { System.out.print(cqueu[i]+"\t"); } System.out.print("\n"); } } public static void main(String[] args) { Cqueue que= new Cqueue(6,-1,-1); que.show(); } } [/i][/i][/i][/SIZE][i][i][i][/i][/i][/i][/COLOR][i][i][i][/i][/i][/i] [/QUOTE]
Insert quotes…
Verification
Hata thunen beduwama keeyada? (60 bedeema thuna)
Post reply
Top
Bottom