Skip to content

Commit eaf11e3

Browse files
committedMar 13, 2025
first commit
0 parents  commit eaf11e3

40 files changed

+1011
-0
lines changed
 

‎AndOrNot.class

895 Bytes
Binary file not shown.

‎AndOrNot.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Scanner;
2+
public class AndOrNot {
3+
public static void main(String[] agrs)
4+
{
5+
//making scanner object
6+
Scanner sc= new Scanner(System.in);
7+
System.out.print("Enter number 1: ");
8+
int num1=sc.nextInt();
9+
System.out.println("Enter the number 2:");
10+
int num2=sc.nextInt();
11+
12+
int sum = num1 + num2;
13+
System.out.println("Sum of num1 and num2 is: " + sum);
14+
}
15+
}

‎CircularLinkedList.class

2.02 KB
Binary file not shown.

‎CircularLinkedList.java

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// CircularNode class to represent each element in the circular linked list
2+
class CircularNode {
3+
int data;
4+
CircularNode next;
5+
6+
// Constructor to initialize the node with data
7+
public CircularNode(int data) {
8+
this.data = data;
9+
this.next = null;
10+
}
11+
}
12+
13+
// CircularLinkedList class to manage the linked list operations
14+
public class CircularLinkedList {
15+
CircularNode head;
16+
17+
// Method to add a node at the end of the list
18+
public void add(int data) {
19+
CircularNode newNode = new CircularNode(data);
20+
21+
// If the list is empty, make the new node the head
22+
if (head == null) {
23+
head = newNode;
24+
head.next = head; // Point the node to itself
25+
return;
26+
}
27+
28+
// Traverse to the end of the list
29+
CircularNode temp = head;
30+
while (temp.next != head) {
31+
temp = temp.next;
32+
}
33+
34+
// Add the new node at the end of the list
35+
temp.next = newNode;
36+
newNode.next = head; // Link the new node to the head
37+
}
38+
39+
// Method to insert a node at a specific position
40+
public void insert(int data, int position) {
41+
CircularNode newNode = new CircularNode(data);
42+
43+
// If inserting at the head
44+
if (position == 0) {
45+
if (head == null) {
46+
head = newNode;
47+
head.next = head;
48+
return;
49+
}
50+
CircularNode temp = head;
51+
while (temp.next != head) {
52+
temp = temp.next;
53+
}
54+
newNode.next = head;
55+
head = newNode;
56+
temp.next = head;
57+
return;
58+
}
59+
60+
// Traverse to the position
61+
CircularNode temp = head;
62+
for (int i = 0; i < position - 1 && temp.next != head; i++) {
63+
temp = temp.next;
64+
}
65+
66+
// Insert the new node
67+
newNode.next = temp.next;
68+
temp.next = newNode;
69+
}
70+
71+
// Method to delete a node at a specific position
72+
public void delete(int position) {
73+
if (head == null) {
74+
System.out.println("List is empty.");
75+
return;
76+
}
77+
78+
// If deleting the head node
79+
if (position == 0) {
80+
if (head.next == head) {
81+
head = null;
82+
return;
83+
}
84+
CircularNode temp = head;
85+
while (temp.next != head) {
86+
temp = temp.next;
87+
}
88+
head = head.next;
89+
temp.next = head;
90+
return;
91+
}
92+
93+
// Traverse to the position
94+
CircularNode temp = head;
95+
for (int i = 0; i < position - 1 && temp.next != head; i++) {
96+
temp = temp.next;
97+
}
98+
99+
// Delete the node
100+
if (temp.next == head) {
101+
System.out.println("Invalid position.");
102+
return;
103+
}
104+
temp.next = temp.next.next;
105+
}
106+
107+
// Method to display the list
108+
public void display() {
109+
if (head == null) {
110+
System.out.println("List is empty.");
111+
return;
112+
}
113+
114+
CircularNode temp = head;
115+
do {
116+
System.out.print(temp.data + " ");
117+
temp = temp.next;
118+
} while (temp != head);
119+
System.out.println();
120+
}
121+
122+
// Main method to test the circular linked list implementation
123+
public static void main(String[] args) {
124+
CircularLinkedList cll = new CircularLinkedList();
125+
126+
// Add elements to the list
127+
cll.add(10);
128+
cll.add(20);
129+
cll.add(30);
130+
cll.add(40);
131+
132+
// Display the list
133+
System.out.println("Circular Linked List:");
134+
cll.display();
135+
136+
// Insert an element at position 2
137+
cll.insert(25, 2);
138+
System.out.println("After inserting 25 at position 2:");
139+
cll.display();
140+
141+
// Delete the element at position 3
142+
cll.delete(3);
143+
System.out.println("After deleting the element at position 3:");
144+
cll.display();
145+
}
146+
}

‎CircularNode.class

304 Bytes
Binary file not shown.
1.22 KB
Binary file not shown.

‎CircularQueueExample.class

1.03 KB
Binary file not shown.

‎CircularQueueExample.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
public class CircularQueueExample {
2+
3+
// CircularQueue class definition
4+
static class CircularQueue {
5+
private int[] queue; // Array to store the elements of the queue
6+
private int front, rear, size; // Variables to keep track of the front, rear, and size of the queue
7+
8+
// Constructor to initialize the queue with a given size
9+
public CircularQueue(int size) {
10+
this.size = size;
11+
this.queue = new int[size];
12+
this.front = -1;
13+
this.rear = -1;
14+
}
15+
16+
// Method to check if the queue is full
17+
public boolean isFull() {
18+
return (front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1));
19+
}
20+
21+
// Method to check if the queue is empty
22+
public boolean isEmpty() {
23+
return front == -1;
24+
}
25+
26+
// Method to add an element to the queue
27+
public void enqueue(int element) {
28+
if (isFull()) {
29+
System.out.println("Queue is full");
30+
return;
31+
}
32+
if (front == -1) {
33+
front = 0;
34+
}
35+
rear = (rear + 1) % size;
36+
queue[rear] = element;
37+
}
38+
39+
// Method to remove and return the front element from the queue
40+
public int dequeue() {
41+
if (isEmpty()) {
42+
System.out.println("Queue is empty");
43+
return -1;
44+
}
45+
int element = queue[front];
46+
if (front == rear) {
47+
front = -1;
48+
rear = -1;
49+
} else {
50+
front = (front + 1) % size;
51+
}
52+
return element;
53+
}
54+
55+
// Method to return the front element without removing it
56+
public int peek() {
57+
if (isEmpty()) {
58+
System.out.println("Queue is empty");
59+
return -1;
60+
}
61+
return queue[front];
62+
}
63+
}
64+
65+
// Main method to demonstrate the CircularQueue usage
66+
public static void main(String[] args) {
67+
CircularQueue cq = new CircularQueue(5); // Create a circular queue with size 5
68+
69+
// Add elements to the queue
70+
cq.enqueue(10);
71+
cq.enqueue(20);
72+
cq.enqueue(30);
73+
cq.enqueue(40);
74+
cq.enqueue(50);
75+
76+
// Remove and print an element from the queue
77+
System.out.println("Dequeued element: " + cq.dequeue());
78+
79+
// Print the front element
80+
System.out.println("Front element: " + cq.peek());
81+
82+
// Add another element to the queue
83+
cq.enqueue(60);
84+
85+
// Remove and print all elements from the queue
86+
while (!cq.isEmpty()) {
87+
System.out.println("Dequeued element: " + cq.dequeue());
88+
}
89+
}
90+
}
91+

‎DoublyLinkedList.class

2.33 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.