Skip to content

Commit 8430689

Browse files
committed
Added new problem solutions
1 parent a9de8e8 commit 8430689

File tree

12 files changed

+592
-0
lines changed

12 files changed

+592
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
};
8+
9+
struct Node* createNode(int data) {
10+
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
11+
newNode->data = data;
12+
newNode->next = NULL;
13+
return newNode;
14+
}
15+
16+
int detectCycle(struct Node* head) {
17+
struct Node *slow = head, *fast = head;
18+
while (fast != NULL && fast->next != NULL) {
19+
slow = slow->next;
20+
fast = fast->next->next;
21+
if (slow == fast)
22+
return 1;
23+
}
24+
return 0;
25+
}
26+
27+
int main() {
28+
int n, val, pos;
29+
struct Node *head = NULL, *tail = NULL, *cycleNode = NULL;
30+
31+
printf("Enter the number of nodes: ");
32+
scanf("%d", &n);
33+
34+
for (int i = 0; i < n; i++) {
35+
printf("Enter value for node %d: ", i + 1);
36+
scanf("%d", &val);
37+
struct Node* newNode = createNode(val);
38+
if (head == NULL) {
39+
head = newNode;
40+
} else {
41+
tail->next = newNode;
42+
}
43+
tail = newNode;
44+
if (i == 0)
45+
cycleNode = newNode;
46+
}
47+
48+
printf("Enter the position to create a cycle (0 for no cycle): ");
49+
scanf("%d", &pos);
50+
if (pos > 0) {
51+
struct Node* temp = head;
52+
for (int i = 1; i < pos; i++)
53+
temp = temp->next;
54+
tail->next = temp;
55+
}
56+
57+
if (detectCycle(head))
58+
printf("Cycle detected in the linked list.\n");
59+
else
60+
printf("No cycle detected in the linked list.\n");
61+
62+
return 0;
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
struct Node {
5+
int data;
6+
Node* next;
7+
Node(int val) : data(val), next(nullptr) {}
8+
};
9+
10+
bool detectCycle(Node* head) {
11+
Node *slow = head, *fast = head;
12+
while (fast != nullptr && fast->next != nullptr) {
13+
slow = slow->next;
14+
fast = fast->next->next;
15+
if (slow == fast)
16+
return true;
17+
}
18+
return false;
19+
}
20+
21+
int main() {
22+
int n, val, pos;
23+
cout << "Enter the number of nodes: ";
24+
cin >> n;
25+
26+
Node *head = nullptr, *tail = nullptr;
27+
for (int i = 0; i < n; i++) {
28+
cout << "Enter value for node " << i + 1 << ": ";
29+
cin >> val;
30+
Node* newNode = new Node(val);
31+
if (head == nullptr) {
32+
head = newNode;
33+
} else {
34+
tail->next = newNode;
35+
}
36+
tail = newNode;
37+
}
38+
39+
cout << "Enter the position to create a cycle (0 for no cycle): ";
40+
cin >> pos;
41+
if (pos > 0) {
42+
Node* temp = head;
43+
for (int i = 1; i < pos; i++)
44+
temp = temp->next;
45+
tail->next = temp;
46+
}
47+
48+
if (detectCycle(head))
49+
cout << "Cycle detected in the linked list.\n";
50+
else
51+
cout << "No cycle detected in the linked list.\n";
52+
53+
return 0;
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.Scanner;
2+
3+
class Node {
4+
int data;
5+
Node next;
6+
Node(int data) {
7+
this.data = data;
8+
this.next = null;
9+
}
10+
}
11+
12+
public class DetectCycle {
13+
static boolean detectCycle(Node head) {
14+
Node slow = head, fast = head;
15+
while (fast != null && fast.next != null) {
16+
slow = slow.next;
17+
fast = fast.next.next;
18+
if (slow == fast)
19+
return true;
20+
}
21+
return false;
22+
}
23+
24+
public static void main(String[] args) {
25+
Scanner scanner = new Scanner(System.in);
26+
System.out.print("Enter the number of nodes: ");
27+
int n = scanner.nextInt();
28+
29+
Node head = null, tail = null;
30+
for (int i = 0; i < n; i++) {
31+
System.out.print("Enter value for node " + (i + 1) + ": ");
32+
int val = scanner.nextInt();
33+
Node newNode = new Node(val);
34+
if (head == null) {
35+
head = newNode;
36+
} else {
37+
tail.next = newNode;
38+
}
39+
tail = newNode;
40+
}
41+
42+
System.out.print("Enter the position to create a cycle (0 for no cycle): ");
43+
int pos = scanner.nextInt();
44+
if (pos > 0) {
45+
Node temp = head;
46+
for (int i = 1; i < pos; i++) {
47+
temp = temp.next;
48+
}
49+
tail.next = temp;
50+
}
51+
52+
if (detectCycle(head))
53+
System.out.println("Cycle detected in the linked list.");
54+
else
55+
System.out.println("No cycle detected in the linked list.");
56+
57+
scanner.close();
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
def detect_cycle(head):
7+
slow, fast = head, head
8+
while fast and fast.next:
9+
slow = slow.next
10+
fast = fast.next.next
11+
if slow == fast:
12+
return True
13+
return False
14+
15+
if __name__ == "__main__":
16+
n = int(input("Enter the number of nodes: "))
17+
head = None
18+
tail = None
19+
20+
for i in range(n):
21+
val = int(input(f"Enter value for node {i + 1}: "))
22+
new_node = Node(val)
23+
if head is None:
24+
head = new_node
25+
else:
26+
tail.next = new_node
27+
tail = new_node
28+
29+
pos = int(input("Enter the position to create a cycle (0 for no cycle): "))
30+
if pos > 0:
31+
temp = head
32+
for _ in range(pos - 1):
33+
temp = temp.next
34+
tail.next = temp
35+
36+
if detect_cycle(head):
37+
print("Cycle detected in the linked list.")
38+
else:
39+
print("No cycle detected in the linked list.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <limits.h>
3+
4+
#define MAX 100
5+
6+
int findMinDistance(int dist[], int visited[], int n) {
7+
int min = INT_MAX, minIndex = -1;
8+
for (int v = 0; v < n; v++) {
9+
if (!visited[v] && dist[v] <= min) {
10+
min = dist[v];
11+
minIndex = v;
12+
}
13+
}
14+
return minIndex;
15+
}
16+
17+
void dijkstra(int graph[MAX][MAX], int n, int start) {
18+
int dist[n], visited[n];
19+
20+
for (int i = 0; i < n; i++) {
21+
dist[i] = INT_MAX;
22+
visited[i] = 0;
23+
}
24+
dist[start] = 0;
25+
26+
for (int count = 0; count < n - 1; count++) {
27+
int u = findMinDistance(dist, visited, n);
28+
visited[u] = 1;
29+
30+
for (int v = 0; v < n; v++) {
31+
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
32+
dist[v] = dist[u] + graph[u][v];
33+
}
34+
}
35+
}
36+
37+
printf("Vertex\tDistance from Source\n");
38+
for (int i = 0; i < n; i++) {
39+
printf("%d\t%d\n", i, dist[i]);
40+
}
41+
}
42+
43+
int main() {
44+
int n, start;
45+
int graph[MAX][MAX];
46+
printf("Enter the number of vertices: ");
47+
scanf("%d", &n);
48+
49+
printf("Enter the adjacency matrix:\n");
50+
for (int i = 0; i < n; i++) {
51+
for (int j = 0; j < n; j++) {
52+
scanf("%d", &graph[i][j]);
53+
}
54+
}
55+
56+
printf("Enter the starting vertex: ");
57+
scanf("%d", &start);
58+
59+
dijkstra(graph, n, start);
60+
return 0;
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
using namespace std;
5+
6+
int findMinDistance(vector<int>& dist, vector<bool>& visited, int n) {
7+
int min = INT_MAX, minIndex = -1;
8+
for (int v = 0; v < n; v++) {
9+
if (!visited[v] && dist[v] <= min) {
10+
min = dist[v];
11+
minIndex = v;
12+
}
13+
}
14+
return minIndex;
15+
}
16+
17+
void dijkstra(vector<vector<int>>& graph, int n, int start) {
18+
vector<int> dist(n, INT_MAX);
19+
vector<bool> visited(n, false);
20+
21+
dist[start] = 0;
22+
23+
for (int count = 0; count < n - 1; count++) {
24+
int u = findMinDistance(dist, visited, n);
25+
visited[u] = true;
26+
27+
for (int v = 0; v < n; v++) {
28+
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
29+
dist[v] = dist[u] + graph[u][v];
30+
}
31+
}
32+
}
33+
34+
cout << "Vertex\tDistance from Source" << endl;
35+
for (int i = 0; i < n; i++) {
36+
cout << i << "\t" << dist[i] << endl;
37+
}
38+
}
39+
40+
int main() {
41+
int n, start;
42+
cout << "Enter the number of vertices: ";
43+
cin >> n;
44+
45+
vector<vector<int>> graph(n, vector<int>(n));
46+
cout << "Enter the adjacency matrix:" << endl;
47+
for (int i = 0; i < n; i++) {
48+
for (int j = 0; j < n; j++) {
49+
cin >> graph[i][j];
50+
}
51+
}
52+
53+
cout << "Enter the starting vertex: ";
54+
cin >> start;
55+
56+
dijkstra(graph, n, start);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)