diff --git a/BINARY TREE Diameter Of a Tree Optimized Approach b/BINARY TREE Diameter Of a Tree Optimized Approach new file mode 100644 index 0000000..73242b6 --- /dev/null +++ b/BINARY TREE Diameter Of a Tree Optimized Approach @@ -0,0 +1,61 @@ +#include +#include +using namespace std; +class node{ + public: + int data; + node*left; + node*right; + + node(int d){ + data = d; + left = NULL; + right = NULL; + } +}; +node* buildTree(){ + int d; + cin>>d; + + if(d==-1){ + return NULL; + } + node * root = new node(d); + root->left = buildTree(); + root->right = buildTree(); + return root; +} +class Pair{ + public: + int height; + int diameter; +}; + +Pair fastDiameter(node*root){ + Pair p; + if(root==NULL){ + p.diameter = p.height = 0; + return p; + } + //Otherwise + Pair left = fastDiameter(root->left); + Pair right = fastDiameter(root->right); + + p.height = max(left.height,right.height) + 1; + p.diameter = max(left.height+right.height, max(left.diameter,right.diameter)); + return p; +} + +int main(){ + node* root = buildTree(); + Pair p = fastDiameter(root); + cout< +#include +using namespace std; + +class node{ + public: + int data; + node*left; + node*right; + + node(int d){ + data = d; + left = NULL; + right = NULL; + } +}; + +//Accepts the old root node & data and returns the new root node +node* insertinBST(node *root,int data){ + //Base Case + if(root==NULL){ + return new node(data); + } + //Rec Case - Insert in the Subtree and Update Pointers + if(data<=root->data){ + root->left = insertinBST(root->left,data); + } + else{ + root->right = insertinBST(root->right,data); + } + return root; +} + +bool search(node*root,int data){ + if(root==NULL){ + return false; + } + if(root->data==data){ + return true; + } + //Recursively search in left and right subtree + if(data<=root->data){ + return search(root->left,data); + } + else{ + return search(root->right,data); + } +} + +node* build(){ + //Read a list of numbers till -1 and also these numbers will be inserted into BST + int d; + cin>>d; + + node*root = NULL; + + while(d!=-1){ + root = insertinBST(root,d); + cin>>d; + } + return root; +} +//Print the BST Level By Level +void bfs(node *root){ + queue q; + q.push(root); + q.push(NULL); + + while(!q.empty()){ + node* f = q.front(); + if(f==NULL){ + cout<data<<","; + q.pop(); + + if(f->left){ + q.push(f->left); + } + if(f->right){ + q.push(f->right); + } + } + } + return; +} +node* add(node* root,int data) +{ + if(root==NULL) + { + return new node(data); + } + if(data<=root->data) + { + root->left=add(root->left,data); + } + if(data>=root->data) + { + root->right=add(root->right,data); + } + return root; +} +int main() +{ + node*root=build(); + bfs(root); + int data; + cin>>data; + add(root,data); + cout< +#include +#include +using namespace std; +class node +{ + public: + int data; + node*left; + node*right; + node(int d) + { + data=d; + left=NULL; + right=NULL; + } +}; +bool isBST(node* root ,int minV=INT_MIN, int maxV=INT_MAX) +{ + if(root==NULL) + { + return true; + } + if(root->data>=minV and root->data<=maxV and isBST(root->left,minV,root->data) and isBST(root->right,root->data,maxV)) + { + return true; + } + else + { + return false; + } +} +node* insertinBST(node*root,int data) //Accepts the old root node & data & returns the new node +{ + if(root==NULL) //base case + { + return new node(data); + } + if(data<=root->data) + { + root->left=insertinBST(root->left,data); + } + else + { + root->right=insertinBST(root->right,data); + } + return root; +} +node*build() //Read a list of numbers till -1 and also these numbers will be inserted in the BST +{ + int d; + cin>>d; + node*root=NULL; + while(d!=-1) // as -1 encountered insertion stops + { + root=insertinBST(root,d); + cin>>d; + } + return root; +} +void bfs(node*root) //to print the tree +{ + queueq; + q.push(root); + q.push(NULL); + while(!q.empty()) + { + node*f=q.front(); + if(f==NULL) + { + cout<data<<","; + q.pop(); + if(f->left) + { + q.push(f->left); + } + if(f->right) + { + q.push(f->right); + } + } + } + return ; +} +void inorder(node*root) //printing left, root then right +{ + if(root==NULL) + { + return; + } + inorder(root->left); + cout<data<<","; + inorder(root->right); +} +int main() +{ + node*root=build(); + inorder(root); + cout< +#include +using namespace std; + +class node{ + public: + int data; + node*left; + node*right; + + node(int d){ + data = d; + left = NULL; + right = NULL; + } +}; + +//Accepts the old root node & data and returns the new root node +node* insertInBST(node *root,int data){ + //Base Case + if(root==NULL){ + return new node(data); + } + //Rec Case - Insert in the Subtree and Update Pointers + if(data<=root->data){ + root->left = insertInBST(root->left,data); + } + else{ + root->right = insertInBST(root->right,data); + } + return root; +} +node* build(){ + //Read a list of numbers till -1 and also these numbers will be inserted into BST + int d; + cin>>d; + + node*root = NULL; + + while(d!=-1){ + root = insertInBST(root,d); + cin>>d; + } + return root; +} +//Print the BST Level By Level +void bfs(node *root){ + queue q; + q.push(root); + q.push(NULL); + + while(!q.empty()){ + node* f = q.front(); + if(f==NULL){ + cout<data<<","; + q.pop(); + + if(f->left){ + q.push(f->left); + } + if(f->right){ + q.push(f->right); + } + } + } + return; +} + +node* deleteinBST(node*root,int data) +{ + if(root==NULL) + { + return NULL; + } + else if(datadata) + { + root->left=deleteinBST(root->left,data); + return root; + } + else if(data==root->data) + { + //1.node with 0 child- LEAF NODE + if(root->left==NULL and root->right==NULL) + { + delete root; + return root; + } + //2.node with 1 child + if(root->left!=NULL and root->right==NULL) //no right child + { + node*temp=root->left; //storing the left subtree in temp + delete root; //then deleting the root + return temp; //returning the left subtree root to the node above the deleted root node + } + if(root->right!=NULL and root->left==NULL) //no left child + { + node*temp=root->right; + delete root; + return temp; + } + //3.node with 2 children***special case + node* replace= root->right; + //find the inorder successor from right subtree + while(replace->left!=NULL) //since the left of the sunbtree is balanced no change is needed. root is replaced with the right subtree's left or the last element + { //that is taking the pointer to shortest element of right subtree and then + replace=replace->left; + } + root->data=replace->data; + root->right=deleteinBST(root->right,replace->data); + return root; + } + else + { + root->right=deleteinBST(root->right,data); + return root; + } +} +//Inorder Print +void inorder(node*root) +{ + if(root==NULL){ + return; + } + inorder(root->left); + cout<data<<","; + inorder(root->right); +} +int main() +{ + node*root=build(); + inorder(root); + int d; + cin>>d; + root=deleteinBST(root,d); + inorder(root); + cout< +#include +using namespace std; +class node +{ + public: + int data; + node*left; + node*right; + node(int d) + { + data=d; + left=NULL; + right=NULL; + } +}; +node* insertinBST(node*root,int data) //Accepts the old root node & data & returns the new node +{ + if(root==NULL) //base case + { + return new node(data); + } + if(data<=root->data) + { + root->left=insertinBST(root->left,data); + } + else + { + root->right=insertinBST(root->right,data); + } + return root; +} +node*build() //Read a list of numbers till -1 and also these numbers will be inserted in the BST +{ + int d; + cin>>d; + node*root=NULL; + while(d!=-1) // as -1 encountered insertion stops + { + root=insertinBST(root,d); + cin>>d; + } + return root; +} +void bfs(node*root) //to print the tree +{ + queueq; + q.push(root); + q.push(NULL); + while(!q.empty()) + { + node*f=q.front(); + if(f==NULL) + { + cout<data<<","; + q.pop(); + if(f->left) + { + q.push(f->left); + } + if(f->right) + { + q.push(f->right); + } + } + } + return ; +} +void inorder(node*root) //printing left, root then right +{ + if(root==NULL) + { + return; + } + inorder(root->left); + cout<data<<","; + inorder(root->right); +} +int main() +{ + node*root=build(); + inorder(root); + cout< +#include +using namespace std; + +class node{ + public: + int data; + node*left; + node*right; + + node(int d){ + data = d; + left = NULL; + right = NULL; + } +}; + +//Accepts the old root node & data and returns the new root node +node* insertinBST(node *root,int data){ + //Base Case + if(root==NULL){ + return new node(data); + } + //Rec Case - Insert in the Subtree and Update Pointers + if(data<=root->data){ + root->left = insertinBST(root->left,data); + } + else{ + root->right = insertinBST(root->right,data); + } + return root; +} + +bool search(node*root,int data){ + if(root==NULL){ + return false; + } + if(root->data==data){ + return true; + } + //Recursively search in left and right subtree + if(data<=root->data){ + return search(root->left,data); + } + else{ + return search(root->right,data); + } +} + +node* build(){ + //Read a list of numbers till -1 and also these numbers will be inserted into BST + int d; + cin>>d; + + node*root = NULL; + + while(d!=-1){ + root = insertinBST(root,d); + cin>>d; + } + return root; +} +//Print the BST Level By Level +void bfs(node *root){ + queue q; + q.push(root); + q.push(NULL); + + while(!q.empty()){ + node* f = q.front(); + if(f==NULL){ + cout<data<<","; + q.pop(); + + if(f->left){ + q.push(f->left); + } + if(f->right){ + q.push(f->right); + } + } + } + return; +} +//Inorder Print +void inorder(node*root){ + if(root==NULL){ + return; + } + inorder(root->left); + cout<data<<","; + inorder(root->right); +} +int main() +{ + node*root=build(); + inorder(root); + cout<>s; + if(search(root,s)) + { + cout<<"Present!!"; + } + else + { + cout<<"Not Present"; + } + return 0; +} +/* +i/p: +5 3 7 1 6 8 -1 +5 +o/p: +1,3,5,6,7,8, +5, +3,7, +1,6,8, +Present!! +*/ diff --git a/Binary Search Tree/BST To Sorten Linked List into Flatten Tree.cpp b/Binary Search Tree/BST To Sorten Linked List into Flatten Tree.cpp new file mode 100644 index 0000000..a7e9b67 --- /dev/null +++ b/Binary Search Tree/BST To Sorten Linked List into Flatten Tree.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; +class node +{ + public: + int data; + node*left; + node*right; + node(int d) + { + data=d; + left=NULL; + right=NULL; + } +}; +node* insertinBST(node*root,int data) +{ + if(root==NULL) + { + return new node(data); + } + if(datadata) + { + root->left=insertinBST(root->left,data); + } + else + { + root->right=insertinBST(root->right,data); + } + return root; +} +class linkedlist +{ + public: + node* head; + node* tail; +}; +linkedlist flatten(node*root) +{ + linkedlist l; + +} +node* build() +{ + node*root=NULL; + int d; + cin>>d; + if(d!=-1) + { + insertinBST(root,d); + cin>>d; + } + return root; +} diff --git a/HASHTABLE/HASHTABLE Implementation.cpp b/HASHTABLE/HASHTABLE Implementation.cpp new file mode 100644 index 0000000..59af842 --- /dev/null +++ b/HASHTABLE/HASHTABLE Implementation.cpp @@ -0,0 +1,102 @@ +#include +#include +using namespace std; +template +class Node +{ + public: + string key; + T value; + Node*next; + Node(string key,T val) //constructor + { + this->key=key; + value=val; + next=NULL; + } + ~Node() //destructor to delete a node + { + if(next!=NULL) + { + delete next; + } + } +}; +template +class Hashtable +{ + Node** table; //pointer to an array of pointers + int current_size; + int table_size; + + int hashfn(string key) + { + int idx=0; + int p=1; + for(int j=0;j*[table_size]; // dynamically allocating the table + current_size=0; + for(int i=0;i*n=new Node (key,value); //node of linked list containing key and value + //insert at the head of the linked list with id=idx + n->next=table[idx]; //assinging the next node of linked list to the index of the table + table[idx]=n; // storing the node at the starting + current_size++; + //rehash.... + } + void print() + { + for(int i=0;i"; + Node*temp=table[i]; + while(temp!=NULL) + { + cout<key<<"->"; + temp=temp->next; + } + cout< price_menu; + price_menu.insert("Burger",120); + price_menu.insert("Pepsi",20); + price_menu.insert("BurgerPizza",150); + price_menu.insert("Noodles",25); + price_menu.insert("Coke",40); + price_menu.print(); + return 0; +} +/* +o/p: + +Bucket 0-> +Bucket 1-> +Bucket 2->Noodles-> +Bucket 3->Burger-> +Bucket 4->Coke->Pepsi-> +Bucket 5->BurgerPizza-> +Bucket 6-> +*/ diff --git a/HASHTABLE/HASHTABLE Implementation.h b/HASHTABLE/HASHTABLE Implementation.h new file mode 100644 index 0000000..c663a16 --- /dev/null +++ b/HASHTABLE/HASHTABLE Implementation.h @@ -0,0 +1,67 @@ +#include +#include +using namespace std; +template +class Node +{ + public: + string key; + T value; + Nodenext; + Node(string key,T val) //constructor + { + this->key=key; + value=val; + next=NULL; + } + ~Node() //destructor + { + if(next!=NULL) + { + delete next; + } + } +}; +template +class Hashtable +{ + Node** table; //pointer to an array of pointers + int current_size; + int table_size; + + int hashfn(string key) + { + int idx=0; + int p=1; + for(int j=0;j*[table_size]; + current_size=0; + for(int i=0;i*n=new Node (key,value); + //insert at the head of the linked list with id=idx + n->next=table[idx]; + table[idx]=n; + current_size++; + //rehash.... + } + //void print() +}; diff --git a/HASHTABLE/HASHTABLE print Implementation.cpp b/HASHTABLE/HASHTABLE print Implementation.cpp new file mode 100644 index 0000000..8d95f5c --- /dev/null +++ b/HASHTABLE/HASHTABLE print Implementation.cpp @@ -0,0 +1,26 @@ +#include +#include +#include"hastable.h" +using namespace std; +int main() +{ + Hashtable price_menu; + price_menu.insert("Burger",120); + price_menu.insert("Pepsi",20); + price_menu.insert("BurgerPizza",150); + price_menu.insert("Noodles",25); + price_menu.insert("Coke",40); + price_menu.print(); + return 0; +} +/* +o/p: + +Bucket 0-> +Bucket 1-> +Bucket 2->Noodles-> +Bucket 3->Burger-> +Bucket 4->Coke->Pepsi-> +Bucket 5->BurgerPizza-> +Bucket 6-> +*/ diff --git a/HASHTABLE/empty b/HASHTABLE/empty new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/HASHTABLE/empty @@ -0,0 +1 @@ + diff --git a/HASHTABLE/hastable.h b/HASHTABLE/hastable.h new file mode 100644 index 0000000..8649b18 --- /dev/null +++ b/HASHTABLE/hastable.h @@ -0,0 +1,80 @@ +#include +#include +using namespace std; +template +class Node +{ + public: + string key; + T value; + Node*next; + Node(string key,T val) //constructor + { + this->key=key; + value=val; + next=NULL; + } + ~Node() //destructor + { + if(next!=NULL) + { + delete next; + } + } +}; +template +class Hashtable +{ + Node** table; //pointer to an array of pointers + int current_size; + int table_size; + + int hashfn(string key) + { + int idx=0; + int p=1; + for(int j=0;j*[table_size]; + current_size=0; + for(int i=0;i*n=new Node (key,value); + //insert at the head of the linked list with id=idx + n->next=table[idx]; + table[idx]=n; + current_size++; + //rehash.... + } + void print() + { + for(int i=0;i"; + Node*temp=table[i]; + while(temp!=NULL) + { + cout<key<<"->"; + temp=temp->next; + } + cout< +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +int length(node*head) +{ + int len=0; + while(head!=NULL) + { + head=head->next; + len+=1; + } + return len; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATmiddle(node*&head,int data,int p) +{ + if(head==NULL or p==0) + { + insertAThead(head,data); + } + else if(p>length(head)) + { + insertATtail(head,data); + } + else + { + int jump=1; + node*temp=head; + while(jump<=p-1) + { + temp=temp->next; + jump+=1; + } + node* n=new node(data); + n->next=temp->next; + temp->next=n; + } +} + +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<next!=NULL) + { + prev=temp; + temp=temp->next; + } + delete temp; + prev->next=NULL; + return; +} +void deleteathead(node*&head) +{ + if(head==NULL) + { + return; + } + node*temp=head; + head=head->next; + delete temp; + return; +} +int main() +{ + node*head=NULL; + insertAThead(head,5); + insertAThead(head,4); + insertAThead(head,3); + insertATtail(head,6); + insertATmiddle(head,2,3); + deleteathead(head); + deleteattail(head); + print(head); + return 0; +} diff --git a/LINKED LIST/Even-After-Odd.cpp b/LINKED LIST/Even-After-Odd.cpp new file mode 100644 index 0000000..d4a1900 --- /dev/null +++ b/LINKED LIST/Even-After-Odd.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data =d; + next=NULL; + } +}; +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void buildlist(node*&head,int n) +{ + int data; + while(n--) + { + cin>>data; + insertattail(head,data); + } +} +node*place(node*&head) +{ + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<>n; + buildlist(head,n); + place(head); + print(head); + return 0; +} diff --git a/LINKED LIST/Even-After-Odd2.cpp b/LINKED LIST/Even-After-Odd2.cpp new file mode 100644 index 0000000..ce78d45 --- /dev/null +++ b/LINKED LIST/Even-After-Odd2.cpp @@ -0,0 +1,68 @@ +#include +#include +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data = d; + next = NULL; + } +}; +void insertattail(node*&head , int data) +{ + if(head == NULL) + { + head = new node(data); + return; + } + node *temp = head; + while(temp->next!=NULL) + { + temp = temp ->next; + } + node *n = new node(data); + temp -> next = n ; + return; +} +void build(node*&head) +{ + int length; + cin>>length; + int data; + for(int i = 0 ; i < length ; i++) + { + cin>>data; + insertattail(head,data); + } +} +void dist(node *&head , list l) +{ + node*temp = head; + while(temp != NULL) + { + if(temp->data % 2 != 0) + { + cout<data<<" "; + temp = temp -> next ; + } + else + { + l.push_back(temp->data); + temp = temp -> next; + } + } + for(int i:l){ + cout< l; + node*head = NULL; + build(head); + dist(head,l); +} diff --git a/LINKED LIST/Forward_list_STL.cpp b/LINKED LIST/Forward_list_STL.cpp new file mode 100644 index 0000000..eef5f97 --- /dev/null +++ b/LINKED LIST/Forward_list_STL.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; +int main() +{ + list l; + list l1{1,2,3,4,5}; + listl2{"apple","guava","mango","banana"}; + l2.push_back("pineapple"); + l2.sort(); + l2.reverse(); + cout<"; + } + cout<"; + } +} diff --git a/LINKED LIST/Forward_list_STL2.cpp b/LINKED LIST/Forward_list_STL2.cpp new file mode 100644 index 0000000..0fd7994 --- /dev/null +++ b/LINKED LIST/Forward_list_STL2.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; +int main() +{ + list l; + list l1{1,2,3,4,5}; + listl2{"apple","guava","mango","banana"}; + l2.push_back("pineapple"); + l2.sort(); + l2.reverse(); + cout<>f; + l2.remove(f); + // or else like + l2.remove("mango"); + //removing 3rd element + auto it=l2.begin(); + it++; + it++; + l2.erase(it); + //insert + it=l2.begin(); + it++; + l2.insert(it,"fruit juice"); + for(auto it=l2.begin();it!=l2.end();it++) + { + cout<<(*it)<<"->"; + } + cout<"; + } +} diff --git a/LINKED LIST/INSERTION_head_tail_btw.cpp b/LINKED LIST/INSERTION_head_tail_btw.cpp new file mode 100644 index 0000000..aebdde0 --- /dev/null +++ b/LINKED LIST/INSERTION_head_tail_btw.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +int length(node*head) +{ + int len=0; + while(head!=NULL) + { + head=head->next; + len+=1; + } + return len; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATmiddle(node*&head,int data,int p) +{ + if(head==NULL or p==0) + { + insertAThead(head,data); + } + else if(p>length(head)) + { + insertATtail(head,data); + } + else + { + int jump=1; + node*temp=head; + while(jump<=p-1) + { + temp=temp->next; + jump+=1; + } + node* n=new node(data); + n->next=temp->next; + temp->next=n; + } +} + +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout< +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<" ->"; + head=head->next; + } + cout< +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data =d; + next=NULL; + } +}; +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +node*fndlst(node*head,int k) +{ + node*fast=head; + node*slow=head; + while(k--) + { + fast=fast->next; + } + while(fast!=NULL) + { + fast=fast->next; + slow=slow->next; + } + return slow; +} +void buildlist(node*&head) +{ + int data; + cin>>data; + while(data!=-1) + { + insertattail(head,data); + cin>>data; + } +} +int main() +{ + node*head=NULL; + buildlist(head); + int k; + cin>>k; + node*m=fndlst(head,k); + cout<data; + return 0; +} diff --git a/LINKED LIST/Kth node_from end.cpp b/LINKED LIST/Kth node_from end.cpp new file mode 100644 index 0000000..65cbe41 --- /dev/null +++ b/LINKED LIST/Kth node_from end.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data =d; + next=NULL; + } +}; +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<next; + len+=1; + } + return len; +} +node*fndlst(node*head,int k) +{ + node*fast=head; + node*slow=head; + while(k--) + { + fast=fast->next; + } + while(fast!=NULL) + { + fast=fast->next; + slow=slow->next; + } + return slow; +} +void buildlist(node*&head) +{ + int data; + cin>>data; + while(data!=-1) + { + insertattail(head,data); + cin>>data; + } +} +int main() +{ + node*head=NULL; + buildlist(head); + print(head); + int k; + cin>>k; + node*m=fndlst(head,k); + cout<data; + return 0; +} diff --git a/LINKED LIST/Linked List-K Append.cpp b/LINKED LIST/Linked List-K Append.cpp new file mode 100644 index 0000000..740726c --- /dev/null +++ b/LINKED LIST/Linked List-K Append.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data =d; + next=NULL; + } +}; +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} + +void buildlist(node*&head,int n) +{ + int data; + while(n--) + { + cin>>data; + insertattail(head,data); + } +} +int length(node*head) +{ + int len=0; + while(head!=NULL) + { + head=head->next; + len++; + } + return len; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<0) + { + head=head->next; + } + while((length(head)-k-1)>0) + { + ptr1=ptr1->next; + } + ptr1->next=NULL; + while(length(head)>0) + { + ptr2=ptr2->next; + } + ptr2->next=temp; +} +int main() +{ + node* head=NULL; + int n,k; + cin>>n; + buildlist(head,n); + cin>>k; + append(head,k); + print(head); + return 0; +} diff --git a/LINKED LIST/LinkedList_atHead.cpp b/LINKED LIST/LinkedList_atHead.cpp new file mode 100644 index 0000000..1095e45 --- /dev/null +++ b/LINKED LIST/LinkedList_atHead.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<" ->"; + head=head->next; + } + cout< +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data=d; + next=NULL; + } +}; +void insertathead(node*&head,int data) +{ + node*n=new node(data); + n->next=head; + head=n; +} +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<next==NULL) + { + return head; + } + node*slow=head; + node*fast=head->next; + while(fast!=NULL and fast->next!=NULL) + { + fast=fast->next->next; + slow=slow->next; + } + return slow; +} +int main() +{ + node*head=NULL; + insertathead(head,1); + insertathead(head,2); + insertathead(head,3); + insertathead(head,4); + insertattail(head,5); + node*m=mid(head); + print(head); + cout<data< +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data=d; + next=NULL; + } +}; +void insertathead(node*&head,int data) +{ + node*n=new node(data); + n->next=head; + head=n; +} +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<next==NULL) + { + return head; + } + node*slow=head; + node*fast=head->next; + while(fast!=NULL and fast->next!=NULL) + { + fast=fast->next->next; + slow=slow->next; + } + return slow; +} +void buildlist(node*&head) +{ + int data; + cin>>data; + while(data!=-1) + { + insertattail(head,data); + cin>>data; + } +} +int main() +{ + node*head=NULL; + buildlist(head); + print(head); + node*m=mid(head); + cout<data< +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data=d; + next=NULL; + } +}; +node*merge(node*a,node*b) +{ + if(a==NULL) + { + return b; + } + else if(b==NULL) + { + return a; + } + node*c; + if(a->datadata) + { + c=a; + c->next=merge(a->next,b); + } + else + { + c=b; + c->next=merge(a,b->next); + } + return c; +} +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void buildlist(node*&head) +{ + int data; + cin>>data; + while(data!=-1) + { + insertattail(head,data); + cin>>data; + } +} +void print(node*head) +{ + node*temp=head; + while(temp!=NULL) + { + cout<data<<"->"; + temp=temp->next; + } + cout<> (istream &is, node*&head) +{ + buildlist(head); + return is; +} +ostream& operator<< (ostream &os, node*&head) +{ + print(head); + return os; +} + +int main() +{ + node*head=NULL; + node*head2=NULL; + cin>>head>>head2; + cout< +using namespace std; +class node +{ + public: + int data; + node*next; + node(int d) + { + data=d; + next=NULL; + } +}; +node*merge(node*a,node*b) +{ + if(a==NULL) + { + return b; + } + else if(b==NULL) + { + return a; + } + node*c; + if(a->datadata) + { + c=a; + c->next=merge(a->next,b); + } + else + { + c=b; + c->next=merge(a,b->next); + } + return c; +} +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void buildlist(node*&head,int n) +{ + int data; + while(n--) + { + cin>>data; + insertattail(head,data); + } +} +void print(node*head) +{ + node*temp=head; + while(temp!=NULL) + { + cout<data<<"->"; + temp=temp->next; + } + cout<>t; + for(int i=0;i>n; + buildlist(head,n); + cin>>m; + buildlist(head2,m); + head=merge(head,head2); + } + for(int i=0;i +#include +using namespace std; +int main() +{ + listl; + list eve; + list odd; + int n,d; + cin>>n; + for(int i=0;i>d; + l.push_back(d); + } + for(int i=0;i"; + } + for(auto x:eve) + { + cout<"; + } + +} +/* +i/p: +10 +1 2 3 4 5 6 7 8 9 10 +o/p: +1->3->5->7->9->2->4->6->8->10-> +*/ diff --git a/LINKED LIST/OVERLOADING_operator.cpp b/LINKED LIST/OVERLOADING_operator.cpp new file mode 100644 index 0000000..d5db932 --- /dev/null +++ b/LINKED LIST/OVERLOADING_operator.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; +class node +{ + public: + int data; + node*next; + node(int data) + { + this->data=data; + next=NULL; + } +}; +void insertattail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void buildlist(node*&head) +{ + int data; + cin>>data; + while(data!=-1) + { + insertattail(head,data); + cin>>data; + } +} +void print(node*head) +{ + node*temp=head; + while(temp!=NULL) + { + cout<data<<"->"; + temp=temp->next; + } + cout<>(istream &is,node*&head) +{ + buildlist(head); + return is; +} +ostream& operator<<(ostream &os,node*&head) +{ + print(head); + return os; +} +int main() +{ + node*head=NULL; + node*head2=NULL; + cin>>head>>head2; + cout<2->3-> + + 4->5->6-> */ diff --git a/LINKED LIST/REVERSE.cpp b/LINKED LIST/REVERSE.cpp new file mode 100644 index 0000000..2755cfe --- /dev/null +++ b/LINKED LIST/REVERSE.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; +class node +{ + public: + int data; + node*next; + node(int data) + { + this->data=data; + next=NULL; + } +}; +void reverse(node*&head) +{ + node*c=head; + node*p=NULL; + node*n=NULL; + while(c!=NULL) + { + n=c->next; + c->next=p; + p=c; + c=n; + } + head=p; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<" ->"; + head=head->next; + } + cout< +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +int length(node*head) +{ + int len=0; + while(head!=NULL) + { + head=head->next; + len+=1; + } + return len; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATmiddle(node*&head,int data,int p) +{ + if(head==NULL or p==0) + { + insertAThead(head,data); + } + else if(p>length(head)) + { + insertATtail(head,data); + } + else + { + int jump=1; + node*temp=head; + while(jump<=p-1) + { + temp=temp->next; + jump+=1; + } + node* n=new node(data); + n->next=temp->next; + temp->next=n; + } +} + + +void reverse(node*&head) +{ + node*c=head; + node*p=NULL; + node*n=NULL; + while(c!=NULL) + { + n=c->next; + c->next=p; + p=c; + c=n; + } + head=p; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout< +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +int length(node*head) +{ + int len=0; + while(head!=NULL) + { + head=head->next; + len+=1; + } + return len; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATmiddle(node*&head,int data,int p) +{ + if(head==NULL or p==0) + { + insertAThead(head,data); + } + else if(p>length(head)) + { + insertATtail(head,data); + } + else + { + int jump=1; + node*temp=head; + while(jump<=p-1) + { + temp=temp->next; + jump+=1; + } + node* n=new node(data); + n->next=temp->next; + temp->next=n; + } +} +bool searchiterative(node*head,int key) +{ + while(head!=NULL) + { + if(head->data==key) + { + return true; + } + head=head->next; + } + return false; +} +bool searchRecursive(node*head,int key) +{ + if(head==NULL) + { + return false; + } + if(head->data==key) + { + return true; + } + else + { + return searchRecursive(head->next,key); + } +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<" ->"; + head=head->next; + } + cout< +#include +using namespace std; +int main() { + listl; + int n; + cin>>n; + while(n--) + { + int k; + cin>>k; + l.push_back(k); + } + l.sort(); + for(int i :l) + { + cout< +using namespace std; +// Create a class node for the circular linked list +class node +{ + public: + int data; + node*next; + node(int data) + { + this->data = data; // Refering to data + //Here the tail pointer would point to head of the node instead of the NULL + } +}; +// A function which is used to print the list +void printlist(node *head) +{ + node *temp = head; + while(temp->next != head) + { + cout<data<<"->"; + temp = temp->next; + } + cout<data<next = head; + if(head != NULL) + { + while(temp->next!=head) + { + temp = temp->next; + } + temp->next = ptr1; + + } + else + { + ptr1->next = ptr1; + } + head = ptr1; +} +int main(int argc, char const *argv[]) +{ + node *head = NULL; + push(head,10); + push(head,20); + push(head,30); + push(head,40); + printlist(head); + return 0; + +} diff --git a/LINKED LIST/dedectcycle1.cpp b/LINKED LIST/dedectcycle1.cpp new file mode 100644 index 0000000..b3f6b44 --- /dev/null +++ b/LINKED LIST/dedectcycle1.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; +// Create a class node for the circular linked list +class node +{ + public: + int data; + node*next; + node(int data) + { + this->data = data; // Refering to data + //Here the tail pointer would point to head of the node instead of the NULL + } +}; +// A function which is used to print the list +void printlist(node *head) +{ + node *temp = head; + while(temp->next != head) + { + cout<data<<"->"; + temp = temp->next; + } + cout<data<next = head; + if(head != NULL) + { + while(temp->next!=head) + { + temp = temp->next; + } + temp->next = ptr1; + + } + else + { + ptr1->next = ptr1; + } + head = ptr1; +} +bool dedectcycle(node*head) +{ + node*slow=head; + node*fast=head; + while(fast!=NULL and fast->next!=NULL) + { + fast=fast->next->next; + slow=slow->next; + if(fast==slow) + { + return true; + } + return false; + } +} +int main(int argc, char const *argv[]) +{ + node *head = NULL; + push(head,10); + push(head,20); + push(head,30); + push(head,40); + printlist(head); +// cout< +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<" ->"; + head=head->next; + } + cout< +using namespace std; +class node +{ +public: + int data; + node* next; + node(int d) + { + data=d; + next=NULL; + } +}; +int length(node*head) +{ + int len=0; + while(head!=NULL) + { + head=head->next; + len+=1; + } + return len; +} +void insertATtail(node*&head,int data) +{ + if(head==NULL) + { + node* head=new node(data); + return; + } + node* tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void insertATmiddle(node*&head,int data,int p) +{ + if(head==NULL or p==0) + { + insertAThead(head,data); + } + else if(p>length(head)) + { + insertATtail(head,data); + } + else + { + int jump=1; + node*temp=head; + while(jump<=p-1) + { + temp=temp->next; + jump+=1; + } + node* n=new node(data); + n->next=temp->next; + temp->next=n; + } +} +bool searchiterative(node*head,int key) +{ + while(head!=NULL) + { + if(head->data==key) + { + return true; + } + head=head->next; + } + return false; +} +bool searchRecursive(node*head,int key) +{ + if(head==NULL) + { + return false; + } + if(head->data==key) + { + return true; + } + else + { + return searchRecursive(head->next,key); + } +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<" ->"; + head=head->next; + } + cout< +using namespace std; +class node +{ + public: + node*next; + int data; + node(int d) + { + next=NULL; + data=d; + } +}; +void insertatTail(node*&head,int data) +{ + if(head==NULL) + { + head=new node(data); + return; + } + node*tail=head; + while(tail->next!=NULL) + { + tail=tail->next; + } + tail->next=new node(data); + return; +} +void insertAThead(node*&head,int data) +{ + node* n= new node(data); + n->next=head; + head=n; +} +void print(node*head) +{ + while(head!=NULL) + { + cout<data<<"->"; + head=head->next; + } + cout<>data; + while(data!=-1) + { + insertatTail(head,data); + cin>>data; + } +} +int main() +{ + node*head=NULL; + buildlist(head); + print(head); + return 0; +} diff --git a/QUEUES First non repeating character Implementation b/QUEUES First non repeating character Implementation new file mode 100644 index 0000000..2f5f21f --- /dev/null +++ b/QUEUES First non repeating character Implementation @@ -0,0 +1,50 @@ +#include +#include +using namespace std; +int main() +{ + queue q; + int freq[27]={0}; //frequency table initialised woth zeros + + //running a stream until a '.' + char ch; + cin>>ch; + while(ch!='.') + { + q.push(ch); //push in the queue + freq[ch-'a']++; //'a'-'a'=0 _______ 'b'-'a'=1.... and at each frequency if the characters are repeating then incremented + + while(!q.empty()) + { + int idx=q.front()-'a'; + if(freq[idx]>1) //if characters are repeating + { + q.pop(); //delete the previous element + } + else + { + cout<>ch; + } + return 0; +} +/* i/p: + aabccbc. + + o/p: + a + -1 + b + b + b + -1 + -1 +*/ diff --git a/README.md b/README.md index 7604f2d..51960db 100644 --- a/README.md +++ b/README.md @@ -93,47 +93,26 @@ Searching , is a process of Searching elements in the Linked List. Search could 3. In the implementation of the advanced algorithm such as Fibonacci Heap

List STL Functions

- List is included in the given C++ Program. The function is there to add all its utility and is managed -

Push_Back()

- This function is used to add the elements at the end of the list -

Sort()

- This function is used to sort the given list -

push_front()

- This function is used to add the element at the front of the list -

pop_back()

- As, the name goes; This function removes the last element from the list -

pop_front()

- This function removes the first element from the list -

insert(position,element)

- This function is used to insert the element in the linked list -

erase(position)

- This function is used to remove the element in the given list -

remove(element)

- This function removes the given element from the list -

front()

- This function is used to print the first element in the list - -

Stack

- +

STACK

Stack is a data structure, which represents the collection of the objects. A item could be added and stored in a stack using push operation. A object can be retrieved using pop operation, to remove an item from the stack. A item could be inserted at the top of the stack. A item could be either removed either from top or bottom of the stack. Their are two types of stack: 1. LIFO(Last In First Out) @@ -142,9 +121,35 @@ Stack is a data structure, which represents the collection of the objects. A ite FIFO stack is basically a type of Queue. Stacks have several applications in the Computer Programming. LIFO is used to retrieve, recently used objects from cache. FIFO stacks is used to ensure the data is retrieved in the order it was entered. Stacks are basically created at the background, while application was running in front. If stack, runs out of memory such situation is termed as Stack Overflow . -

Genralizaition of Stack

A stack could be generalized using templated class template . You can generalize the given code in a following way as told in file generalization of stack - -

Queue

+ +

STL Stack Functions

+stack header file is used in order to implement STL functions. The function is there to add all its utility and is managed +

push(data)

+This function is used to add the elements at the end of the list +

top()

+This function is used to check the top most element. +

pop()

+This function is used to delete the element at the front of the list. + + +

QUEUES

+ Queue is a data structure designed to operate in FIFO (First in First out) context. In queue elements are inserted from rear end and get removed from front end operations called 'enqueued' and 'dequed' respectively. Queue class is container adapter. Container is an objects that hold data of same type. Queue can be created from different sequence containers. +

There are two ways to implement Queues

+ + + 1. Circular Array based + 2. Linked List based + + Out of the two Circular Array based implementation is quite lengthier than the Linked List based implementation. + +

STL Queue class function

+queue header file is used in order to implement STL functions. The function is there to add all its utility and is managed +

push(data)

+This function is used to add the elements at the end of the list +

front()

+This function is used to check the front element. +

pop()

+This function is used to delete the element at the front of the list. diff --git a/Stack and Queue/Balanced_Brackets.cpp b/Stack and Queue/Balanced_Brackets.cpp new file mode 100644 index 0000000..7fb68ce --- /dev/null +++ b/Stack and Queue/Balanced_Brackets.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +using namespace std; +bool checkexpr(string str) +{ + stacks; + for(int i=0;i +#include +#include +using namespace std; + +bool redundant(string str) +{ + stack s; + + int n = str.length() ; + for(int i = 0 ; i < n;i++) + { + if(str[i] == ')' && !s.empty()) + { + if(s.top() == '('){ + return true; + } + else{ + while(s.top() != '('){ + s.pop(); + } + s.pop(); + } + + } + else + { + s.push(str[i]) ; + } + } + return false ; +} + + +int main(){ + + string str ; + getline(cin, str); + if(redundant(str)) + { + cout<<"true"< +#include +#include +using namespace std; + +int precidence(char oper){ + if(oper == '+'){ + return 1; + } + else if(oper == '-'){ + return 1; + } + else if(oper == '*'){ + return 2; + } + else if(oper == '/'){ + return 2; + } + return 0; +} + +int operation(int v1 , int v2, char oper){ + if(oper == '+'){ + return v1 + v2; + } + else if(oper == '-'){ + return v1 - v2; + } + else if(oper == '*'){ + return v1 * v2; + } + else if(oper == '/'){ + return v1 / v2; + } + return 0; +} + +int main() +{ + string str; + getline(cin , str); + stack o1; + stack o2; + + for(int i = 0 ; i < str.length() ; i++) + { + char ch = str.at(i); + if(ch == ' ') + { + continue; + } + else + { + if(ch == '(') + { + o2.push(ch); + } + else if(ch == ')') + { + while(o2.top() != '('){ + char op = o2.top(); + o2.pop(); + int v2 = o1.top(); + o1.pop(); + int v1 = o1.top(); + o1.pop(); + int ans = operation(v1 ,v2 , op); + o1.push(ans); + } + o2.pop(); + } + else if(ch == '+' || ch == '-' || ch == '*' || ch == '/') + { + int pc = precidence(str[i]); + while(!o2.empty() && o2.top() != '(' && precidence(o2.top()) >= pc) + { + char op = o2.top(); + o2.pop(); + int v2 = o1.top(); + o1.pop(); + int v1 = o1.top(); + o1.pop(); + int ans = operation(v1 ,v2 , op); + o1.push(ans); + } + o2.push(ch); + } + else + { + o1.push(int(str[i])- 48); + } + } + } + while(!o2.empty()){ + char op = o2.top(); + o2.pop(); + int v2 = o1.top(); + o1.pop(); + int v1 = o1.top(); + o1.pop(); + int ans = operation(v1 ,v2 , op); + o1.push(ans); + } + cout< +#include +using namespace std; +int main() +{ + stacks1,s2; + int n; + cin>>n; + int a[n]; + for(int i=0;i>a[i]; + } + s1.push(a[n-1]); + s2.push(-1); + for(int i=n-2;i>=0;i--) + { + while(!s1.empty() and a[i]>s1.top()) + { + s1.pop(); + } + if(s1.empty()) + { + s2.push(-1); + } + else + { + s2.push(s1.top()); + } + s1.push(a[i]); + } + while(!s2.empty()) + { + cout< +#include +#include +using namespace std; +int main() +{ + string str; + cin>>str; + stack s; + int n=1; + for(int i=0;i 21 +i -> 12 +ddd -> 4321 +iii -> 1234 +dddiddd -> 43218765 +iiddd -> 126543 + +i/p +dddiidid +o/p +432157698 +*/ diff --git a/Stack and Queue/Stock_Span.cpp b/Stack and Queue/Stock_Span.cpp new file mode 100644 index 0000000..f5819aa --- /dev/null +++ b/Stack and Queue/Stock_Span.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; +int main() +{ + long int n; + cin>>n; + long int a[n],b[n]; + for(long int i=0;i>a[i]; + } + stack s; + b[0]=1; + s.push(0); + for(long int i=1;ia[s.top()]) + { + s.pop(); + } + if(s.empty()) + { + b[i]=i+1; + } + else + { + b[i]=i-s.top(); + } + s.push(i); + } + for(long int i=0;i