Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions CODE in C/Data Structures/node_delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

#include <bits/stdc++.h>
using namespace std;

/* Link list node */
struct Node {
int data;
struct Node* next;
};

// Fucntion to delete the node without head
void deleteNodeWithoutHead(struct Node* pos)
{
if (pos == NULL) // If linked list is empty
return;
else {

if (pos->next == NULL) {
printf("This is last node, require head, can't be freed\n");
return;
}

struct Node* temp = pos->next;

// Copy data of the next node to current node
pos->data = pos->next->data;

// Perform conventional deletion
pos->next = pos->next->next;

free(temp);
}
}

// Function to print the linked list
void print(Node* head)
{
Node* temp = head;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
}

cout << "NULL";
}

void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node();

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

// Driver Code
int main()
{
/* Start with the empty list */
struct Node* head = NULL;

// create linked 35->15->4->20
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 35);
cout << "Initial Linked List: \n";
print(head);
cout << endl
<< endl;

// Delete 15 without sending head
Node* del = head->next;
deleteNodeWithoutHead(del);

// Print the final linked list
cout << "Final Linked List after deletion of 15:\n";
print(head);

return 0;

// This code has been contributed by Striver
}
16 changes: 16 additions & 0 deletions CODE in C/Mathematical/Factorial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
24 changes: 24 additions & 0 deletions CODE in C/Mathematical/factorial_in_c++.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;

int factorial(int n);

int main()
{
int n;

cout << "Enter a positive integer: ";
cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

return 0;
}

int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}