diff --git a/DIRECTORY.md b/DIRECTORY.md index 178e88499..567460083 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -58,7 +58,6 @@ * [Edit Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/edit_distance.cpp) * [Egg Dropping Puzzle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/egg_dropping_puzzle.cpp) * [Fibonacci Bottom Up](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_bottom_up.cpp) - * [Fibonacci Top Down](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_top_down.cpp) * [Floyd Warshall](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/floyd_warshall.cpp) * [Kadane](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane.cpp) * [Longest Common String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_string.cpp) diff --git a/data_structures/linked_list_delete_without_head.cpp b/data_structures/linked_list_delete_without_head.cpp new file mode 100644 index 000000000..53b9f288d --- /dev/null +++ b/data_structures/linked_list_delete_without_head.cpp @@ -0,0 +1,109 @@ +/** + * @file + * \brief Delete a node in a linked list without the head pointer + * \details + * This algortihm helps delete a node in a linked list without the access of a head pointer + * @author rakshaa2000 +*/ + +//Delete without a head pointer. +/*You are given a pointer/ reference to the node which is to be deleted from the linked list of N nodes. The task is to delete the node. Pointer/ reference to head node is not given. +Note: No head reference is given to you.*/ + +/* +Example 1: + +Input: +N = 3 +value[] = {1,2,3} +node = 1 +Output: 2 3 +Explanation: After deleting 1 from the +linked list, we have remaining nodes +as 2 and 3. + +Example 2: + +Input: +N = 4 +value[] = {70,20,60,30} +node = 20 +Output: 40 60 30 +Explanation: After deleting 20 from +the linked list, we have remaining +nodes as 40, 60 and 30. + +*/ + +#include +//Driver Code starts +//Linked list node structure +struct Node{ + int data; + Node *next; + explicit Node(int x){ + data=x; + next=nullptr; + } +} *head; + +//To search for the given node +Node *findNode(Node *head, int search){ + Node *cur= head; + while(cur!=nullptr){ + if(cur->data==search) + break; + cur=cur->next; + } +} + +//To populate the linked list +void insert(){ + int i=0, n, value; + Node *temp; + scanf("%d",&n); + for(int i=0; inext=new Node(value); + temp=temp->next; + new Node(value); + temp->next=NULL; + } + } +} + +//To print the entire list +void printList (Node *node){ + while(node!=NULL){ + printf("%d",node->data); + node=node->next; + } + std::cout<next; +} + +//Main function +int main(){ + int k; + insert(); + scanf("%d",&k); + Node *del= findNode(head,k); + if(del!=NULL && del->next!=NULL){ + deleteNode(del); + } + printList(head); + return 0; +} diff --git a/dynamic_programming/fibonacci_top_down.cpp b/dynamic_programming/fibonacci_top_down.cpp deleted file mode 100644 index 3c0c9a1a3..000000000 --- a/dynamic_programming/fibonacci_top_down.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -using namespace std; -int arr[1000000]; -int fib(int n) { - if (arr[n] == -1) { - if (n <= 1) - arr[n] = n; - else - arr[n] = fib(n - 1) + fib(n - 2); - } - return arr[n]; -} -int main(int argc, char const *argv[]) { - int n; - cout << "Enter n: "; - cin >> n; - for (int i = 0; i < n + 1; ++i) { - arr[i] = -1; - } - cout << "Fibonacci number is " << fib(n) << endl; - return 0; -} \ No newline at end of file diff --git a/greedy_algorithms/jumpgame.cpp b/greedy_algorithms/jumpgame.cpp index 6f7a2dd18..94b3b2685 100644 --- a/greedy_algorithms/jumpgame.cpp +++ b/greedy_algorithms/jumpgame.cpp @@ -6,13 +6,10 @@ * This solution takes in input as a vector and output as a boolean to check if you can reach the last position. * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000) */ -//Jump Game: -/*Given an array of non-negative integers, you are initially positioned at the first index of the array. -Each element in the array represents your maximum jump length at that position. -Determine if you are able to reach the last index.*/ #include #include +#include //Implements the algorithm /*We name the indices good and bad based on whether we can reach the destination if we start at that position. @@ -22,6 +19,11 @@ Determine if you are able to reach the last index.*/ *If yes, then that is the last position you can reach starting from the back. *After the end of the loop, if we reach the lastPos as 0, then the destination can be reached from the start position. */ + /** + * @brief This function implements the above algorithm + * @param vector of nums containing the maximum jump (in steps) from that index + * @return returns bool value whether final index can be reached or not + */ bool canJump(std::vector nums) { auto lastPos = nums.size() - 1; for (auto i = nums.size() - 1; i >= 0; i--) { @@ -32,13 +34,35 @@ bool canJump(std::vector nums) { return lastPos == 0; } + +/** + * @brief Function to test above algorithm + */ +void test(){ + //Test 1 + std::vector num1={4,3,1,0,5}; + assert(canJump(num1)==true); + std::cout<<"Input: "; + for(auto i: num1){ + std::cout< num2={3,2,1,0,4}; + assert(canJump(num2)==false); + std::cout<<"Input: "; + for(auto i: num2){ + std::cout< num={4,3,1,0,5}; - std::cout<