Merge pull request #4 from rakshaa2000/master

More documentation and testing
This commit is contained in:
Rakshaa Viswanathan 2020-09-04 11:30:25 +05:30 committed by GitHub
commit 74947cecb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 30 deletions

View File

@ -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)

View File

@ -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 <iostream>
//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; i<n; i++){
scanf("%d", &value);
if(i==0){
head= new Node(value);
temp=head;
continue;
}
else{
temp->next=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<<endl;
}
//Driver Code Ends
//Function to locate the node to be deleted
void deleteNode(Node *m)
{
*m=*m->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;
}

View File

@ -1,22 +0,0 @@
#include <iostream>
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;
}

View File

@ -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 <vector>
#include<iostream>
#include <cassert>
//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<int> nums) {
auto lastPos = nums.size() - 1;
for (auto i = nums.size() - 1; i >= 0; i--) {
@ -32,13 +34,35 @@ bool canJump(std::vector<int> nums) {
return lastPos == 0;
}
/**
* @brief Function to test above algorithm
*/
void test(){
//Test 1
std::vector<int> num1={4,3,1,0,5};
assert(canJump(num1)==true);
std::cout<<"Input: ";
for(auto i: num1){
std::cout<<i<<" ";
}
std::cout<<"Output: true"<<std::endl;
//Test 2
std::vector<int> num2={3,2,1,0,4};
assert(canJump(num2)==false);
std::cout<<"Input: ";
for(auto i: num2){
std::cout<<i<<" ";
}
std::cout<<"Output: false"<<std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main(){
//Sample test case
std::vector<int> num={4,3,1,0,5};
std::cout<<canJump(num); //Should display true, as when you take one step from position 0, you reach position 1, from which 3 steps lead you to the destination
test();
return 0;
}