diff --git a/C++/CycleDetection.cpp b/C++/CycleDetection.cpp new file mode 100644 index 0000000..f1609f8 --- /dev/null +++ b/C++/CycleDetection.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +// 0-> unvisited +// 1-> processing +// 2-> exited +vector visited; +bool found = false; +vector ans; +vector curStack; + +void DFS(vector graph[], int node){ + if (visited[node] != 0) return; + + visited[node] = 1; + curStack.push_back(node); + + for (int neighbour: graph[node]){ + if (visited[neighbour] == 1){ + // Found a cycle + found = true; + ans.push_back(neighbour); + while (curStack.back() != neighbour){ + ans.push_back(curStack.back()); + curStack.pop_back(); + } + ans.push_back(neighbour); + reverse(ans.begin(), ans.end()); + return; + } + + else if (visited[neighbour] == 0){ + DFS(graph, neighbour); + } + if (found) return; + } + + curStack.pop_back(); + visited[node] = 2; // Done processing +} + +void solve(vector graph[], int n){ + for (int i=0; i> n >> m; + + vector graph[n]; + visited.resize(n+1); + fill(visited.begin(), visited.end(), false); + + while (m--){ + int u, v; + cin >> u >> v; + u--; v--; + graph[u].push_back(v); + } + solve(graph, n); + + + return 0; +} \ No newline at end of file diff --git a/C++/FindMiddleElementLL.cpp b/C++/FindMiddleElementLL.cpp new file mode 100644 index 0000000..62b0648 --- /dev/null +++ b/C++/FindMiddleElementLL.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +struct Node{ + int val; + struct Node *next; +}; + +// typedef Node* nodeptr; + + +Node *head = NULL; + +void insert(int data){ + Node *newNode = new Node; + newNode->val = data; + newNode->next = head; + head = newNode; +} + +struct Node* findMiddle(){ + Node* slowptr = head; + Node* fastptr = head; + + while(fastptr && fastptr->next){ + slowptr = slowptr->next; + fastptr = fastptr->next->next; + } + return slowptr; +} + +void display(){ + Node *curNode = head; + while (curNode != NULL){ + cout << curNode->val << " "; + curNode = curNode->next; + } + cout << "\n"; +} + +int main(){ + insert(5); + insert(10); + insert(20); + insert(25); + insert(30); + display(); + Node* mid = findMiddle(); + cout << mid->val << endl; + return 0; +} \ No newline at end of file diff --git a/C++/NQueens.cpp b/C++/NQueens.cpp new file mode 100644 index 0000000..80e8b90 --- /dev/null +++ b/C++/NQueens.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +const int size = 4; +int board[size][size] = {0}; + +void printBoard(){ + // Output the state of the board + for (int i=0; i=0 && j=0; i++, j--){ + if (board[i][j] == 1) { + cout << "4" << endl; + return false; + } + } + + // Check if there is a qeen in the upper left diagonal + for (int i=row, j=col; i>=0 && j>=0; i--, j--){ + if (board[i][j] == 1) { + cout << "5" << endl; + return false; + } + } + + // Check if there is a qeen in the lower left diagonal + for (int i=row, j=col; i + +using namespace std; + +void TowersOfHanoi(int Stand_from, int Stand_mid, int Stand_to, int diskNum){ + // Base case: Only one disk + if (diskNum == 1){ + cout << "Move disk " << diskNum << " from stand " << Stand_from << " to stand " << Stand_to << endl; + return; + } + // Move the current disk to the spare stand + TowersOfHanoi(Stand_from, Stand_to, Stand_mid, diskNum-1); + + cout << "Move disk " << diskNum << " from stand " << Stand_from << " to stand " << Stand_to << endl; + // Move the disk from the spare stand to the required stand + TowersOfHanoi(Stand_mid, Stand_from, Stand_to, diskNum-1); +} + + +int main(){ + TowersOfHanoi(1, 2, 3, 3); + return 0; +} \ No newline at end of file