From 3ec0e6f501eb77072a620f6bd00fff813607ae84 Mon Sep 17 00:00:00 2001 From: Arjun Singh <61823639+arjunsingh64@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:28:59 +0530 Subject: [PATCH 01/12] Update README.md On Line number 12 mistake in word --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eaa7486..f537819 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Thanks for taking the time to contribute to this project. Before making PRs, ple **A few norms you should follow :** - Proper intendation is must! -- Include ample comments so that code is understandable and easy to follow. +- Include Sample comments so that code is understandable and easy to follow. - Mention the complexity of a function in a comment. - Time complexity - Space complexity From d6e9d06f2f3a532bd75e836e36d71787d5140a51 Mon Sep 17 00:00:00 2001 From: Manasi Wader <52437677+WaderManasi@users.noreply.github.com> Date: Thu, 1 Oct 2020 00:07:58 +0530 Subject: [PATCH 02/12] Add files via upload --- Linked Lists/Check_Circularll.cpp | 12 + Linked Lists/DeleteAtPos_Circularll.cpp | 26 ++ Linked Lists/DeleteHead_Circularll.cpp | 18 ++ Linked Lists/DeleteHead_Doublyll.cpp | 14 + Linked Lists/DeleteHead_LinkedList.cpp | 12 + Linked Lists/DeleteTail_Circular.cpp | 13 + Linked Lists/DeleteTail_Doublyll.cpp | 15 + Linked Lists/DeleteTail_LinkedList.cpp | 18 ++ Linked Lists/Display_DoublyCircularll.cpp | 17 ++ Linked Lists/Display_Doublyll.cpp | 17 ++ Linked Lists/Display_LinkedList.cpp | 19 ++ Linked Lists/DoublyCircular-LinkedList.cpp | 328 +++++++++++++++++++++ Linked Lists/DoublyLinear-LinkedList.cpp | 305 +++++++++++++++++++ Linked Lists/InsertAtPos_Circular.cpp | 35 +++ Linked Lists/InsertHead_Circularll.cpp | 24 ++ Linked Lists/InsertHead_Doubly;;.cpp | 18 ++ Linked Lists/InsertTail_Circularll.cpp | 25 ++ Linked Lists/InsertTail_Doublyll.cpp | 20 ++ Linked Lists/Insertion_LinkedList.cpp | 31 ++ Linked Lists/JoinTwo_LinkedzLists.cpp | 18 ++ Linked Lists/Length_Circularll.cpp | 15 + Linked Lists/Max&Min_LinkedList.cpp | 34 +++ Linked Lists/NodesSum_LinkedList.cpp | 16 + Linked Lists/Searching_LinkedList.cpp | 19 ++ Linked Lists/SinglyCircular-LinkedList.cpp | 314 ++++++++++++++++++++ Linked Lists/SinglyLinear-LinkedList.cpp | 300 +++++++++++++++++++ 26 files changed, 1683 insertions(+) create mode 100644 Linked Lists/Check_Circularll.cpp create mode 100644 Linked Lists/DeleteAtPos_Circularll.cpp create mode 100644 Linked Lists/DeleteHead_Circularll.cpp create mode 100644 Linked Lists/DeleteHead_Doublyll.cpp create mode 100644 Linked Lists/DeleteHead_LinkedList.cpp create mode 100644 Linked Lists/DeleteTail_Circular.cpp create mode 100644 Linked Lists/DeleteTail_Doublyll.cpp create mode 100644 Linked Lists/DeleteTail_LinkedList.cpp create mode 100644 Linked Lists/Display_DoublyCircularll.cpp create mode 100644 Linked Lists/Display_Doublyll.cpp create mode 100644 Linked Lists/Display_LinkedList.cpp create mode 100644 Linked Lists/DoublyCircular-LinkedList.cpp create mode 100644 Linked Lists/DoublyLinear-LinkedList.cpp create mode 100644 Linked Lists/InsertAtPos_Circular.cpp create mode 100644 Linked Lists/InsertHead_Circularll.cpp create mode 100644 Linked Lists/InsertHead_Doubly;;.cpp create mode 100644 Linked Lists/InsertTail_Circularll.cpp create mode 100644 Linked Lists/InsertTail_Doublyll.cpp create mode 100644 Linked Lists/Insertion_LinkedList.cpp create mode 100644 Linked Lists/JoinTwo_LinkedzLists.cpp create mode 100644 Linked Lists/Length_Circularll.cpp create mode 100644 Linked Lists/Max&Min_LinkedList.cpp create mode 100644 Linked Lists/NodesSum_LinkedList.cpp create mode 100644 Linked Lists/Searching_LinkedList.cpp create mode 100644 Linked Lists/SinglyCircular-LinkedList.cpp create mode 100644 Linked Lists/SinglyLinear-LinkedList.cpp diff --git a/Linked Lists/Check_Circularll.cpp b/Linked Lists/Check_Circularll.cpp new file mode 100644 index 0000000..d6b0d6b --- /dev/null +++ b/Linked Lists/Check_Circularll.cpp @@ -0,0 +1,12 @@ +//Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. An empty linked list is considered as circular. +//solution + +bool isCircular(Node *head) +{ + if(head==NULL || head->next==head) return true; + Node* temp=head; + while(temp->next!=NULL && temp->next!=head) + temp=temp->next; + + return (temp->next==head)?true:false; +} \ No newline at end of file diff --git a/Linked Lists/DeleteAtPos_Circularll.cpp b/Linked Lists/DeleteAtPos_Circularll.cpp new file mode 100644 index 0000000..f8a4987 --- /dev/null +++ b/Linked Lists/DeleteAtPos_Circularll.cpp @@ -0,0 +1,26 @@ +//Given a linked list of size n, you have to delete the node at position pos of the linked list and return the new head. The position of initial node is 1. +//The tail of the circular linked list is connected to the head using next pointer. + +//solution +Node * deleteAtPosition(Node *head,int pos) +{ +if(pos==1) +{ +if(head->next==head) +return NULL; +Node *temp=head->next; +head->next=head->next->next; +delete(temp); +return head; +} +else +{ +Node *temp=head; +Node *cur; +for(int i=0;inext; +cur=temp->next; +temp->next=temp->next->next; +delete(cur); +return head; +} +} \ No newline at end of file diff --git a/Linked Lists/DeleteHead_Circularll.cpp b/Linked Lists/DeleteHead_Circularll.cpp new file mode 100644 index 0000000..fef7b07 --- /dev/null +++ b/Linked Lists/DeleteHead_Circularll.cpp @@ -0,0 +1,18 @@ +//Given a circular linked list of size n, you have to delete the head of the linked list and return the new head. +//In the circular linked list the tail of the list is connected to the head using the next pointer. +//Note: Please also set the next of the original head to null. + +//solution +Node * deleteHead(Node *head) +{ + if(head==NULL) + return NULL; + + Node* temp=head; + Node* temp1=head; + while(temp->next!=head) temp=temp->next; + temp->next=temp1->next; + head=temp1->next; + temp1->next=NULL; + return head; +} \ No newline at end of file diff --git a/Linked Lists/DeleteHead_Doublyll.cpp b/Linked Lists/DeleteHead_Doublyll.cpp new file mode 100644 index 0000000..1b3673f --- /dev/null +++ b/Linked Lists/DeleteHead_Doublyll.cpp @@ -0,0 +1,14 @@ +//Given a doubly linked list of size n, you have to delete the head of the linked list and return the new head. +//Note: Please set the previous of new head to null, and set the next of old head to null, and then delete the old head. + +Node *deleteHead(Node * head) +{ + if(head==NULL) return NULL; + Node* temp=head; + + head=temp->next; + head->prev=NULL; + + delete temp; + return head; +} \ No newline at end of file diff --git a/Linked Lists/DeleteHead_LinkedList.cpp b/Linked Lists/DeleteHead_LinkedList.cpp new file mode 100644 index 0000000..1c33097 --- /dev/null +++ b/Linked Lists/DeleteHead_LinkedList.cpp @@ -0,0 +1,12 @@ +//Given a linked list of size n, you have to delete the head of the linked list and return the new head. +//Note: Please also set the next of the original head to null. + +Node* deleteHead(Node *head) +{ + Node* temp=head->next; + head->next=NULL; + + delete head; + + return temp; +} \ No newline at end of file diff --git a/Linked Lists/DeleteTail_Circular.cpp b/Linked Lists/DeleteTail_Circular.cpp new file mode 100644 index 0000000..7cc7651 --- /dev/null +++ b/Linked Lists/DeleteTail_Circular.cpp @@ -0,0 +1,13 @@ +//Given a circular linked list of size n, you have to delete the tail (last element) in the linked list. +//In a circular linked list, the tail is connect to the head using the next pointer. + +//solution +Node * deleteTail(Node * head) +{ + if(head==NULL) return NULL; + + Node* temp=head; + while(temp->next->next!=head) temp=temp->next; + temp->next=head; + return head; +} \ No newline at end of file diff --git a/Linked Lists/DeleteTail_Doublyll.cpp b/Linked Lists/DeleteTail_Doublyll.cpp new file mode 100644 index 0000000..5de1c7f --- /dev/null +++ b/Linked Lists/DeleteTail_Doublyll.cpp @@ -0,0 +1,15 @@ +//Given a doubly linked list of size n, you have to delete the tail (last element) in the linked list. + +Node *deleteTail(Node * head) +{ + if(head==NULL) return NULL; + + Node* temp=head; + + while(temp->next->next) temp=temp->next; + + delete temp->next; + temp->next=NULL; + + return head; +} \ No newline at end of file diff --git a/Linked Lists/DeleteTail_LinkedList.cpp b/Linked Lists/DeleteTail_LinkedList.cpp new file mode 100644 index 0000000..b019107 --- /dev/null +++ b/Linked Lists/DeleteTail_LinkedList.cpp @@ -0,0 +1,18 @@ +//Given a linked list of size n, you have to delete the tail (last element) in the linked list. +//solution + +Node* deleteTail(Node *head) +{ + //empty linked list + if(head==NULL) + return head; + + Node* temp=head; + while(temp->next->next!=NULL) + temp=temp->next; + + delete temp->next; + temp->next=NULL; + + return head; +} \ No newline at end of file diff --git a/Linked Lists/Display_DoublyCircularll.cpp b/Linked Lists/Display_DoublyCircularll.cpp new file mode 100644 index 0000000..50bc91b --- /dev/null +++ b/Linked Lists/Display_DoublyCircularll.cpp @@ -0,0 +1,17 @@ +void displayList(Node *head) +{ + vectorv; + Node* temp=head; + while(temp->next!=head) + { + v.push_back(temp->data); + cout<data<<" "; + temp=temp->next; + } + cout<data<<" "; + v.push_back(temp->data); + cout<=0;i--) + cout< displayList(Node *head) +{ + vectorv; + if(head==NULL) + return v; + Node* temp=head; + while(temp) + { + v.push_back(temp->data); + temp=temp->next; + } + return v; + +} \ No newline at end of file diff --git a/Linked Lists/Display_LinkedList.cpp b/Linked Lists/Display_LinkedList.cpp new file mode 100644 index 0000000..0504686 --- /dev/null +++ b/Linked Lists/Display_LinkedList.cpp @@ -0,0 +1,19 @@ +//Given a singly linked list of integers. The task is to display the linked list. +//solution code snippet + + +vector displayList(Node *head) +{ + vectorv; + //empty linked list + if(head==NULL) + return v; + Node* temp=head; + while(temp!=NULL) + { + int val=temp->data; + v.push_back(val); + temp=temp->next; + } + return v; +} \ No newline at end of file diff --git a/Linked Lists/DoublyCircular-LinkedList.cpp b/Linked Lists/DoublyCircular-LinkedList.cpp new file mode 100644 index 0000000..c801c75 --- /dev/null +++ b/Linked Lists/DoublyCircular-LinkedList.cpp @@ -0,0 +1,328 @@ +/////////////////////////////////////////////////////////////////////////////////// +//Name :Manasi Mohan Wader // +//Program :Doubly Circular Linked List // +//Language :C++ (Object Oriented Approach) // +//Functions:To perform Create, Insert, Delete operations on Doubly Circular // +// Linked List // +/////////////////////////////////////////////////////////////////////////////////// + +#include +using namespace std; + +/////////////////////////////////////////////////////////////////////////////////// +// Slightly different approach ,with 'void' return type // +// Doubly Circular linked list is maintained using two pointers (head and tail) // +// Due to which it is possible to traverse in reverse as well as forward direction.// +// There no as such termination ,because last node does not contain NULL value // +/////////////////////////////////////////////////////////////////////////////////// + +typedef struct node +{ + int data; + struct node* next; + struct node* prev; +}NODE,*PNODE; +/////////////////////////////////////////////////////////////////////////////////// + +class DoublyCircularLL +{ + PNODE head,tail; //maintaining two pointers + //there is no as such termination in CLL unlike linear linkedlist + int ct; + public: + DoublyCircularLL(); //constructor + ~DoublyCircularLL(); //To deallocate the used memory + + void InsertFirst(int); + void InsertLast(int); + void InsertAtPos(int,int); + + void DeleteFirst(); + void DeleteLast(); + void DeleteAtPos(int); + + void DisplayCLL(); //to Display the Linked List + int Count(); //Display total number of nodes present in Linked List +}; +/////////////////////////////////////////////////////////////////////////////////// +//Initializing memory +DoublyCircularLL::DoublyCircularLL() +{ + head=tail=NULL; + ct=0; +} +/////////////////////////////////////////////////////////////////////////////////// +//To get total number of nodes/elements present in the Linked List +int DoublyCircularLL::Count() +{ + //do-while loop can also be used here + ct=0; + PNODE temp=head; + while(temp->next!=head) + { + ct++; + temp=temp->next; + } + ct++; + return ct; +} +/////////////////////////////////////////////////////////////////////////////////// +//Display entire Linked List +void DoublyCircularLL::DisplayCLL() +{ + PNODE temp=head; + while(temp->next!=head) + { + cout<data<<" "; + temp=temp->next; + } + cout<data<<" "; +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the beginning of Linked List +void DoublyCircularLL::InsertFirst(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + newn->prev=NULL; + + //Empty linked list + if(head==NULL && tail==NULL) + { + head=newn; + tail=newn; + } + //multiple nodes + else + { + newn->next=head; + newn->prev=tail; + head->prev=newn; + head=newn; + } + tail->next=head; +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the end of Linked List +void DoublyCircularLL::InsertLast(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + if(head==NULL && tail==NULL) + { + head=newn; + tail=newn; + } + else + { + newn->prev=tail; + tail->next=newn; + tail=newn; + } + tail->next=head; +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at desired Position in the Linked List +void DoublyCircularLL::InsertAtPos(int pos,int n) +{ + int tot=Count(); + if(pos<1 || pos>tot+1) + { + cout<<"\nPosition invalid (element cannot be inserted"; + return; + } + else if(pos==1) + { + //To insert element at beginning + InsertFirst(n); + } + else if(pos==tot+1) + { + //To insert element at end + InsertLast(n); + } + else + { + PNODE temp=head; + PNODE newn=new node; + newn->next=NULL; + newn->data=n; + for(int i=1;inext; + } + newn->next=temp; + newn->prev=temp->prev; + temp->prev->next=newn; + temp->prev=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete first element of Linked List +void DoublyCircularLL::DeleteFirst() +{ + //Empty Linked list + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete"; + return; + } + + //Linked list containing only one element + else if(head==tail) + { + delete head; + head=tail=NULL; + } + //Multiple nodes + else + { + head=head->next; + delete tail->next; + tail->next=head; + head->prev=tail; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete last element from the Linked List +void DoublyCircularLL::DeleteLast() +{ + //Empty Linked list + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete"; + return; + } + + //Linked list containing only one element + else if(head==tail) + { + delete tail; + head=tail=NULL; + } + //Multiple nodes + else + { + PNODE temp=head; + while(temp->next!=tail) + { + temp=temp->next; + } + temp->next=head; + tail=temp; + head->prev=tail; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete element from desired position in the Linked List +void DoublyCircularLL::DeleteAtPos(int pos) +{ + int tot=Count(); + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete\n"; + return; + } + if(pos<1 || pos>tot) + { + cout<<"\nPosition invalid\n"; + } + else if(pos==1) + { + DeleteFirst(); + } + else if(pos==tot) + { + DeleteLast(); + } + else + { + PNODE temp=head; + PNODE curr=NULL; + for(int i=1;inext; + } + temp->next->prev=temp->prev; + temp->prev->next=temp->next; + delete temp; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Destructor: to deallocate the used memory +DoublyCircularLL::~DoublyCircularLL() +{ + PNODE temp=head; + if(head==NULL && tail==NULL) + return; + else + { + while(head!=tail) + { + head=head->next; + delete temp; + temp=head; + } + } +} +/////////////////////////////////////////////////////////////////////////////////// +//main function of the program +/////////////////////////////////////////////////////////////////////////////////// +int main() +{ + DoublyCircularLL obj; + + //Here one can opt for menu driven approach also! + obj.InsertFirst(30); + obj.DisplayCLL();cout<<"\n"; + obj.InsertFirst(20); + obj.DisplayCLL();cout<<"\n"; + obj.InsertFirst(10); + obj.DisplayCLL(); + cout<<"\nSuccessfully inserted element from beginning"; + cout<<"Total number of elements "< +using namespace std; + +/////////////////////////////////////////////////////////////////////////////////// +// Slightly different approach ,with 'void' return type // +// Doubly linear linked list is maintained using two pointers // +// Due to which it is possible to traverse in reverse as well as forward direction.// +/////////////////////////////////////////////////////////////////////////////////// + +typedef struct node +{ + int data; + struct node* next; + struct node* prev; +}NODE,*PNODE; +/////////////////////////////////////////////////////////////////////////////////// + +class DoublyLinearLL +{ + PNODE head; + int ct; + public: + DoublyLinearLL(); //constructor + ~DoublyLinearLL(); //To deallocate the used memory + + void InsertFirst(int); + void InsertLast(int); + void InsertAtPos(int,int); + + void DeleteFirst(); + void DeleteLast(); + void DeleteAtPos(int); + + void DisplayDLL(); //to Display the Linked List + int Count(); //Display total number of nodes present in Linked List +}; +/////////////////////////////////////////////////////////////////////////////////// +//Initializing memory +DoublyLinearLL::DoublyLinearLL() +{ + head=NULL; + ct=0; +} +/////////////////////////////////////////////////////////////////////////////////// +//To get total number of nodes/elements present in the Linked List +int DoublyLinearLL::Count() +{ + ct=0; + PNODE temp=head; + while(temp!=NULL) + { + ct++; + temp=temp->next; + } + return ct; +} +/////////////////////////////////////////////////////////////////////////////////// +//Display entire Linked List +void DoublyLinearLL::DisplayDLL() +{ + PNODE temp=head; + while(temp!=NULL) + { + cout<data<<" "; + temp=temp->next; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the beginning of Linked List +void DoublyLinearLL::InsertFirst(int n) +{ + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + newn->prev=NULL; + if(head==NULL) + { + head=newn; + } + else + { + newn->next=head; + head->prev=newn; + head=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the end of Linked List +void DoublyLinearLL::InsertLast(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + newn->prev=NULL; + + if(head==NULL) + { + head=newn; + } + else + { + while(temp->next!=NULL) + { + temp=temp->next; + } + temp->next=newn; + temp->next->prev=temp; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at desired Position in the Linked List +void DoublyLinearLL::InsertAtPos(int pos,int n) +{ + int tot=Count(); + if(pos<1 || pos>tot+1) + { + cout<<"\nPosition invalid (element cannot be inserted"; + return; + } + else if(pos==1) + { + //To insert element at beginning + InsertFirst(n); + } + else if(pos==tot+1) + { + //To insert element at end + InsertLast(n); + } + else + { + PNODE temp=head; + PNODE newn=new node; + newn->next=NULL; + newn->data=n; + for(int i=1;inext; + } + newn->next=temp; + newn->prev=temp->prev; + temp->prev->next=newn; + temp->prev=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete first element of Linked List +void DoublyLinearLL::DeleteFirst() +{ + if(head==NULL) + { + cout<<"\nNothing to delete"; + return; + } + else + { + PNODE temp=head; + temp->next->prev=NULL; + head=head->next; + delete temp; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete last element from the Linked List +void DoublyLinearLL::DeleteLast() +{ + if(head==NULL) + { + cout<<"\nNothing to delete\n"; + return; + } + else if(head->next==NULL) + { + delete head; + head=NULL; + } + else + { + PNODE temp=head; + while(temp->next!=NULL) + { + temp=temp->next; + } + temp->prev->next=NULL; + delete temp; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete element from desired position in the Linked List +void DoublyLinearLL::DeleteAtPos(int pos) +{ + int tot=Count(); + if(head==NULL) + { + cout<<"\nNothing to delete\n"; + return; + } + if(pos<1 || pos>tot) + { + cout<<"\nPosition invalid\n"; + } + else if(pos==1) + { + DeleteFirst(); + } + else if(pos==tot) + { + DeleteLast(); + } + else + { + PNODE temp=head; + PNODE curr=NULL; + for(int i=1;inext; + } + temp->next->prev=temp->prev; + temp->prev->next=temp->next; + delete temp; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Destructor: to deallocate the used memory +DoublyLinearLL::~DoublyLinearLL() +{ + PNODE temp=head; + if(head==NULL) + return; + else if(head!=NULL) + { + while(head!=NULL) + { + head=head->next; + delete temp; + temp=head; + } + } +} +/////////////////////////////////////////////////////////////////////////////////// +//main function of the program +/////////////////////////////////////////////////////////////////////////////////// +int main() +{ + DoublyLinearLL obj; + + //Here one can opt for menu driven approach also! + obj.InsertFirst(30); + obj.DisplayDLL();cout<<"\n"; + obj.InsertFirst(20); + obj.DisplayDLL();cout<<"\n"; + obj.InsertFirst(10); + obj.DisplayDLL(); + cout<<"\nSuccessfully inserted element from beginning"; + cout<<"Total number of elements "<next==head) +{ +temp->next=head; +head->next=temp; +return ; +} +temp->next=head->next; +head->next=temp; +} +else{ +for(i=0;inext; +if(cur==head){ +return ; +} + +} +temp->next=cur->next; +cur->next=temp; +} +} \ No newline at end of file diff --git a/Linked Lists/InsertHead_Circularll.cpp b/Linked Lists/InsertHead_Circularll.cpp new file mode 100644 index 0000000..1ee1384 --- /dev/null +++ b/Linked Lists/InsertHead_Circularll.cpp @@ -0,0 +1,24 @@ +//Given a circular linked list of size N, you need to insert an element data before the head and make it the new head. The tail of the linked list is connected to head. + +//solution +Node *insertInHead(Node * head, int data) +{ + Node* newn=new Node(data); + if(head==NULL) + { + head=newn; + newn->next=head; + } + + else + { + Node* temp=head; + do{ + temp=temp->next; + }while(temp->next!=head); + newn->next=head; + head=newn; + temp->next=head; + } + return head; +} \ No newline at end of file diff --git a/Linked Lists/InsertHead_Doubly;;.cpp b/Linked Lists/InsertHead_Doubly;;.cpp new file mode 100644 index 0000000..70d7871 --- /dev/null +++ b/Linked Lists/InsertHead_Doubly;;.cpp @@ -0,0 +1,18 @@ +//Given a doubly linked list of size n, you need to insert an element data before the head and make it the new head. + +//solution +Node *insertInHead(Node * head, int data) +{ + Node* newn = new Node(data); + + if(head==NULL) + head=newn; + else + { + Node* temp=head; + newn->next=head; + head->prev=newn; + head=newn; + } + return head; +} \ No newline at end of file diff --git a/Linked Lists/InsertTail_Circularll.cpp b/Linked Lists/InsertTail_Circularll.cpp new file mode 100644 index 0000000..d1f660f --- /dev/null +++ b/Linked Lists/InsertTail_Circularll.cpp @@ -0,0 +1,25 @@ +//Given a circular linked list of size N, you need to insert an element data after the tail. +//The tail of the linked list is connected to head. + +//solution +Node *insertInTail(Node * head, int data) +{ + Node* newn=new Node(data); + if(head==NULL) + { + head=newn; + newn->next=head; + } + else + { + Node* temp=head; + + while(temp->next!=head) + { + temp=temp->next; + } + temp->next=newn; + newn->next=head; + } + return head; +} \ No newline at end of file diff --git a/Linked Lists/InsertTail_Doublyll.cpp b/Linked Lists/InsertTail_Doublyll.cpp new file mode 100644 index 0000000..b35e391 --- /dev/null +++ b/Linked Lists/InsertTail_Doublyll.cpp @@ -0,0 +1,20 @@ +//Given a doubly linked list of size n, you need to insert an element data after the tail. +//solution + +Node *insertInTail(Node * head, int data) +{ + Node* newn=new Node(data); + + if(head==NULL) + head=newn; + else + { + Node* temp=head; + while(temp->next!=NULL) + temp=temp->next; + + temp->next=newn; + newn->prev=temp; + } + return head; +} \ No newline at end of file diff --git a/Linked Lists/Insertion_LinkedList.cpp b/Linked Lists/Insertion_LinkedList.cpp new file mode 100644 index 0000000..baaf3a1 --- /dev/null +++ b/Linked Lists/Insertion_LinkedList.cpp @@ -0,0 +1,31 @@ +//Insertion at front and end of Linked List +//solution + +//function inserts the data in front of the list +Node *insertAtBegining(Node *head, int newData) { +   Node *newn=new Node(newData); +   if(head==NULL) +        head=newn; +      else +   { +       newn->next=head; +       head=newn; +   } +   return head; +} + +// function appends the data at the end of the list +Node *insertAtEnd(Node *head, int newData)  { +  Node *newn=new Node(newData); +  if(head==NULL) +     head=newn; +   else +   { +       Node* temp=head; +       while(temp->next!=NULL) +           temp=temp->next; + +       temp->next=newn; +   } +   return head; +} \ No newline at end of file diff --git a/Linked Lists/JoinTwo_LinkedzLists.cpp b/Linked Lists/JoinTwo_LinkedzLists.cpp new file mode 100644 index 0000000..b481bbf --- /dev/null +++ b/Linked Lists/JoinTwo_LinkedzLists.cpp @@ -0,0 +1,18 @@ +//Given two linked lists of size n1 and n2 respectively, +//you have to join the head of second list to the tail of first so that we can traverse both the lists using head of 1st list. + +//solution +Node * joinTheLists(Node * head1, Node * head2) +{ + if(head1==NULL) + head1=head2; + + Node* temp=head1; + + while(temp->next!=NULL) + temp=temp->next; + + temp->next=head2; + + return head1; +} \ No newline at end of file diff --git a/Linked Lists/Length_Circularll.cpp b/Linked Lists/Length_Circularll.cpp new file mode 100644 index 0000000..2f06e66 --- /dev/null +++ b/Linked Lists/Length_Circularll.cpp @@ -0,0 +1,15 @@ + +//solution +int length(Node* head) +{ +int ct=0; +if(head==NULL) return ct; + +Node* temp=head; +do{ +ct++; +temp=temp->next; +}while(temp!=head); + +return ct; +} \ No newline at end of file diff --git a/Linked Lists/Max&Min_LinkedList.cpp b/Linked Lists/Max&Min_LinkedList.cpp new file mode 100644 index 0000000..3fa30f7 --- /dev/null +++ b/Linked Lists/Max&Min_LinkedList.cpp @@ -0,0 +1,34 @@ +//Given a singly linked list of N elements. The task is to find the maximum and minimum element. +//solution + +//to find max element +int maximum(Node *head) +{ + vectorv; + int max=0; + if(head==NULL) + return max; + Node* temp=head; + while(temp!=NULL) + { + v.push_back(temp->data); + temp=temp->next; + } + return *max_element(v.begin(),v.end()); +} + +//to find min element +int minimum(Node *head) +{ + vectorv; + int max=0; + if(head==NULL) + return max; + Node* temp=head; + while(temp!=NULL) + { + v.push_back(temp->data); + temp=temp->next; + } + return *min_element(v.begin(),v.end()); +} \ No newline at end of file diff --git a/Linked Lists/NodesSum_LinkedList.cpp b/Linked Lists/NodesSum_LinkedList.cpp new file mode 100644 index 0000000..939b8e1 --- /dev/null +++ b/Linked Lists/NodesSum_LinkedList.cpp @@ -0,0 +1,16 @@ +//Given a singly linked list of size N. The task is to sum the elements of the linked list. +//solution + +int sumOfElements(Node *head) +{ + int sum=0; + if(head==NULL) + return 0; + Node* temp=head; + while(temp!=NULL) + { + sum=sum+temp->data; + temp=temp->next; + } + return sum; +} \ No newline at end of file diff --git a/Linked Lists/Searching_LinkedList.cpp b/Linked Lists/Searching_LinkedList.cpp new file mode 100644 index 0000000..145fb45 --- /dev/null +++ b/Linked Lists/Searching_LinkedList.cpp @@ -0,0 +1,19 @@ +//Search given element in Linked list +//solution + +bool searchLinkedList(Node *head, int x) +{ +   int pos=1; +   Node* temp=head; +   while(temp!=NULL) +   { +       if(temp->data==x) +       return pos; +       else +       { +           pos++; +           temp=temp->next; +       } +   } +    return 0;    +} \ No newline at end of file diff --git a/Linked Lists/SinglyCircular-LinkedList.cpp b/Linked Lists/SinglyCircular-LinkedList.cpp new file mode 100644 index 0000000..af14530 --- /dev/null +++ b/Linked Lists/SinglyCircular-LinkedList.cpp @@ -0,0 +1,314 @@ +/////////////////////////////////////////////////////////////////////////////////// +//Name :Manasi Mohan Wader // +//Program :Singly Circular Linked List // +//Language :C++ (Object Oriented Approach) // +//Functions:To perform Create, Insert, Delete operations on Circular Linked List // +/////////////////////////////////////////////////////////////////////////////////// + +#include +using namespace std; + +/////////////////////////////////////////////////////////////////////////////////// +// Slightly different approach ,with 'void' return type // +/////////////////////////////////////////////////////////////////////////////////// + +typedef struct node +{ + int data; + struct node* next; +}NODE,*PNODE; +/////////////////////////////////////////////////////////////////////////////////// + +class SinglyCircularLL +{ + PNODE head,tail; //maintaining two pointers + //there is no as such termination in CLL unlike linear linkedlist + int ct; + public: + SinglyCircularLL(); //constructor + ~SinglyCircularLL(); //To deallocate the used memory + + void InsertFirst(int); + void InsertLast(int); + void InsertAtPos(int,int); + + void DeleteFirst(); + void DeleteLast(); + void DeleteAtPos(int); + + void DisplayCLL(); //to Display the Linked List + int Count(); //Display total number of nodes present in Linked List +}; +/////////////////////////////////////////////////////////////////////////////////// +//Initializing memory +SinglyCircularLL::SinglyCircularLL() +{ + head=tail=NULL; + ct=0; +} +/////////////////////////////////////////////////////////////////////////////////// +//To get total number of nodes/elements present in the Linked List +int SinglyCircularLL::Count() +{ + ct=0; + PNODE temp=head; + while(temp->next!=head) + { + ct++; + temp=temp->next; + } + ct++; + return ct; +} +/////////////////////////////////////////////////////////////////////////////////// +//Display entire Linked List +void SinglyCircularLL::DisplayCLL() +{ + PNODE temp=head; + while(temp->next!=head) + { + cout<data<<" "; + temp=temp->next; + } + cout<data<<" "; +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the beginning of Linked List +void SinglyCircularLL::InsertFirst(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + + //Empty linked list + if(head==NULL && tail==NULL) + { + head=newn; + tail=newn; + }else + { + newn->next=head; + head=newn; + } + tail->next=head; +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the end of Linked List +void SinglyCircularLL::InsertLast(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + if(head==NULL && tail==NULL) + { + head=newn; + tail=newn; + } + else + { + tail->next=newn; + tail=tail->next; + } + tail->next=head; + +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at desired Position in the Linked List +void SinglyCircularLL::InsertAtPos(int pos,int n) +{ + int tot=Count(); + if(pos<1 || pos>tot+1) + { + cout<<"\nPosition invalid (element cannot be inserted"; + return; + } + else if(pos==1) + { + //To insert element at beginning + InsertFirst(n); + } + else if(pos==tot+1) + { + //To insert element at end + InsertLast(n); + } + else + { + PNODE temp=head; + PNODE newn=new node; + newn->next=NULL; + newn->data=n; + for(int i=1;inext; + } + newn->next=temp->next; + temp->next=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete first element of Linked List +void SinglyCircularLL::DeleteFirst() +{ + //Empty Linked list + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete"; + return; + } + + //Linked list containing only one element + else if(head==tail) + { + delete head; + head=tail=NULL; + } + //Multiple nodes + else + { + head=head->next; + delete tail->next; + tail->next=head; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete last element from the Linked List +void SinglyCircularLL::DeleteLast() +{ + //Empty Linked list + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete"; + return; + } + + //Linked list containing only one element + else if(head==tail) + { + delete tail; + head=tail=NULL; + } + //Multiple nodes + else + { + PNODE temp=head; + while(temp->next!=tail) + { + temp=temp->next; + } + delete tail; + tail=temp; + tail->next=head; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete element from desired position in the Linked List +void SinglyCircularLL::DeleteAtPos(int pos) +{ + int tot=Count(); + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete\n"; + return; + } + if(pos<1 || pos>tot) + { + cout<<"\nPosition invalid\n"; + } + else if(pos==1) + { + DeleteFirst(); + } + else if(pos==tot) + { + DeleteLast(); + } + else + { + PNODE temp=head; + PNODE curr=NULL; + for(int i=1;inext; + } + curr=temp->next; + temp->next=curr->next; + delete curr; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Destructor: to deallocate the used memory +SinglyCircularLL::~SinglyCircularLL() +{ + PNODE temp=head; + if(head==NULL && tail==NULL) + return; + else + { + while(head!=tail) + { + head=head->next; + delete temp; + temp=head; + } + } +} +/////////////////////////////////////////////////////////////////////////////////// +//main function of the program +/////////////////////////////////////////////////////////////////////////////////// +int main() +{ + SinglyCircularLL obj; + + //Here one can opt for menu driven approach also! + obj.InsertFirst(30); + obj.DisplayCLL();cout<<"\n"; + obj.InsertFirst(20); + obj.DisplayCLL();cout<<"\n"; + obj.InsertFirst(10); + obj.DisplayCLL(); + cout<<"\nSuccessfully inserted element from beginning"; + cout<<"Total number of elements "< +using namespace std; + +/////////////////////////////////////////////////////////////////////////////////// +// Slightly different approach ,with 'void' return type // +/////////////////////////////////////////////////////////////////////////////////// + +typedef struct node +{ + int data; + struct node* next; +}NODE,*PNODE; +/////////////////////////////////////////////////////////////////////////////////// + +class SinglyLinearLL +{ + PNODE head; + int ct; + public: + SinglyLinearLL(); //constructor + ~SinglyLinearLL(); //To deallocate the used memory + + void InsertFirst(int); + void InsertLast(int); + void InsertAtPos(int,int); + + void DeleteFirst(); + void DeleteLast(); + void DeleteAtPos(int); + + void DisplayLL(); //to Display the Linked List + int Count(); //Display total number of nodes present in Linked List +}; +/////////////////////////////////////////////////////////////////////////////////// +//Initializing memory +SinglyLinearLL::SinglyLinearLL() +{ + head=NULL; + ct=0; +} +/////////////////////////////////////////////////////////////////////////////////// +//To get total number of nodes/elements present in the Linked List +int SinglyLinearLL::Count() +{ + ct=0; + PNODE temp=head; + while(temp!=NULL) + { + ct++; + temp=temp->next; + } + return ct; +} +/////////////////////////////////////////////////////////////////////////////////// +//Display entire Linked List +void SinglyLinearLL::DisplayLL() +{ + PNODE temp=head; + while(temp!=NULL) + { + cout<data<<" "; + temp=temp->next; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the beginning of Linked List +void SinglyLinearLL::InsertFirst(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + if(temp==NULL) + { + head=newn; + }else if(temp!=NULL) + { + newn->next=head; + head=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the end of Linked List +void SinglyLinearLL::InsertLast(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + if(head==NULL) + { + head=newn; + } + else + { + while(temp->next!=NULL) + { + temp=temp->next; + } + temp->next=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at desired Position in the Linked List +void SinglyLinearLL::InsertAtPos(int pos,int n) +{ + int tot=Count(); + if(pos<1 || pos>tot+1) + { + cout<<"\nPosition invalid (element cannot be inserted"; + return; + } + else if(pos==1) + { + //To insert element at beginning + InsertFirst(n); + } + else if(pos==tot+1) + { + //To insert element at end + InsertLast(n); + } + else + { + PNODE temp=head; + PNODE newn=new node; + newn->next=NULL; + newn->data=n; + for(int i=1;inext; + } + newn->next=temp->next; + temp->next=newn; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete first element of Linked List +void SinglyLinearLL::DeleteFirst() +{ + if(head==NULL) + { + cout<<"\nNothing to delete"; + return; + } + else + { + PNODE temp=head; + head=head->next; + delete temp; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete last element from the Linked List +void SinglyLinearLL::DeleteLast() +{ + if(head==NULL) + { + cout<<"\nNothing to delete\n"; + return; + } + else if(head->next==NULL) + { + delete head; + head=NULL; + } + else + { + PNODE temp=head; + while(temp->next->next!=NULL) + { + temp=temp->next; + } + delete temp->next; + temp->next=NULL; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete element from desired position in the Linked List +void SinglyLinearLL::DeleteAtPos(int pos) +{ + int tot=Count(); + if(head==NULL) + { + cout<<"\nNothing to delete\n"; + return; + } + if(pos<1 || pos>tot) + { + cout<<"\nPosition invalid\n"; + } + else if(pos==1) + { + DeleteFirst(); + } + else if(pos==tot) + { + DeleteLast(); + } + else + { + PNODE temp=head; + PNODE curr=NULL; + for(int i=1;inext; + } + curr=temp->next; + temp->next=temp->next->next; + delete curr; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Destructor: to deallocate the used memory +SinglyLinearLL::~SinglyLinearLL() +{ + PNODE temp=head; + if(head==NULL) + return; + else if(head!=NULL) + { + while(head!=NULL) + { + head=head->next; + delete temp; + temp=head; + } + } + +} +/////////////////////////////////////////////////////////////////////////////////// +//main function of the program +/////////////////////////////////////////////////////////////////////////////////// +int main() +{ + SinglyLinearLL obj; + + //Here one can opt for menu driven approach also! + obj.InsertFirst(30); + obj.InsertFirst(20); + obj.InsertFirst(10); + + cout<<"\nSuccessfully inserted element from beginning\n"; + + obj.DisplayLL(); + cout<<"\nTotal number of elements "< Date: Thu, 1 Oct 2020 00:10:56 +0530 Subject: [PATCH 03/12] Strings in Cpp --- Strings/C++/C++.cpp | 2 - Strings/C++/README.md | 0 Strings/C++/removing_spaces_from_string.cpp | 25 +++++++++++ Strings/C++/string.cpp | 17 ++++++++ Strings/C++/string_concatenation.cpp | 22 ++++++++++ Strings/C++/string_length.cpp | 27 ++++++++++++ Strings/C++/string_library.cpp | 46 +++++++++++++++++++++ Strings/C++/user_input_string.cpp | 20 +++++++++ 8 files changed, 157 insertions(+), 2 deletions(-) delete mode 100644 Strings/C++/C++.cpp create mode 100644 Strings/C++/README.md create mode 100644 Strings/C++/removing_spaces_from_string.cpp create mode 100644 Strings/C++/string.cpp create mode 100644 Strings/C++/string_concatenation.cpp create mode 100644 Strings/C++/string_length.cpp create mode 100644 Strings/C++/string_library.cpp create mode 100644 Strings/C++/user_input_string.cpp diff --git a/Strings/C++/C++.cpp b/Strings/C++/C++.cpp deleted file mode 100644 index 546ea0f..0000000 --- a/Strings/C++/C++.cpp +++ /dev/null @@ -1,2 +0,0 @@ -//Use C++ only - diff --git a/Strings/C++/README.md b/Strings/C++/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Strings/C++/removing_spaces_from_string.cpp b/Strings/C++/removing_spaces_from_string.cpp new file mode 100644 index 0000000..bec1748 --- /dev/null +++ b/Strings/C++/removing_spaces_from_string.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +// Function to remove all spaces from a given string +void removeSpaces(char *str) +{ + // To keep track of non-space character count + int count = 0; + + // Traverse the given string. If current character + // is not space, then place it at index 'count++' + for (int i = 0; str[i]; i++) + if (str[i] != ' ') + str[count++] = str[i]; + str[count] = '\0'; +} + + +int main() +{ + char str[] = "AC ES -D YP CO E "; + removeSpaces(str); + cout << str< +using namespace std; + +int main() { + + //Declaring String + string name1 = "ACES DYPCOE"; + cout< +using namespace std; + +int main() { + + //Declaring String + string name1 = "ACES "; + string name2 = "DYPCOE"; + + // Direct Concatenation + cout<<"Direct Concatenation : "<< name1 + name2 < +using namespace std; + +int main() { + + //Declaring String + string name1 = "ACES-DYPCOE"; + char myString[] = "ACES-DYPCOE"; + + //calculating length using length() function + cout<<"using Length() : "< +#include +using namespace std; + +int main() { + + // Declaring the Strings + char name1[] = "ACES"; + char name2[] = "DYPCOE"; + char fullName[] = "ACES-DYPOCE"; + char copiedString[20]; + + + // Copying String + strcpy(copiedString, fullName); + cout<<"Copied String: "< +using namespace std; + +int main() { + + string name; + string college; + + // taking input from user + cout<<"Enter you Name: "; + getline(cin,name); + cout<<"Your name is "< Date: Thu, 1 Oct 2020 00:11:58 +0530 Subject: [PATCH 04/12] Create circularqueue.cpp --- Queue/C++/circularqueue.cpp | 166 ++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 Queue/C++/circularqueue.cpp diff --git a/Queue/C++/circularqueue.cpp b/Queue/C++/circularqueue.cpp new file mode 100644 index 0000000..10cb2c1 --- /dev/null +++ b/Queue/C++/circularqueue.cpp @@ -0,0 +1,166 @@ +#include +using namespace std; + +/////////////////////////////////////////////////////////////////////////////////// +// Slightly different approach ,with 'void' return type // +/////////////////////////////////////////////////////////////////////////////////// + +typedef struct node +{ + int data; + struct node* next; +}NODE,*PNODE; +/////////////////////////////////////////////////////////////////////////////////// + +class CircularQueue +{ + PNODE head,tail; //maintaining two pointers + //there is no as such termination in CLL unlike linear linkedlist + int ct; + public: + CircularQueue(); //constructor + ~CircularQueue(); //To deallocate the used memory + + void Insert(int); + + void Remove(); + + void DisplayQueue(); //to Display the Queue + int Count(); //Display total number of nodes present in Linked List +}; +/////////////////////////////////////////////////////////////////////////////////// +//Initializing memory +CircularQueue::CircularQueue() +{ + head=tail=NULL; + ct=0; +} +/////////////////////////////////////////////////////////////////////////////////// +//To get total number of nodes/elements present in the Linked List +int CircularQueue::Count() +{ + ct=0; + PNODE temp=head; + while(temp->next!=head) + { + ct++; + temp=temp->next; + } + ct++; + return ct; +} +/////////////////////////////////////////////////////////////////////////////////// +//Display entire Linked List +void CircularQueue::DisplayQueue() +{ + PNODE temp=head; + while(temp->next!=head) + { + cout<data<<" "; + temp=temp->next; + } + cout<data<<" "; +} +/////////////////////////////////////////////////////////////////////////////////// +//Inserting element at the end of Linked List +void CircularQueue::Insert(int n) +{ + PNODE temp=head; + PNODE newn=new node; + newn->data=n; + newn->next=NULL; + if(head==NULL && tail==NULL) + { + head=newn; + tail=newn; + } + else + { + tail->next=newn; + tail=tail->next; + } + tail->next=head; + +} +/////////////////////////////////////////////////////////////////////////////////// +//Delete first element of Linked List +void CircularQueue::Remove() +{ + //Empty Linked list + if(head==NULL && tail==NULL) + { + cout<<"\nNothing to delete"; + return; + } + + //Linked list containing only one element + else if(head==tail) + { + delete head; + head=tail=NULL; + } + //Multiple nodes + else + { + head=head->next; + delete tail->next; + tail->next=head; + } +} +/////////////////////////////////////////////////////////////////////////////////// +//Destructor: to deallocate the used memory +CircularQueue::~CircularQueue() +{ + PNODE temp=head; + if(head==NULL && tail==NULL) + return; + else + { + while(head!=tail) + { + head=head->next; + delete temp; + temp=head; + } + } +} +/////////////////////////////////////////////////////////////////////////////////// +//main function of the program +/////////////////////////////////////////////////////////////////////////////////// +int main() +{ + CircularQueue obj; + + //Here one can opt for menu driven approach also! + cout<<"\n"; + obj.Insert(40); + obj.DisplayQueue(); + obj.Insert(70);cout<<"\n"; + obj.DisplayQueue(); + obj.Insert(100);cout<<"\n"; + obj.DisplayQueue(); + cout<<"\nSuccessfully inserted in queue"; + obj.DisplayQueue();cout<<"\n"; + cout<<"\nTotal number of elements "< Date: Thu, 1 Oct 2020 00:34:14 +0530 Subject: [PATCH 05/12] Update README.md --- Strings/C++/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Strings/C++/README.md b/Strings/C++/README.md index e69de29..b05405f 100644 --- a/Strings/C++/README.md +++ b/Strings/C++/README.md @@ -0,0 +1,29 @@ +# Strings + +## Definition: String is a sequence of characters which is represented within double Quotes. + +## String Library +**cstring library** provides functions for string operations. + +1. **strxfrm()**- Transforms a given null terminated byte string into an implementation defined form. +2. **strcoll()**- Compares two null terminating string. +3. **strlen()**- returns the length of the given string. +4. **strerror()**- returns the textual description of the system error code. +5. **memset()**- copies a single character for a specified number of time to an object. +6. **strtok()**- returns the next token in a null terminated byte string. +7. **strstr()**- finds the first occurrence of a substring in a string. +8. **strspn()**- takes two string dest and src and gives the length of maximum initial segment of the string dest that consists of characters that are present in the string src. +9. **strrchr()**- searches for the last occurrence of a character in a string. +10. **strpbrk()**- searches for a set of characters present in a string in another string. +11. **strcspn()**- takes two null terminated byte string: dest and src as its argument and searches dest for any characters that is present in src. +12. **strchr()**- searches for the first occurrence of a character in a string. +13. **memchr()**- searches for the first occurrence of a character in a specified number of characters. +14. **strncmp()**- compares a specified number of characters of two null terminating strings. The comparison is done lexicographically. +15. **strcmp()**- compares two null terminating string. +16. **memcmp()**- compares a specified number of characters of two pointer objects +17. **strncat()**- appends a specified number of characters of a string to the end of another string. +18. **strcat()**- appends a copy of a string to the end of another string. +19. **strncpy()**- copies a specified bytes of characters from source to destination. +20. **strcpy()**- copies a character string from source to destination. +21. **memmove()**- specified bytes of data from source to the destination. +22. **memcpy()**- copies a specified bytes of data from source to the destination. From b33a671b89edac3ca7286a5629483c232d297cba Mon Sep 17 00:00:00 2001 From: Dhruv Kothari Date: Thu, 1 Oct 2020 00:50:43 +0530 Subject: [PATCH 06/12] added segment tree --- Segment Tree/C++/segmenttree.cpp | 173 +++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 Segment Tree/C++/segmenttree.cpp diff --git a/Segment Tree/C++/segmenttree.cpp b/Segment Tree/C++/segmenttree.cpp new file mode 100644 index 0000000..f26e815 --- /dev/null +++ b/Segment Tree/C++/segmenttree.cpp @@ -0,0 +1,173 @@ +#include +using namespace std; + +void buildtree(int *tree,int *a, int index, int s, int e) +{ + + //base case + if(s > e) + return; + + //base class leaf node + if(s == e) + { + tree[index] = a[s]; + return; + } + + //recursive case + int mid = (s + e)/2; + + //build left sub tree + buildtree(tree,a,2*index,s, mid); + + //build right sub tree + buildtree(tree, a, 2*index+1, mid+1, e); + + //getting min value + int left = tree[2*index]; + int right = tree[2*index + 1]; + + tree[index] = min(left, right); + +} + +int query(int *tree,int index, int s, int e, int qs, int qe) +{ + //complete overlap condition + if(qs <= s and qe >= e) + return tree[index]; + + //No overlap condition + if(qe < s or qs > e) + return INT_MAX; + + //Partial Overlap + int mid = (s+e)/2; + //getting results from left and right + int left = query(tree, index*2, s, mid,qs,qe); + int right = query(tree, index*2+1, mid+1, e,qs,qe); + + //returning the minimum one + return min(left, right); +} + +//to update a single node +void updatenode(int *tree, int index, int s, int e, int i, int val) +{ + //no overlap + if(i < s or i > e) + return; + + //reached leaf node + if(s == e) + { + tree[index] = val; + return; + } + + //partial overlap (i.e. i is lying between s and e) + int mid = (s + e)/2; + updatenode(tree, 2*index, s, mid, i, val); + updatenode(tree, 2*index + 1, mid + 1, e, i, val); + + //updating parent nodes + tree[index] = min(tree[2*index], tree[2*index + 1]); + + return; +} + +//incrementing a range with a particular value +void updaterange(int *tree, int index, int s, int e, int rs, int re, int inc) +{ + + //no overlap + if(re < s or rs > e) + return; + + if(s==e) + { + tree[index] += inc; + return; + } + + int mid = (s + e)/2; + updaterange(tree, 2*index, s, mid, rs, re, inc); + updaterange(tree, 2*index + 1, mid + 1, e, rs, re, inc); + + //updating parent nodes + tree[index] = min(tree[2*index], tree[2*index + 1]); + return; +} + + +int main() +{ + int n; + cout<<"Enter the no. of elements: "; + cin>>n; + int a[n]; + for(int i=0; i>a[i]; + } + //starting and ending values + int s = 0; + int e = n-1; + + + //making a tree array of size(4*n + 1).[4*n + 1 is an upper bound] + int tree[(4*n) + 1]; + + //building the tree + buildtree(tree, a, 1, s, e); + + int q; + cout<<"Enter the number of queries: "; + cin>>q; + + int l, r, i, val; + + while(q--) + { + int ch; + cout<<"Press 1 to get the minimum value between a range"<>ch; + + switch(ch) + { + case 1: cout<<"Range starts from (0 based indexing): "; + cin>>l; + cout<<"Range ends at (0 based indexing): "; + cin>>r; + cout<<"minimum number in range "<>i; + cout<<"Enter the updated value: "; + cin>>val; + updatenode(tree, 1, s, e, i, val); + cout<<"value updated successfully."<>l; + cout<<"Range ends at (0 based indexing): "; + cin>>r; + cout<<"Range updated from "< Date: Thu, 1 Oct 2020 00:55:09 +0530 Subject: [PATCH 07/12] Box Stacking Problem --- .../C++/BoxStackingProblem.cpp | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Dynamic Programming/C++/BoxStackingProblem.cpp diff --git a/Dynamic Programming/C++/BoxStackingProblem.cpp b/Dynamic Programming/C++/BoxStackingProblem.cpp new file mode 100644 index 0000000..98b7743 --- /dev/null +++ b/Dynamic Programming/C++/BoxStackingProblem.cpp @@ -0,0 +1,79 @@ +#include +#include + +struct Box +{ +int h, w, d; +}; + +int min (int x, int y) +{ return (x < y)? x : y; } + +int max (int x, int y) +{ return (x > y)? x : y; } + +int compare (const void *a, const void * b) +{ + return ( (*(Box *)b).d * (*(Box *)b).w ) - + ( (*(Box *)a).d * (*(Box *)a).w ); +} + +int maxStackHeight( Box arr[], int n ) +{ +Box rot[3*n]; +int index = 0; +for (int i = 0; i < n; i++) +{ + rot[index].h = arr[i].h; + rot[index].d = max(arr[i].d, arr[i].w); + rot[index].w = min(arr[i].d, arr[i].w); + index++; + + rot[index].h = arr[i].w; + rot[index].d = max(arr[i].h, arr[i].d); + rot[index].w = min(arr[i].h, arr[i].d); + index++; + + rot[index].h = arr[i].d; + rot[index].d = max(arr[i].h, arr[i].w); + rot[index].w = min(arr[i].h, arr[i].w); + index++; +} + +n = 3*n; + +qsort (rot, n, sizeof(rot[0]), compare); + +int msh[n]; +for (int i = 0; i < n; i++ ) + msh[i] = rot[i].h; + +for (int i = 1; i < n; i++ ) + for (int j = 0; j < i; j++ ) + if ( rot[i].w < rot[j].w && + rot[i].d < rot[j].d && + msh[i] < msh[j] + rot[i].h + ) + { + msh[i] = msh[j] + rot[i].h; + } + + +int max = -1; +for ( int i = 0; i < n; i++ ) + if ( max < msh[i] ) + max = msh[i]; + +return max; +} + +int main() +{ +Box arr[] = { {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32} }; +int n = sizeof(arr)/sizeof(arr[0]); + +printf("The maximum possible height of stack is %d\n", + maxStackHeight (arr, n) ); + +return 0; +} From e81b5ca24374dbd5b1f2ed1d4879676c28b6e08f Mon Sep 17 00:00:00 2001 From: Shwetank14 Date: Thu, 1 Oct 2020 00:57:08 +0530 Subject: [PATCH 08/12] Detect Cycle in a directed Graph --- Graph/C++/DetectCycleInADirectedGraph.cpp | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Graph/C++/DetectCycleInADirectedGraph.cpp diff --git a/Graph/C++/DetectCycleInADirectedGraph.cpp b/Graph/C++/DetectCycleInADirectedGraph.cpp new file mode 100644 index 0000000..4aa9f9b --- /dev/null +++ b/Graph/C++/DetectCycleInADirectedGraph.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + + +/* Function to check if the given graph contains cycle +* V: number of vertices +* adj[]: representation of graph +*/ +bool check_cycle(vector g[],int *visit,int *cycle,int k){ + cycle[k] = 1; + visit[k] = 1; + + for(auto i = g[k].begin();i!=g[k].end();i++){ + if(cycle[*i] == 1){ + return true; + } + + else if(cycle[*i] == 0) + if(visit[*i] != 0 && check_cycle(g,visit,cycle,*i))return true; + + } + cycle[k] = 0; + return false; +} + + +bool isCyclic(int V, vector adj[]) +{ + int visited[V] = {0}; + int cycleCheck[V] = {0}; + for(int i=0;i> t; + + while(t--){ + + int v, e; + cin >> v >> e; + + vector adj[v]; + + for(int i =0;i> u >> v; + adj[u].push_back(v); + } + + cout << isCyclic(v, adj) << endl; + + } + + return 0; +} \ No newline at end of file From 8d28be32b17a285c1e4e1471103d8220e4cf1e26 Mon Sep 17 00:00:00 2001 From: Shwetank14 Date: Thu, 1 Oct 2020 01:01:18 +0530 Subject: [PATCH 09/12] DFS traversal --- Graph/C++/DFS_traversal.cpp | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Graph/C++/DFS_traversal.cpp diff --git a/Graph/C++/DFS_traversal.cpp b/Graph/C++/DFS_traversal.cpp new file mode 100644 index 0000000..4262f71 --- /dev/null +++ b/Graph/C++/DFS_traversal.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + + + + +/* Function to do DFS of graph + +g : adjacency list of graph +N : number of vertices + +return a list containing the DFS traversal of the given graph +*/ + + + +void rec_dfs(vector g[],vector &result,int k,int *visit){ + + result.push_back(k); + visit[k] = 1; + + for(i:g[k]){ + if(visit[i] == 0){ + rec_dfs(g,result,i,visit); + } + } + + + return; +} + + +vector dfs(vector g[], int N) +{ + vector result; + + int visit[N] = {0}; + + rec_dfs(g,result,0,visit); + + + + return result; + +} + + + +int main() +{ + int T; + cin>>T; + while(T--) + { + + int N, E; + cin>>N>>E; + + vector g[N]; + bool vis[N]; + + memset(vis, false, sizeof(vis)); + + for(int i=0;i>u>>v; + g[u].push_back(v); + g[v].push_back(u); + } + + vector res = dfs(g, N); + for (int i = 0; i < res.size (); i++) + cout << res[i] << " "; + cout< Date: Thu, 1 Oct 2020 04:00:14 +0530 Subject: [PATCH 10/12] added tree --- Tree/C++/oddlevel.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Tree/C++/oddlevel.cpp diff --git a/Tree/C++/oddlevel.cpp b/Tree/C++/oddlevel.cpp new file mode 100644 index 0000000..9a56f51 --- /dev/null +++ b/Tree/C++/oddlevel.cpp @@ -0,0 +1,25 @@ +int level=1; +// int size; +if(root==nullptr) +{ +cout<<"\n"; +return ; +} +queue q; +q.push(root); +while(1){ +int size=q.size(); +if(size==0) +break; +while(size>0){ +Node *temp=q.front(); +if(level%2!=0) +cout<data<<" "; +q.pop(); +if(temp->left) +q.push(temp->left); +if(temp->right) +q.push(temp->right); +size--; +} +level++; From f1c2234b7fe806794255aad1255c7b4311d3e358 Mon Sep 17 00:00:00 2001 From: prajakta Date: Thu, 1 Oct 2020 04:02:35 +0530 Subject: [PATCH 11/12] added tree --- Tree/C++/bstfromarray.cpp | 37 +++++++ Tree/C++/fix2nodeinBST.cpp | 191 ++++++++++++++++++++++++++++++++++++ Tree/C++/isBST.cpp | 166 +++++++++++++++++++++++++++++++ Tree/C++/leafnodesum.cpp | 114 +++++++++++++++++++++ Tree/C++/leftlef.cpp | 146 +++++++++++++++++++++++++++ Tree/C++/removehalfnode.cpp | 147 +++++++++++++++++++++++++++ Tree/C++/rightleaf.cpp | 90 +++++++++++++++++ 7 files changed, 891 insertions(+) create mode 100644 Tree/C++/bstfromarray.cpp create mode 100644 Tree/C++/fix2nodeinBST.cpp create mode 100644 Tree/C++/isBST.cpp create mode 100644 Tree/C++/leafnodesum.cpp create mode 100644 Tree/C++/leftlef.cpp create mode 100644 Tree/C++/removehalfnode.cpp create mode 100644 Tree/C++/rightleaf.cpp diff --git a/Tree/C++/bstfromarray.cpp b/Tree/C++/bstfromarray.cpp new file mode 100644 index 0000000..863b73a --- /dev/null +++ b/Tree/C++/bstfromarray.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; +void create(int ar[],int start,int end) +{ + + if(start > end) + return; + + int mid = (start+end)/2; + cout<>t; + while(t--) + { + int n; + cin>>n; + int a[n]; + for(int i=0;i>a[i]; + } + + create(a,0,n-1); + + + cout< +using namespace std; + +struct Node +{ + int data; + Node *left, *right; + Node(int val) + { + data = val; + left = right = NULL; + } +}; + +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +bool isBST(Node* n, int lower, int upper) +{ + if(!n) return true; + if( n->data <= lower || n->data >= upper ) return false; + return ( isBST( n->left, lower, n->data ) && isBST( n->right, n->data, upper ) ); +} + +bool compare( Node* a, Node* b, vector> &mismatch ) +{ + if( !a && !b ) return true; + if( !a || !b ) return false; + + if( a->data != b->data ) + mismatch.push_back( pair (a->data,b->data) ); + + return ( compare( a->left, b->left, mismatch ) && compare( a->right, b->right, mismatch ) ); +} + +struct Node *correctBST( struct Node* root ); + +int main() +{ + int t; + cin>>t; + getchar(); + + while(t--) + { + string s; + getline(cin,s); + + Node* root = buildTree(s); + Node* duplicate = buildTree(s); + + root = correctBST(root); + + // check 1: is tree now a BST + if( ! isBST(root, INT_MIN, INT_MAX) ) + { + cout<< "0\n"; + continue; + } + + // check 2: comparing with duplicate tree + + vector> mismatch; + // a vector to store data of mismatching nodes + + if( ! compare( root, duplicate, mismatch) ) + { + // false output from this function indicates change in structure of tree + cout<< "0\n"; + continue; + } + + // finally, analysing the mismatching nodes + if( mismatch.size() !=2 || mismatch[0].first!=mismatch[1].second || mismatch[0].second!=mismatch[1].first ) + cout<<"0\n"; + else cout<<"1\n"; + } + return 0; +} +// } Driver Code Ends + + +/*Complete the function +Node is as follows: + +struct Node +{ + int data; + struct Node *left, *right; + Node(int x){ + int data = x; + left = right = NULL; + } +}; + +*/ +void defect(Node*root,Node* &prev,Node* &first,Node* &second) +{ + if(!root) + return; + + defect(root->left,prev,first,second); + + if(prev!=NULL && prev->data>root->data) + { + if(!first) + { + first=prev; + } + second=root; + } + prev=root; + + defect(root->right,prev,first,second); + +} + + +struct Node *correctBST( struct Node* root ) +{ + Node*first =NULL,*second = NULL,*prev = NULL; + + defect(root,prev,first,second); + swap(first->data,second->data); + + return root; +} + diff --git a/Tree/C++/isBST.cpp b/Tree/C++/isBST.cpp new file mode 100644 index 0000000..d90126d --- /dev/null +++ b/Tree/C++/isBST.cpp @@ -0,0 +1,166 @@ +// { Driver Code Starts +#include +using namespace std; +#define MAX_HEIGHT 100000 + +// Tree Node +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; + + + +bool isBST(struct Node* node); +int isBSTUtil(struct Node* node, int min, int max); + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +void inorder(Node *root, vector &v) +{ + if(root==NULL) + return; + + inorder(root->left, v); + v.push_back(root->data); + inorder(root->right, v); +} + +int main() { + + int t; + string tc; + getline(cin, tc); + t=stoi(tc); + while(t--) + { + string s; + getline(cin, s); + Node* root = buildTree(s); + cout << isBST(root) << endl; + } + return 0; +} + + + + // } Driver Code Ends + + +/* A binary tree node has data, pointer to left child + and a pointer to right child +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +*/ + +bool level(Node*root,Node* &prev) +{ + + if(root) + { + if(!level(root->left,prev)) + return false; + + if(prev!=NULL && root->data<=prev->data) + return false; + + prev=root; + + return level(root->right,prev); + } + + return true; + +} + + +// return true if the given tree is a BST, else return false +bool isBST(Node* root) { + // Your code here + + Node*prev=NULL; + + return level(root,prev); +} + + + + +// { Driver Code Starts + // } Driver Code Ends diff --git a/Tree/C++/leafnodesum.cpp b/Tree/C++/leafnodesum.cpp new file mode 100644 index 0000000..0fb0343 --- /dev/null +++ b/Tree/C++/leafnodesum.cpp @@ -0,0 +1,114 @@ +// { Driver Code Starts +//Author- SAJAL AGRAWAL +//sajal.agrawal1997@gmail.com + +#include +#include +#include + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x){ + data = x; + left = NULL; + right = NULL; + } +}; + +Node *insert(Node *r,int num); +int sumOfLeafNodes(Node *r); +void preOrderDisplay(Node *r); +void inOrderDisplay(Node *r); +void postOrderDisplay(Node *r); +int search(Node *r, int num); + +int main(void) { + int t,n,data; + scanf("%d",&t); + while(t--){ + Node *root=NULL; + scanf("%d",&n); + //printf("n=%d\n",n); + while(n--){ + scanf("%d",&data); + //printf("data=%d\n",data); + root=insert(root,data); + } + //inOrderDisplay(root); + printf("%d\n",sumOfLeafNodes(root)); + } + return 0; +} + +Node *insert(Node *r,int num){ + if(r==NULL){ + r = new Node(num); + }else{ + if(numdata){ + r->left=insert(r->left,num); + }else{ + r->right=insert(r->right,num); + } + } + return r; +} + +void preOrderDisplay(Node *r){ + if(r!=NULL){ + printf("%d ",r->data); + preOrderDisplay(r->left); + preOrderDisplay(r->right); + } +} + +void inOrderDisplay(Node *r){ + if(r!=NULL){ + inOrderDisplay(r->left); + printf("%d ",r->data); + inOrderDisplay(r->right); + } +} + +void postOrderDisplay(Node *r){ + if(r!=NULL){ + postOrderDisplay(r->left); + postOrderDisplay(r->right); + printf("%d ",r->data); + } +} + +int search(Node *r,int num){ + if(r==NULL)return 0; + else if(r->data==num)return 0; + else if(r->data > num) + search(r->left,num); + else + search(r->right,num); +}// } Driver Code Ends + + +/* The structure of Node +struct Node{ + int data; + Node *left,*right; +}; */ + + +/*You are required to complete below method */ +int sumOfLeafNodes(Node *r ){ + /*Your code here */ + + if(!r) + return 0; + else{ + if(!r->left && !r->right) + {return r->data;} + else{ + return (sumOfLeafNodes(r->left)+sumOfLeafNodes(r->right)); + } + } +} diff --git a/Tree/C++/leftlef.cpp b/Tree/C++/leftlef.cpp new file mode 100644 index 0000000..a222457 --- /dev/null +++ b/Tree/C++/leftlef.cpp @@ -0,0 +1,146 @@ +// { Driver Code Starts +#include +using namespace std; +#define MAX_HEIGHT 100000 + +// Tree Node +struct Node +{ + int data; + Node* left; + Node* right; +}; + +// Utility function to create a new Tree Node +Node* newNode(int val) +{ + Node* temp = new Node; + temp->data = val; + temp->left = NULL; + temp->right = NULL; + + return temp; +} + + + +int leftLeavesSum(Node *root); + +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = newNode(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = newNode(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} + +void inorder(Node *root, vector &v) +{ + if(root==NULL) + return; + + inorder(root->left, v); + v.push_back(root->data); + inorder(root->right, v); +} + +int main() { + //freopen("input.txt","r", stdin); + //freopen("output.txt","w", stdout); + int t; + string tc; + getline(cin, tc); + t=stoi(tc); + //cout << t << endl; + while(t--) + { + string s; + getline(cin, s); + Node* root = buildTree(s); + + //getline(cin, s); + + cout << leftLeavesSum(root) << endl; + + //cout<<"~"<left && !root->left->left && !root->left->right) + return (root->left->data+leftLeavesSum(root->right)); + + return leftLeavesSum(root->left)+leftLeavesSum(root->right); +} diff --git a/Tree/C++/removehalfnode.cpp b/Tree/C++/removehalfnode.cpp new file mode 100644 index 0000000..2a8203a --- /dev/null +++ b/Tree/C++/removehalfnode.cpp @@ -0,0 +1,147 @@ +// { Driver Code Starts +#include +using namespace std; + +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +// Function to Build Tree +Node* buildTree(string str) +{ + // Corner Case + if(str.length() == 0 || str[0] == 'N') + return NULL; + + // Creating vector of strings from input + // string after spliting by space + vector ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // Create the root of the tree + Node* root = new Node(stoi(ip[0])); + + // Push the root to the queue + queue queue; + queue.push(root); + + // Starting from the second element + int i = 1; + while(!queue.empty() && i < ip.size()) { + + // Get and remove the front of the queue + Node* currNode = queue.front(); + queue.pop(); + + // Get the current node's value from the string + string currVal = ip[i]; + + // If the left child is not null + if(currVal != "N") { + + // Create the left child for the current node + currNode->left = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->left); + } + + // For the right child + i++; + if(i >= ip.size()) + break; + currVal = ip[i]; + + // If the right child is not null + if(currVal != "N") { + + // Create the right child for the current node + currNode->right = new Node(stoi(currVal)); + + // Push it to the queue + queue.push(currNode->right); + } + i++; + } + + return root; +} +void inorder(Node * node) +{ + if(node==NULL) + return; + + inorder(node->left); + cout<data<<" "; + inorder(node->right); +} +Node* RemoveHalfNodes(Node* root) ; + +int main() +{ + + int t; + scanf("%d ",&t); + while(t--) + { + string s; + getline(cin,s); + Node* root = buildTree(s); + Node * fresh = RemoveHalfNodes(root); + inorder(fresh); + cout<left==NULL && root->right==NULL) + return root; + + if(root->left==NULL || root->right==NULL ) + { + Node*temp; + if(root->left) + temp=root->left; + else + if(root->right) + temp=root->right; + + delete root; + return RemoveHalfNodes(temp); + } + root->left = RemoveHalfNodes(root->left); + root->right = RemoveHalfNodes(root->right); + +return root; +} diff --git a/Tree/C++/rightleaf.cpp b/Tree/C++/rightleaf.cpp new file mode 100644 index 0000000..878d339 --- /dev/null +++ b/Tree/C++/rightleaf.cpp @@ -0,0 +1,90 @@ +// { Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left; + struct Node *right; + + Node(int x){ + data = x; + left = NULL; + right = NULL; + } +}; + +void inorder(Node *root) +{ + if (root == NULL)return; + inorder(root->left); + cout << root->data << " "; + inorder(root->right); +} + +int rightLeafSum(Node* root); + +/* Driver program to test size function*/ +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + map m; + int n; + scanf("%d",&n); + struct Node *root = NULL; + struct Node *child; + while (n--) + { + Node *parent; + char lr; + int n1, n2; + scanf("%d %d %c", &n1, &n2, &lr); + if (m.find(n1) == m.end()) + { + parent = new Node(n1); + m[n1] = parent; + if (root == NULL) + root = parent; + } + else + parent = m[n1]; + child = new Node(n2); + if (lr == 'L') + parent->left = child; + else + parent->right = child; + m[n2] = child; + } + // inorder(root); + // cout<right && !root->right->left && !root->right->right) + return root->right->data + rightLeafSum(root->left); + + + return rightLeafSum(root->left) + rightLeafSum(root->right); +} From 3a88222d8069fec75041dbe952064064b409cb3c Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Thu, 1 Oct 2020 08:11:51 +0530 Subject: [PATCH 12/12] Vertical Order Traversal of a Binary Tree --- Vertical Order Traversal of a Binary Tree | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Vertical Order Traversal of a Binary Tree diff --git a/Vertical Order Traversal of a Binary Tree b/Vertical Order Traversal of a Binary Tree new file mode 100644 index 0000000..59fcd60 --- /dev/null +++ b/Vertical Order Traversal of a Binary Tree @@ -0,0 +1,37 @@ + +void solve(TreeNode * root, map>>&mp , int hd, int depth){ + if(root==NULL){ + return ; + } + mp[hd].push_back({depth,root->val}); + solve(root->left , mp,hd-1,depth+1); + solve(root->right,mp,hd+1,depth+1); +} + + +class Solution { +public: + vector> verticalTraversal(TreeNode* root) { + vector>A; + if(root==NULL){ + return A; + } + + map>>mp; + solve(root,mp,0,0); + + for(auto it=mp.begin();it!=mp.end();it++){ + sort(it->second.begin(),it->second.end()); + vectorB; + for(int i=0;isecond.size();i++){ + B.push_back(it->second[i].second); + } + + A.push_back(B); + + } + + return A; + + } +};