From 9faa47548841418da385ed9747e487887c1c13a5 Mon Sep 17 00:00:00 2001 From: Nokhalal Mahato Date: Wed, 9 Mar 2022 16:01:25 +0530 Subject: [PATCH] Added binary tree,binary search tree and linked list --- Binary Tree/BT_from_postorder.cpp | 49 ++++++++++ Binary Tree/BT_from_preorder.cpp | 55 +++++++++++ Binary Tree/Binary_tree.cpp | 24 +++++ Binary Tree/Diameter_of_binary_tree.cpp | 38 ++++++++ Binary Tree/Height_balanced_BT.cpp | 68 +++++++++++++ Binary Tree/Inorder_traversal.cpp | 34 +++++++ Binary Tree/Left_view_BT.cpp | 41 ++++++++ Binary Tree/Lowest_common_ancestor.cpp | 76 +++++++++++++++ Binary Tree/Max_sum_path.cpp | 39 ++++++++ Binary Tree/Nodes_at_distance_k_in_BT.cpp | 63 ++++++++++++ Binary Tree/Right_view_BT.cpp | 65 +++++++++++++ Binary Tree/count_of_nodes.cpp | 31 ++++++ Binary Tree/flatten_a_binary_tree.cpp | 61 ++++++++++++ Binary Tree/height_of_binary_tree.cpp | 32 ++++++ Binary Tree/level_order_traversal.cpp | 47 +++++++++ Binary Tree/postorder_traversal.cpp | 34 +++++++ Binary Tree/preorder_traversal.cpp | 34 +++++++ .../shortest_distance_between_two_nodes.cpp | 70 +++++++++++++ Binary Tree/sum_at_k_level.cpp | 55 +++++++++++ Binary Tree/sum_of_nodes.cpp | 31 ++++++ Binary Tree/sum_replacement_in_BT.cpp | 44 +++++++++ Binary search tree/BST_from_preorder.cpp | 44 +++++++++ .../Balanced_BST_from_sorted_array.cpp | 37 +++++++ Binary search tree/Catalan_number.cpp | 18 ++++ Binary search tree/Check_for_BST.cpp | 35 +++++++ Binary search tree/Identical_BST.cpp | 51 ++++++++++ Binary search tree/Largest_BST_in_BT.cpp | 55 +++++++++++ Binary search tree/Recover_BST.cpp | 67 +++++++++++++ Binary search tree/binary_search_tree.cpp | 42 ++++++++ Binary search tree/delete_in_bst.cpp | 68 +++++++++++++ Binary search tree/n_Possible_BST.cpp | 52 ++++++++++ Binary search tree/search_in_bst.cpp | 36 +++++++ Binary search tree/zig_zig_traversal.cpp | 52 ++++++++++ .../Put_even_posistion_node_after_odd.cpp | 60 ++++++++++++ Linked List/append_last_knode_to_start.cpp | 64 ++++++++++++ Linked List/check_cycle.cpp | 72 ++++++++++++++ Linked List/circular_linked_list.cpp | 88 +++++++++++++++++ Linked List/deletion_linked_list.cpp | 73 ++++++++++++++ Linked List/doubly_linked_list.cpp | 81 ++++++++++++++++ Linked List/find_interection_point.cpp | 97 +++++++++++++++++++ Linked List/linked_list.cpp | 47 +++++++++ Linked List/merge_2_sorted_linked_list.cpp | 77 +++++++++++++++ Linked List/remove_cycle.cpp | 89 +++++++++++++++++ Linked List/reverse_k_node.cpp | 64 ++++++++++++ Linked List/reverse_linked_list.cpp | 70 +++++++++++++ Linked List/searching_linked_list.cpp | 41 ++++++++ 46 files changed, 2471 insertions(+) create mode 100644 Binary Tree/BT_from_postorder.cpp create mode 100644 Binary Tree/BT_from_preorder.cpp create mode 100644 Binary Tree/Binary_tree.cpp create mode 100644 Binary Tree/Diameter_of_binary_tree.cpp create mode 100644 Binary Tree/Height_balanced_BT.cpp create mode 100644 Binary Tree/Inorder_traversal.cpp create mode 100644 Binary Tree/Left_view_BT.cpp create mode 100644 Binary Tree/Lowest_common_ancestor.cpp create mode 100644 Binary Tree/Max_sum_path.cpp create mode 100644 Binary Tree/Nodes_at_distance_k_in_BT.cpp create mode 100644 Binary Tree/Right_view_BT.cpp create mode 100644 Binary Tree/count_of_nodes.cpp create mode 100644 Binary Tree/flatten_a_binary_tree.cpp create mode 100644 Binary Tree/height_of_binary_tree.cpp create mode 100644 Binary Tree/level_order_traversal.cpp create mode 100644 Binary Tree/postorder_traversal.cpp create mode 100644 Binary Tree/preorder_traversal.cpp create mode 100644 Binary Tree/shortest_distance_between_two_nodes.cpp create mode 100644 Binary Tree/sum_at_k_level.cpp create mode 100644 Binary Tree/sum_of_nodes.cpp create mode 100644 Binary Tree/sum_replacement_in_BT.cpp create mode 100644 Binary search tree/BST_from_preorder.cpp create mode 100644 Binary search tree/Balanced_BST_from_sorted_array.cpp create mode 100644 Binary search tree/Catalan_number.cpp create mode 100644 Binary search tree/Check_for_BST.cpp create mode 100644 Binary search tree/Identical_BST.cpp create mode 100644 Binary search tree/Largest_BST_in_BT.cpp create mode 100644 Binary search tree/Recover_BST.cpp create mode 100644 Binary search tree/binary_search_tree.cpp create mode 100644 Binary search tree/delete_in_bst.cpp create mode 100644 Binary search tree/n_Possible_BST.cpp create mode 100644 Binary search tree/search_in_bst.cpp create mode 100644 Binary search tree/zig_zig_traversal.cpp create mode 100644 Linked List/Put_even_posistion_node_after_odd.cpp create mode 100644 Linked List/append_last_knode_to_start.cpp create mode 100644 Linked List/check_cycle.cpp create mode 100644 Linked List/circular_linked_list.cpp create mode 100644 Linked List/deletion_linked_list.cpp create mode 100644 Linked List/doubly_linked_list.cpp create mode 100644 Linked List/find_interection_point.cpp create mode 100644 Linked List/linked_list.cpp create mode 100644 Linked List/merge_2_sorted_linked_list.cpp create mode 100644 Linked List/remove_cycle.cpp create mode 100644 Linked List/reverse_k_node.cpp create mode 100644 Linked List/reverse_linked_list.cpp create mode 100644 Linked List/searching_linked_list.cpp diff --git a/Binary Tree/BT_from_postorder.cpp b/Binary Tree/BT_from_postorder.cpp new file mode 100644 index 0000000..8556740 --- /dev/null +++ b/Binary Tree/BT_from_postorder.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +struct Node{ + int data; + struct Node* left; + struct Node* right; + Node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int search(int in[],int curr,int start,int end){ + for(int i=start;i<=end;i++){ + if(in[i]==curr) + return i; + } + return -1; +} +Node* create_tree(int post[],int in[],int start,int end){ + static int idx=end; + if(start>end) + return NULL; + int cur=post[idx]; + idx--; + Node* node=new Node(cur); + if(start==end) + return node; + int pos=search(in,cur,start,end); + node->right=create_tree(post,in,pos+1,end); + node->left=create_tree(post,in,start,pos-1); + return node; +} +void display(Node* node){ + if(node==NULL) + return; + display(node->left); + cout<data<<" "; + display(node->right); +} + +int main(){ + int post[]={4,2,5,3,1}; + int in[]={4,2,1,5,3}; + Node* root=create_tree(post,in,0,4); + display(root); +} diff --git a/Binary Tree/BT_from_preorder.cpp b/Binary Tree/BT_from_preorder.cpp new file mode 100644 index 0000000..728c755 --- /dev/null +++ b/Binary Tree/BT_from_preorder.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int search_pos(int in[],int el,int l,int h){ + + for(int i=l;i<=h;i++){ + if(in[i]==el) + return i; + } + return -1; +} + +node* create_tree(int in[],int pre[],int l,int h){ + static int idx=0; + if(l>h){ + return NULL; + } + int curr=pre[idx]; + idx++; + node* root=new node(curr); + if(l==h){ + return root; + } + int pos=search_pos(in,curr,l,h); + root->left=create_tree(in,pre,l,pos-1); + root->right=create_tree(in,pre,pos+1,h); + return root; +} + +void inorder_traversal(node* root){ + if(root==NULL){ + return; + } + inorder_traversal(root->left); + cout<data<<" "; + inorder_traversal(root->right); +} + +int main(){ + int in[]={4,2,1,5,3}; + int pre[]={1,2,4,3,5}; + node* root=create_tree(in,pre,0,4); + inorder_traversal(root); +} \ No newline at end of file diff --git a/Binary Tree/Binary_tree.cpp b/Binary Tree/Binary_tree.cpp new file mode 100644 index 0000000..4f0b1bf --- /dev/null +++ b/Binary Tree/Binary_tree.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + + +int main(){ + node *root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); +} \ No newline at end of file diff --git a/Binary Tree/Diameter_of_binary_tree.cpp b/Binary Tree/Diameter_of_binary_tree.cpp new file mode 100644 index 0000000..4c29f43 --- /dev/null +++ b/Binary Tree/Diameter_of_binary_tree.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int diameter_of_bt(node* root,int* h){ + if(root==NULL){ + *h=0; + return 0; + } + int lh=0,rh=0; + int leftdiameter=diameter_of_bt(root->left,&lh); + int rightdiameter=diameter_of_bt(root->right,&rh); + int currdiamter=lh+rh+1; + *h=max(lh,rh)+1; + return max(currdiamter,max(rightdiameter,leftdiameter)); +} + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + int h=0; + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int height(node* root){ + if(root==NULL) + return 0; + return max(height(root->left),height(root->right)) + 1; +} + +bool height_balance(node* root){ + if(root==NULL) + return true; + if(height_balance(root->left)==false) + return false; + if(height_balance(root->right)==false) + return false; + int lh=height(root->left); + int rh=height(root->right); + if(abs(lh-rh)<=1) + return true; + else + return false; +} + +bool height_balance_optimised(node* root,int *h){ + if(root==NULL){ + *h=0; + return true; + } + int lh=0,rh=0; + if(height_balance_optimised(root->left,&lh)==false) + return false; + if(height_balance_optimised(root->right,&rh)==false) + return false; + *h=max(lh,rh)+1; + if(abs(lh-rh)<=1) + return true; + else + return false; +} + +int main(){ + node* root2=new node(1); + root2->left=new node(2); + root2->left->left=new node(3); + + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void inorder_traversal(node* root){ + if(root==NULL){ + return; + } + inorder_traversal(root->left); + cout<data<<" "; + inorder_traversal(root->right); +} + + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + inorder_traversal(root); +} \ No newline at end of file diff --git a/Binary Tree/Left_view_BT.cpp b/Binary Tree/Left_view_BT.cpp new file mode 100644 index 0000000..755e7fe --- /dev/null +++ b/Binary Tree/Left_view_BT.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void left_view(node* root){ + queue q; + q.push(root); + while(!q.empty()){ + int s=q.size(); + for(int i=0;idata<<" "; + if(temp->left!=NULL) + q.push(temp->left); + if(temp->right!=NULL) + q.push(temp->right); + } + } +} +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + left_view(root); +} \ No newline at end of file diff --git a/Binary Tree/Lowest_common_ancestor.cpp b/Binary Tree/Lowest_common_ancestor.cpp new file mode 100644 index 0000000..24ba42a --- /dev/null +++ b/Binary Tree/Lowest_common_ancestor.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +node* Lca(node* root,int a,int b){ + if(root==NULL) + return NULL; + if(root->data==a||root->data==b) + return root; + node* left=Lca(root->left,a,b); + node* right=Lca(root->right,a,b); + if(left!=NULL && right!=NULL) + return root; + if(left==NULL && right==NULL) + return NULL; + if(left!=NULL) + return Lca(root->left,a,b); + if(right!=NULL) + return Lca(root->right,a,b); +} + +bool getpath(node* root,int a,vector &path){ + if(root==NULL) + return 0; + path.push_back(root->data); + if(root->data==a) + return 1; + if(getpath(root->left,a,path) || getpath(root->right,a,path)) + return 1; + path.pop_back(); + return 0; +} +int lowest_common_ancestor(node* root,int a,int b){ + vector path1,path2; + + if(!getpath(root,a,path1) || !getpath(root,b,path2)) + return -1; + int i; + for(i=0;ileft=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + int ans=lowest_common_ancestor(root,6,7); + if(ans==-1) + cout<<"Not exist"<data; +} \ No newline at end of file diff --git a/Binary Tree/Max_sum_path.cpp b/Binary Tree/Max_sum_path.cpp new file mode 100644 index 0000000..fc90bf6 --- /dev/null +++ b/Binary Tree/Max_sum_path.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int max_sum_pathutil(node* root,int &ans){ + if(root==NULL) + return 0; + int ls=max_sum_pathutil(root->left,ans); + int rs=max_sum_pathutil(root->right,ans); + int curr=max(max((ls+rs+root->data),root->data),max((ls+root->data),(rs+root->data))); + ans=max(ans,curr); + return max((root->data),max((ls+root->data),(rs+root->data))); +} + +int max_sum_path(node* root){ + int ans=INT_MIN; + max_sum_pathutil(root,ans); + return ans; +} +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void print_subtree(node* root,int k){ + if(root==NULL || k<0) + return; + if(k==0){ + cout<data<<" "; + return; + } + print_subtree(root->left,k-1); + print_subtree(root->right,k-1); +} + +int node_at_distance(node* root,node* target,int k){ + if(root==NULL) + return -1; + if(root==target){ + print_subtree(root,k); + return 0; + } + int dl=node_at_distance(root->left,target,k); + if(dl!=-1){ + if(dl+1==k) + cout<data<<" "; + else{ + print_subtree(root->right,k-dl-2); + return 1+dl; + } + } + int dr=node_at_distance(root->right,target,k); + if(dr!=-1){ + if(dr+1==k) + cout<data<<" "; + else{ + print_subtree(root->left,k-dr-2); + return 1+dr; + } + } + return -1; +} + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + node_at_distance(root,root->right,2); +} \ No newline at end of file diff --git a/Binary Tree/Right_view_BT.cpp b/Binary Tree/Right_view_BT.cpp new file mode 100644 index 0000000..741e0ac --- /dev/null +++ b/Binary Tree/Right_view_BT.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void right_view(node* root){ + queue q; + q.push(root); + while(!q.empty()){ + int s=q.size(); + for(int i=0;idata<<" "; + if(temp->left!=NULL) + q.push(temp->left); + if(temp->right!=NULL) + q.push(temp->right); + } + } +} +void right_view2(node* root){ + queue q; + q.push(root); + q.push(NULL); + int data; + while(!q.empty()){ + node* temp=q.front(); + q.pop(); + if(temp!=NULL){ + data=temp->data; + if(temp->left!=NULL) + q.push(temp->left); + if(temp->right!=NULL) + q.push(temp->right); + } + else if(!q.empty()){ + cout<left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + right_view2(root); +} \ No newline at end of file diff --git a/Binary Tree/count_of_nodes.cpp b/Binary Tree/count_of_nodes.cpp new file mode 100644 index 0000000..9e215cd --- /dev/null +++ b/Binary Tree/count_of_nodes.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int count_nodes(node* root){ + if(root==NULL){ + return 0; + } + return count_nodes(root->left)+count_nodes(root->right)+1; +} + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void flatten_tree(node* root){ + if(root==NULL) + return; + node* temp; + flatten_tree(root->left); + if(root->left!=NULL){ + temp=root->right; + root->right=root->left; + root->left=NULL; + node* t=root; + while(t->right!=NULL){ + t=t->right; + } + t->right=temp; + } + if(temp!=NULL) + flatten_tree(temp); +} + +void display(node* root){ + while(root!=NULL){ + cout<data<<" "; + root=root->right; + } +} +void inorder_traversal(node* root){ + if(root==NULL){ + return; + } + cout<data<<" "; + inorder_traversal(root->left); + inorder_traversal(root->right); +} + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + inorder_traversal(root); + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int cal_height(node* root){ + if(root==NULL) + return 0; + int lh=cal_height(root->left); + int rh=cal_height(root->right); + return max(lh,rh)+1; +} + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void level_order_traversal(node* root){ + if(root==NULL) + return; + queue q; + q.push(root); + q.push(NULL); + while(!q.empty()){ + node* temp=q.front(); + q.pop(); + if(temp!=NULL){ + cout<data<<" "; + if(temp->left !=NULL) + q.push(temp->left); + if(temp->right !=NULL) + q.push(temp->right); + } + else if(!q.empty()){ + q.push(NULL); + cout<left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + level_order_traversal(root); +} \ No newline at end of file diff --git a/Binary Tree/postorder_traversal.cpp b/Binary Tree/postorder_traversal.cpp new file mode 100644 index 0000000..e2fdb27 --- /dev/null +++ b/Binary Tree/postorder_traversal.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void postorder_traversal(node *root){ + if(root==NULL){ + return; + } + postorder_traversal(root->left); + postorder_traversal(root->right); + cout<data<<" "; +} + + +int main(){ + node *root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + postorder_traversal(root); +} \ No newline at end of file diff --git a/Binary Tree/preorder_traversal.cpp b/Binary Tree/preorder_traversal.cpp new file mode 100644 index 0000000..ed3363d --- /dev/null +++ b/Binary Tree/preorder_traversal.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void preorder_traversal(node *root){ + if(root==NULL){ + return; + } + cout<data<<" "; + preorder_traversal(root->left); + preorder_traversal(root->right); +} + + +int main(){ + node *root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + preorder_traversal(root); +} \ No newline at end of file diff --git a/Binary Tree/shortest_distance_between_two_nodes.cpp b/Binary Tree/shortest_distance_between_two_nodes.cpp new file mode 100644 index 0000000..723924f --- /dev/null +++ b/Binary Tree/shortest_distance_between_two_nodes.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +node* lca(node* root,int a,int b){ + if(root==NULL) + return NULL; + + if(root->data==a || root->data==b) + return root; + + node* left=lca(root->left,a,b); + node* right=lca(root->right,a,b); + if(left==NULL && right==NULL) + return NULL; + if(left!=NULL && right!=NULL) + return root; + + if(left!=NULL) + return lca(root->left,a,b); + + if(right!=NULL) + return lca(root->right,a,b); +} + +int finddist(node* root,int a,int h){ + if(root==NULL) + return -1; + if(root->data==a) + return h; + int ld=finddist(root->left,a,h+1); + if(ld!=-1) + return ld; + return finddist(root->right,a,h+1); +} + +int shortest_distance(node* root,int a,int b){ + node* common=lca(root,a,b); + if(common==NULL){ + return -1; + } + int lh=finddist(common,a,0); + int rh=finddist(common,a,0); + return lh+rh; +} + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + int sd=shortest_distance(root,7,5); + if(sd==-1) + cout<<"not exist"; + else + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int sum_at_level(node* root,int k){ + if(root==NULL){ + return -1; + } + queue q; + q.push(root); + q.push(NULL); + int lvl=0; + int sum=0; + while(!q.empty() && lvl<=k){ + node* temp=q.front(); + q.pop(); + if(temp!=NULL){ + if(lvl==k) + sum+=temp->data; + if(temp->left) + q.push(temp->left); + if(temp->right) + q.push(temp->right); + } + else if(!q.empty()){ + q.push(NULL); + lvl++; + } + } + return sum; +} + + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + int k; + cin>>k; + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +int sum_nodes(node* root){ + if(root==NULL) + return 0; + return sum_nodes(root->left)+sum_nodes(root->right)+root->data; +} + + +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + cout< +using namespace std; + +struct node{ + int data; + struct node* left; + struct node* right; + node(int n){ + data=n; + left=NULL; + right=NULL; + } +}; + +void sum_replacement(node* root){ + if(root==NULL) + return; + sum_replacement(root->left); + sum_replacement(root->right); + if(root->left!=NULL) + root->data+=root->left->data; + if(root->right!=NULL) + root->data+=root->right->data; + +} +void display(node* root){ + if(root==NULL){ + return; + } + cout<data<<" "; + display(root->left); + display(root->right); +} +int main(){ + node* root=new node(1); + root->left=new node(2); + root->right=new node(3); + root->left->left=new node(4); + root->left->right=new node(5); + root->right->left=new node(6); + root->right->right=new node(7); + sum_replacement(root); + display(root); +} \ No newline at end of file diff --git a/Binary search tree/BST_from_preorder.cpp b/Binary search tree/BST_from_preorder.cpp new file mode 100644 index 0000000..f6eb180 --- /dev/null +++ b/Binary search tree/BST_from_preorder.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +node* createBST(int pre[],int *pos,int key,int n,int min,int max){ + if(*pos>=n) + return NULL; + node* root=NULL; + if(key>min && keyleft=createBST(pre,pos,pre[*pos],n,min,root->data); + if(*posright=createBST(pre,pos,pre[*pos],n,root->data,max); + } + + return root; +} + +void display(node* root){ + if(root==NULL) + return; + cout<data<<" "; + display(root->left); + display(root->right); +} + +int main(){ + int pre[]={4,2,1,3,5,6}; + int pos=0; + node* root=createBST(pre,&pos,pre[0],6,INT_MIN,INT_MAX); + + display(root); +} \ No newline at end of file diff --git a/Binary search tree/Balanced_BST_from_sorted_array.cpp b/Binary search tree/Balanced_BST_from_sorted_array.cpp new file mode 100644 index 0000000..43d9470 --- /dev/null +++ b/Binary search tree/Balanced_BST_from_sorted_array.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +node* build_BST(int a[],int start,int end){ + if(start>end) + return NULL; + int mid=(start+end)/2; + node* root=new node(a[mid]); + root->left=build_BST(a,start,mid-1); + root->right=build_BST(a,mid+1,end); + return root; +} +void display(node* root){ + if(root==NULL) + return; + display(root->left); + cout<data<<" "; + display(root->right); +} + +int main(){ + int ar[]={10,20,30,40,50,60}; + + node* root=build_BST(ar,0,5); + display(root); +} diff --git a/Binary search tree/Catalan_number.cpp b/Binary search tree/Catalan_number.cpp new file mode 100644 index 0000000..3ec16bd --- /dev/null +++ b/Binary search tree/Catalan_number.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int catalan(int n){ + if(n<=1) + return 1; + int ans=0; + for(int i=0;i<=n-1;i++){ + ans+=catalan(i)*catalan(n-i-1); + } + return ans; +} + +int main(){ + for(int i=0;i<=8;i++){ + cout< +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +bool check_bst(node* root,node* min,node* max){ + if(root==NULL) + return true; + if(min && root->data data) + return false; + if(max && root->data > max->data) + return false; + bool left=check_bst(root->left,min,root); + bool right=check_bst(root->right,root,max); + return left && right; +} + +int main(){ + node* root=new node(4); + root->left=new node(2); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(7); + root->right->right=new node(6); + cout< +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +bool identical(node* root1,node* root2){ + if(root1==NULL && root2==NULL) + return true; + else if(root1==NULL||root2==NULL) + return false; + else{ + bool cur=false; + if(root1->data==root2->data) + cur=true; + bool left=identical(root1->left,root2->left); + bool right=identical(root1->right,root2->right); + if (cur && left && right) + return true; + else + return false; + } +} + +int main(){ + node* root=new node(4); + root->left=new node(2); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(3); + root->right->right=new node(6); + + cout<left=new node(2); + root2->right=new node(5); + root2->left->left=new node(1); + root2->left->right=new node(3); + root2->right->right=new node(7); + + cout< +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + left=NULL; + right=NULL; + } +}; + +struct info{ + int size; + int max; + int min; + int ans; + bool isBST; +}; + +info largest_bst(node* root){ + if(root==NULL){ + return {0,INT_MIN,INT_MAX,0,true}; + } + if(root->left==NULL && root->right==NULL) + return {1,root->data,root->data,1,true}; + + info left=largest_bst(root->left); + info right=largest_bst(root->right); + info cur; + cur.size=(1 + left.size + right.size); + if(left.isBST && right.isBST && left.max < root->data && right.min > root->data){ + cur.min=min(left.min,min(right.min,root->data)); + cur.max=max(right.max,max(left.max,root->data)); + cur.isBST=true; + cur.ans=cur.size; + return cur; + } + cur.isBST=false; + cur.ans=max(left.ans,right.ans); + return cur; +} + + +int main(){ + node* root=new node(4); + root->left=new node(2); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(3); + root->right->right=new node(6); + + cout< +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +void recover_util(node* root,node **first,node** mid,node** last,node** prev){ + if(root==NULL) + return; + recover_util(root->left,first,mid,last,prev); + if(*prev && (*prev)->data>root->data){ + if(!*first){ + *first=*prev; + *mid=root; + } + else{ + *last=root; + } + } + *prev=root; + recover_util(root->right,first,mid,last,prev); +} + + + +void recover(node* root){ + node *first,*mid,*last,*prev; + first=NULL; + last=NULL; + mid=NULL; + prev=NULL; + recover_util(root,&first,&mid,&last,&prev); + if(first && last){ + swap(first->data,last->data); + } + else if(first && mid) + swap(first->data,mid->data); +} + +void display(node* root){ + if(root==NULL) + return; + display(root->left); + cout<data<<" "; + display(root->right); +} + +int main(){ + node* root=new node(4); + root->left=new node(6); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(3); + root->right->right=new node(2); + display(root); + cout< +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +node* create_BST(node* root,int val){ + if(root==NULL){ + return new node(val); + } + if(root->data > val) + root->left=create_BST(root->left,val); + else if(root->data < val) + root->right=create_BST(root->right,val); + return root; +} + +void display(node* root){ + if(root==NULL) + return; + display(root->left); + cout<data<<" "; + display(root->right); +} +int main(){ + node* root=NULL; + root=create_BST(root,5); + root=create_BST(root,1); + root=create_BST(root,3); + root=create_BST(root,4); + root=create_BST(root,2); + root=create_BST(root,7); + display(root); +} \ No newline at end of file diff --git a/Binary search tree/delete_in_bst.cpp b/Binary search tree/delete_in_bst.cpp new file mode 100644 index 0000000..c0962fc --- /dev/null +++ b/Binary search tree/delete_in_bst.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; +node* succesor(node* root){ + node* temp=root; + while(temp && temp->left!=NULL){ + temp=temp->left; + } + return temp; +} + +node* delete_bst(node* root,int val){ + if(root==NULL){ + return NULL; + } + if(root->data >val) + root->left= delete_bst(root->left,val); + else if(root->data < val) + root->right= delete_bst(root->right,val); + else{ + if(root->left==NULL){ + node* temp=root->right; + free(root); + return temp; + } + else if(root->right==NULL){ + node* temp=root->left; + free(root); + return temp; + } + node* s=succesor(root->right); + root->data=s->data; + root->right=delete_bst(root->right,s->data); + } + + return root; +} + +void display(node* root){ + if(root==NULL) + return; + cout<data<<" "; + display(root->left); + display(root->right); + +} +int main(){ + node* root=new node(4); + root->left=new node(2); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(3); + root->right->right=new node(6); + display(root); + cout< +using namespace std; + +struct node{ + int data; + node *left,*right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +vector possible_bst(int start,int end){ + vector trees; + + if(start>end){ + trees.push_back(NULL); + return trees; + } + for(int i=start;i<=end;i++){ + vector lefttree=possible_bst(start,i-1); + vector righttree=possible_bst(i+1,end); + for(int j=0;jleft=left; + root->right=right; + trees.push_back(root); + } + } + } + return trees; +} + +void display(node* root){ + if(root==NULL) + return; + cout<data<<" "; + display(root->left); + display(root->right); +} + +int main(){ + vector totaltree=possible_bst(1,3); + for(int i=0;i +using namespace std; + +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +node* search_BST(node* root,int val){ + if(root==NULL) + return NULL; + if(root->data==val) + return root; + if(root->data > val) + return search_BST(root->left,val); + return search_BST(root->right,val); +} + +int main(){ + node* root=new node(4); + root->left=new node(2); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(3); + root->right->right=new node(6); + if(search_BST(root,6)==NULL) + cout<<"NOt is BST"; + else + cout<data; +} \ No newline at end of file diff --git a/Binary search tree/zig_zig_traversal.cpp b/Binary search tree/zig_zig_traversal.cpp new file mode 100644 index 0000000..7819e03 --- /dev/null +++ b/Binary search tree/zig_zig_traversal.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +struct node{ + int data; + node* left; + node* right; + node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; + +void zigzag(node* root){ + stack stck1; + stack stck2; + stck1.push(root); + bool leftright=true; + while(!stck1.empty()){ + node* temp=stck1.top(); + stck1.pop(); + if(temp!=NULL){ + cout<data<<" "; + if(leftright){ + if(temp->left) + stck2.push(temp->left); + if(temp->right) + stck2.push(temp->right); + } + else{ + if(temp->left) + stck2.push(temp->left); + if(temp->right) + stck2.push(temp->right); + } + } + if(stck1.empty() && !stck2.empty()){ + leftright=!leftright; + swap(stck1,stck2); + cout<left=new node(2); + root->right=new node(5); + root->left->left=new node(1); + root->left->right=new node(3); + root->right->right=new node(6); + zigzag(root); +} \ No newline at end of file diff --git a/Linked List/Put_even_posistion_node_after_odd.cpp b/Linked List/Put_even_posistion_node_after_odd.cpp new file mode 100644 index 0000000..7642d69 --- /dev/null +++ b/Linked List/Put_even_posistion_node_after_odd.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL) + head=el; + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} + + +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; +} +void change_order(node* &head){ + if(head==NULL || head->next==NULL) + return; + node* evenptr=head->next; + node* even=evenptr; + node* oddptr=head; + while(evenptr->next!=NULL && oddptr->next!=NULL){ + oddptr->next=evenptr->next; + oddptr=oddptr->next; + evenptr->next=oddptr->next; + evenptr=evenptr->next; + } + if(oddptr->next==NULL) + evenptr->next=NULL; + oddptr->next=even; +} + +int main(){ + node* head=NULL; + int a[]={1,2,3,4,5,6}; + for(int i=0;i<6;i++){ + insertion(head,a[i]); + } + change_order(head); + display(head); +} \ No newline at end of file diff --git a/Linked List/append_last_knode_to_start.cpp b/Linked List/append_last_knode_to_start.cpp new file mode 100644 index 0000000..e52a23c --- /dev/null +++ b/Linked List/append_last_knode_to_start.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL) + head=el; + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} + +void append(node* &head,int pos){ + node* temp=head; + int l=1; + while(temp->next!=NULL){ + l++; + temp=temp->next; + } + node* point=head; + int c=1; + while(point->next!=NULL && cnext; + } + temp->next=head; + head=point->next; + point->next=NULL; + +} + +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + return; + } + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; +} + +void create_cycle(node* &head, int pos){ + int c=1; + node* temp=head; + node* point=head;; + while(temp->next!=NULL){ + temp=temp->next; + } + while(point->next!=NULL && cnext; + c++; + } + if(c==pos){ + temp->next=point; + } + +} + +int check_cycle(node* head){ + node* slowptr=head; + node* fastptr=head; + while(fastptr->next!=NULL && fastptr!=NULL){ + slowptr=slowptr->next; + fastptr=fastptr->next->next; + if(slowptr==fastptr) + return 1; + } + return -1; +} +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; + cout< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + head->next=head; + } + else{ + node* temp=head; + while(temp->next!=head){ + temp=temp->next; + } + temp->next=el; + el->next=head; + } +} + +void insertion_head(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + head->next=head; + } + el->next=head; + node* temp=head; + while(temp->next!=head){ + temp=temp->next; + } + temp->next=el; + head=el; +} + +void deletion(node* &head,int pos){ + int c=1; + node* temp=head; + while(temp->next!=head && cnext; + } + node* del=temp->next; + temp->next=temp->next->next; + delete del; +} + +void delete_head(node* &head){ + node* del=head; + node* temp=head; + while(temp->next!=head){ + temp=temp->next; + } + temp->next=head->next; + head=head->next; + delete del; +} +void display(node* head){ + node* temp=head; + do{ + cout<data<<"->"; + temp=temp->next; + } + while(temp!=head); + + cout<<"NULL"; +} + +int main(){ + node* head=NULL; + insertion(head,1); + insertion(head,2); + insertion(head,3); + insertion(head,5); + insertion_head(head,4); + deletion(head,3); + delete_head(head); + display(head); +} \ No newline at end of file diff --git a/Linked List/deletion_linked_list.cpp b/Linked List/deletion_linked_list.cpp new file mode 100644 index 0000000..47d4f72 --- /dev/null +++ b/Linked List/deletion_linked_list.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + } + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} +node* delete_head(node* &head){ + if(head==NULL || head->next==NULL){ + delete head; + return NULL; + } + else{ + node* temp=head; + head=head->next; + delete temp; + return head; + } +} + +node* deletion(node* &head,int key){ + if(head->data==key) + return delete_head(head); + else{ + node* temp=head; + while(temp->next!=NULL){ + if(temp->next->data==key){ + node* del=temp->next; + temp->next=del->next; + delete del; + return head; + } + temp=temp->next; + } + return head; + } +} +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; +} + +int main(){ + node* head=NULL; + insertion(head,1); + insertion(head,2); + insertion(head,3); + insertion(head,4); + head=deletion(head,5); + display(head); +} \ No newline at end of file diff --git a/Linked List/doubly_linked_list.cpp b/Linked List/doubly_linked_list.cpp new file mode 100644 index 0000000..098a356 --- /dev/null +++ b/Linked List/doubly_linked_list.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +class node{ + public: + int data; + node* prev; + node* next; + node(int n){ + data=n; + prev=NULL; + next=NULL; + } +}; + +void insertion_head(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + return; + } + head->prev=el; + el->next=head; + head=el; +} +void insertion(node* &head,int n){ + if(head==NULL){ + insertion_head(head,n); + return; + } + node* el=new node(n); + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + el->prev=temp; +} + +void deletion(node* &head,int pos){ + node* temp=head; + int c=1; + while(temp->next!=NULL && cnext; + } + + if(c==pos){ + node* del=temp; + temp->prev->next=temp->next; + temp->next->prev=temp->prev; + delete del; + } +} + +void delete_head(node* &head){ + node* temp=head; + head->next->prev=NULL; + head=head->next; + delete temp; +} + +void display(node* head){ + while(head!=NULL){ + cout<data<<"<->"; + head=head->next; + } + cout<<"NULL"< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL) + head=el; + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} +void make_intersection(node* head,node* &head2,int pos){ + int c=1; + while(head->next!=NULL && cnext; + } + node* temp=head2; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=head; +} + +int find_intersection(node* head,node* head2){ + int l1=0; + int l2=0; + + node* temp1=head; + node* temp2=head2; + while(temp1!=NULL){ + l1++; + temp1=temp1->next; + } + + while(temp2!=NULL){ + l2++; + temp2=temp2->next; + } + + temp1=head; + temp2=head2; + int c=1; + while(temp1!=NULL){ + if(c==(l1-l2)) + break; + temp1=temp1->next; + c++; + } + temp1=temp1->next; + while(temp1!=NULL){ + if(temp1==temp2) + return temp1->data; + temp1=temp1->next; + temp2=temp2->next; + } + return -1; +} + +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL) + head=el; + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} + +void insertion_head(node* &head,int n){ + node* el=new node(n); + el->next=head; + head=el; +} +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; +} + +int main(){ + node* head=NULL; + insertion(head,1); + insertion(head,2); + insertion(head,3); + insertion_head(head,4); + display(head); +} \ No newline at end of file diff --git a/Linked List/merge_2_sorted_linked_list.cpp b/Linked List/merge_2_sorted_linked_list.cpp new file mode 100644 index 0000000..1c35d02 --- /dev/null +++ b/Linked List/merge_2_sorted_linked_list.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL) + head=el; + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} + +node* merge(node* head1,node* head2){ + node* dummy=new node(0); + node* dummy_head=dummy; + while(head1!=NULL && head2!=NULL){ + if(head1->datadata){ + dummy->next=head1; + head1=head1->next; + } + else{ + dummy->next=head2; + head2=head2->next; + } + dummy=dummy->next; + } + while(head1!=NULL){ + dummy->next=head1; + head1=head1->next; + dummy=dummy->next; + } + while(head2!=NULL){ + dummy->next=head2; + head2=head2->next; + dummy=dummy->next; + } + return dummy_head->next; +} + +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + return; + } + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; +} + +void create_cycle(node* &head, int pos){ + int c=1; + node* temp=head; + node* point=head;; + while(temp->next!=NULL){ + temp=temp->next; + } + while(point->next!=NULL && cnext; + c++; + } + if(c==pos){ + temp->next=point; + } + +} + +int check_cycle(node* head){ + node* slowptr=head; + node* fastptr=head; + while(fastptr->next!=NULL && fastptr!=NULL){ + slowptr=slowptr->next; + fastptr=fastptr->next->next; + if(slowptr==fastptr) + return 1; + } + return -1; +} + +void remove_cycle(node* head){ + node* slowptr=head; + node* fastptr=head; + do{ + slowptr=slowptr->next; + fastptr=fastptr->next->next; + }while(slowptr!=fastptr); + fastptr=head; + while(fastptr->next!=slowptr->next){ + fastptr=fastptr->next; + slowptr=slowptr->next; + } + slowptr->next=NULL; +} +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; + cout< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + return; + } + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; +} +node* reverse_k_node(node* &head,int k){ + + int c=0; + node* prev=NULL; + node* current=head; + node* next; + while(current!=NULL && cnext; + current->next=prev; + prev=current; + current=next; + c++; + } + if(next!=NULL) + head->next=reverse_k_node(next,k); + return prev; +} + +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; + cout< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + return; + } + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; +} +node* reverse(node* &head){ + node* prev=NULL; + node* current=head; + node* next; + while (current!=NULL) + { + next=current->next; + current->next=prev; + prev=current; + current=next; + } + return prev; +} + +node* reverse_rec(node* &head){ + if(head==NULL ||head->next==NULL) + return head; + node* newhead=reverse_rec(head->next); + head->next->next=head; + head->next=NULL; + + return newhead; +} + +void display(node* head){ + while(head!=NULL){ + cout<data<<"->"; + head=head->next; + } + cout<<"NULL"; + cout< +using namespace std; + +class node{ + public: + int data; + node* next; + node(int n){ + data=n; + next=NULL; + } +}; + +void insertion(node* &head,int n){ + node* el=new node(n); + if(head==NULL){ + head=el; + } + else{ + node* temp=head; + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=el; + } +} +bool searching(node* head,int key){ + while(head->next!=NULL){ + if(head->data==key) + return true; + head=head->next; + } + return false; +} +int main(){ + node* head=NULL; + insertion(head,1); + insertion(head,2); + insertion(head,3); + cout<