Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new code #408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 59 additions & 59 deletions cir_linkedlist.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@

import java.util.*;
class cir_linkedlist
{
static class Node
{
import java.util.*;

class CircularLinkedListQueue {
private static class Node {
int data;
Node link;

Node(int data) {
this.data = data;
this.link = null;
}
}
static class Queue
{

private static class Queue {
Node front, rear;

Queue() {
front = rear = null;
}
}
// Function to create Circular queue
static void enQueue(Queue q, int value)
{
Node temp = new Node();
temp.data = value;
if (q.front == null)

// Function to create Circular queue
static void enQueue(Queue q, int value) {
Node temp = new Node(value);
if (q.front == null) {
q.front = temp;
else
} else {
q.rear.link = temp;

}
q.rear = temp;
q.rear.link = q.front;
q.rear.link = q.front; // Link the last node to the front
}
// Function to delete element from Circular Queue
static int deQueue(Queue q)
{
if (q.front == null)
{
System.out.printf("Queue is empty");
return Integer.MIN_VALUE;
}

// If this is the last node to be deleted
int value; // Value to be dequeued
if (q.front == q.rear)
{
value = q.front.data;
q.front = null;
q.rear = null;

// Function to delete element from Circular Queue
static int deQueue(Queue q) {
if (q.front == null) {
System.out.println("Queue is empty");
return -1; // Indicate that the queue is empty
}
else // There are more than one nodes
{
Node temp = q.front;
value = temp.data;

int value = q.front.data;
if (q.front == q.rear) {
// Only one node in the queue
q.front = q.rear = null;
} else {
// More than one node
q.front = q.front.link;
q.rear.link = q.front;
}

return value;
}

// Function displaying the elements of Circular Queue
static void displayQueue(Queue q)
{
static void displayQueue(Queue q) {
if (q.front == null) {
System.out.println("Queue is empty");
return;
}

Node temp = q.front;
System.out.printf("\nElements in Circular Queue are: ");
while (temp.link != q.front)
{
System.out.printf("%d ", temp.data);
System.out.print("Elements in Circular Queue are: ");
do {
System.out.print(temp.data + " ");
temp = temp.link;
}
System.out.printf("%d", temp.data);
} while (temp != q.front);
System.out.println();
}

/* Driver of the program */
public static void main(String args[])
{
// Create a queue and initialize front and rear

// Driver of the program
public static void main(String[] args) {
Queue q = new Queue();
q.front = q.rear = null;


// Inserting elements in Circular Queue
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);

// Display elements present in Circular Queue
displayQueue(q);

// Deleting elements from Circular Queue
System.out.printf("\nDeleted value = %d", deQueue(q));
System.out.printf("\nDeleted value = %d", deQueue(q));
System.out.printf("Deleted value = %d%n", deQueue(q));
System.out.printf("Deleted value = %d%n", deQueue(q));

// Remaining elements in Circular Queue
displayQueue(q);

enQueue(q, 9);
enQueue(q, 20);
displayQueue(q);
Expand Down