First commit

master
En Yi 2019-08-26 14:23:02 +08:00
commit 6b23f5cdfa
4 changed files with 370 additions and 0 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
*
!.gitignore
!*.cc

112
BST.cc 100644
View File

@ -0,0 +1,112 @@
#include <iostream>
struct node{
int val;
struct node* left_child;
struct node* right_child;
};
node* initialise_BST_node(int val);
void print_BST(node* root);
void delete_BST(node* root);
void insert(node* root, int val);
void delete_node(node* root, int val, node* prev_node);
int main(){
node* root = initialise_BST_node(5);
insert(root, 3);
insert(root, 10);
insert(root, 4);
insert(root, 8);
insert(root, 14);
insert(root, 1);
print_BST(root);
std::cout << std::endl;
delete_node(root, 8, NULL);
print_BST(root);
std::cout << std::endl;
delete_BST(root);
}
node* initialise_BST_node(int val){
node* root = new node();
root->val = val;
root->left_child = NULL;
root->right_child = NULL;
return root;
}
void print_BST(node* root){
if (root->left_child != NULL)
print_BST(root->left_child);
std::cout << root->val << " ";
if (root->right_child != NULL)
print_BST(root->right_child);
}
void delete_BST(node* root){
if (root->left_child != NULL)
delete_BST(root->left_child);
if (root->right_child != NULL)
delete_BST(root->right_child);
delete root;
}
void insert(node* root, int val){
if (val == root->val)
return;
if (val < root->val){
if (root->left_child != NULL)
insert(root->left_child, val);
else{
node* new_node = initialise_BST_node(val);
root->left_child = new_node;
}
}else{
if (root->right_child != NULL)
insert(root->right_child, val);
else{
node* new_node = initialise_BST_node(val);
root->right_child = new_node;
}
}
}
void delete_node(node* root, int val, node* prev_node){
if (root->left_child != NULL && val < root->val)
delete_node(root->left_child, val, root);
if (root->right_child != NULL && val > root->val)
delete_node(root->right_child, val, root);
if (root->val == val){
node* replacing_node = NULL;
if (root->left_child != NULL && root->left_child != NULL){
replacing_node = root->right_child;
while (replacing_node->left_child != NULL){
replacing_node = replacing_node->left_child;
}
}
else if (root->left_child != NULL)
replacing_node = root->left_child;
else if (root->right_child != NULL)
replacing_node = root->right_child;
if (replacing_node != NULL){
replacing_node->left_child = root->left_child;
replacing_node->right_child = root->right_child;
}
if (prev_node != NULL){
if (prev_node->left_child == root)
prev_node->left_child = replacing_node;
if (prev_node->right_child == root)
prev_node->right_child = replacing_node;
}
delete root;
}
}

99
linked_list.cc 100644
View File

@ -0,0 +1,99 @@
#include <iostream>
struct node{
int val;
node *next;
};
node* initiate_linked_list(int val);
void print_linked_list(node* head);
void push(node** head, int val);
int pop(node* head);
int search(node* head, int val);
void delete_linked_list(node* head);
node* recursive_reverse(node* head);
int main(){
node *head = initiate_linked_list(1);
print_linked_list(head);
push(&head, 5);
push(&head, 2);
push(&head, 7);
push(&head, 9);
print_linked_list(head);
head = recursive_reverse(head);
print_linked_list(head);
std::cout << search(head, 7) << std::endl;
std::cout << search(head, 10) << std::endl;
pop(head);
print_linked_list(head);
delete_linked_list(head);
}
node* initiate_linked_list(int val){
node *head = new node();
head->val = val;
return head;
}
void print_linked_list(node* head){
node *current_node = head;
while (current_node != NULL){
std::cout << current_node->val << " ";
current_node = current_node->next;
}
std::cout << std::endl;
}
void push(node** head, int val){
node *new_node = new node();
new_node->val = val;
new_node->next = *head;
*head = new_node;
}
int pop(node* head){
node *current_node = head;
while (current_node->next->next != NULL){
current_node = current_node->next;
}
int val = current_node->next->val;
delete current_node->next;
current_node->next = NULL;
return val;
}
int search(node* head, int val){
node *current_node = head;
int index = 0;
while (current_node != NULL){
if (current_node->val == val)
return index;
current_node = current_node->next;
++index;
}
return -1;
}
void delete_linked_list(node* head){
node *current_node = head;
node *next_node;
while (current_node != NULL){
next_node = current_node->next;
delete current_node;
current_node = next_node;
}
}
node* recursive_reverse(node* head){
node* current_node = head; // For clarity
if (current_node->next != NULL){
node* new_head = recursive_reverse(current_node->next);
current_node->next->next = current_node;
current_node->next = NULL;
return new_head;
}else{
return current_node;
}
}

156
mergesort.cc 100644
View File

@ -0,0 +1,156 @@
/** This is the code for sorting linked list using mergesort
as a practice using C++. Also for practice using Vim :p
Feel free to modify.
**/
#include <iostream>
#include <stdlib.h>
#include <time.h>
struct node{
int val;
node *next;
};
int generate_random_number(int upper_bound);
node* initiate_linked_list(int val);
void print_linked_list(node* head);
void push(node** head, int val);
void delete_linked_list(node* head);
node* merge_sort(node* head);
int main(int argc, char** argv){
int n;
int upper_bound;
// Input checking. Could've used a loop, but I'm not bothered :p
if(argc == 3){
char* endp;
char* endp2;
int val = strtol(argv[1], &endp, 10);
int val2 = strtol(argv[2], &endp2, 10);
if (!*endp && !*endp2){
n = val;
upper_bound = val2;
}else{
std::cout << "Usage: ./mergesort val1 val2"<< std::endl;
std::cout << "val1 is the length of the array, int" << std::endl;
std::cout << "val2 is the upper bound of the random number (exclusive), int" << std::endl;
return 1;
}
}else{
std::cout << "Usage: ./mergesort val1 val2"<< std::endl;
std::cout << "val1 is the length of the array, int" << std::endl;
std::cout << "val2 is the upper bound of the random number (exclusive), int" << std::endl;
return 1;
}
srand (time(NULL));
node* head = initiate_linked_list(generate_random_number(upper_bound));
--n;
for(int i=0;i<n;++i){
push(&head, generate_random_number(upper_bound));
}
std::cout << "Before: "<<std::endl;
print_linked_list(head);
head = merge_sort(head);
std::cout << "After: "<<std::endl;
print_linked_list(head);
delete_linked_list(head);
}
node* merge_sort(node* head){
// Return if list is only one node
if (head->next == NULL)
return head;
// Get to the half way of the list
node *p1 = head, *p2 = head;
while(p1->next != NULL && p1->next->next != NULL){
p1 = p1->next->next;
p2 = p2->next;
}
// Break into two lists
p1 = p2->next;
p2->next = NULL;
// Sort the two halves, recursively
p1 = merge_sort(p1);
//print_linked_list(p1);
p2 = merge_sort(head);
//print_linked_list(p2);
node *newHead = NULL;
node *current;
node **smaller; // Address of pointer of smaller value
// If either still have an element
while(p1 != NULL || p2 != NULL){
// Get whichever address of pointer to smaller value
// Get the other if one is null
if(p1 == NULL)
smaller = &p2;
else if(p2 == NULL)
smaller = &p1;
else{
if(p1->val < p2->val)
smaller = &p1;
else
smaller = &p2;
}
// If newHead is null (first element), make one
// Otherwise append the current node
// Require dereferencing the smaller variable
if (newHead == NULL){
newHead = *smaller;
}else{
current->next = *smaller;
}
// Make the current pointer at the appended node
current = *smaller;
// Shift the pointer of the smaller value to the next
*smaller = (*smaller)->next;
}
return newHead;
}
int generate_random_number(int upper_bound){
return rand() % upper_bound;
}
node* initiate_linked_list(int val){
node *head = new node();
head->val = val;
return head;
}
void print_linked_list(node* head){
node *current_node = head;
while (current_node != NULL){
std::cout << current_node->val << " ";
current_node = current_node->next;
}
std::cout << std::endl;
}
void push(node** head, int val){
node *new_node = new node();
new_node->val = val;
new_node->next = *head;
*head = new_node;
}
void delete_linked_list(node* head){
node *current_node = head;
node *next_node;
while (current_node != NULL){
next_node = current_node->next;
delete current_node;
current_node = next_node;
}
}