Merge pull request #2 from rakshaa2000/patch-3

Patch 3
This commit is contained in:
Rakshaa Viswanathan
2020-09-01 10:59:06 +05:30
committed by GitHub
3 changed files with 109 additions and 23 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;
Node(int x){
data=x;
next=NULL;
}
} *head;
//To search for the given node
Node *findNode(Node *head, int search){
Node *cur= head;
while(cur!=NULL){
if(cur->data==search)
break;
cur=cur->next;
}
}
//To populate the linked list
void insert(){
int i, 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, n, val;
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;
}