From aa6599e5551440a0555cb8591fe2f10c0a75d2e4 Mon Sep 17 00:00:00 2001 From: priysnty123 <118986828+priysnty123@users.noreply.github.com> Date: Mon, 30 Oct 2023 22:20:01 +0530 Subject: [PATCH] This code are related to linked list topic. NAME = PRIYANSHU TIWARI Github= priysnty123 --- C++/LINKED LIST/createlinkedlist.cpp | 62 ++++++++++++++ C++/LINKED LIST/cyclicdetection.cpp | 96 +++++++++++++++++++++ C++/LINKED LIST/deleteduplicate.cpp | 77 +++++++++++++++++ C++/LINKED LIST/findMIDDLE.cpp | 82 ++++++++++++++++++ C++/LINKED LIST/mergelinkedlist.cpp | 113 ++++++++++++++++++++++++ C++/LINKED LIST/pallindrome.cpp | 119 ++++++++++++++++++++++++++ C++/LINKED LIST/reverselinkedlist.cpp | 98 +++++++++++++++++++++ C++/LINKED LIST/swapadjacent.cpp | 80 +++++++++++++++++ 8 files changed, 727 insertions(+) create mode 100644 C++/LINKED LIST/createlinkedlist.cpp create mode 100644 C++/LINKED LIST/cyclicdetection.cpp create mode 100644 C++/LINKED LIST/deleteduplicate.cpp create mode 100644 C++/LINKED LIST/findMIDDLE.cpp create mode 100644 C++/LINKED LIST/mergelinkedlist.cpp create mode 100644 C++/LINKED LIST/pallindrome.cpp create mode 100644 C++/LINKED LIST/reverselinkedlist.cpp create mode 100644 C++/LINKED LIST/swapadjacent.cpp diff --git a/C++/LINKED LIST/createlinkedlist.cpp b/C++/LINKED LIST/createlinkedlist.cpp new file mode 100644 index 0000000..614d83b --- /dev/null +++ b/C++/LINKED LIST/createlinkedlist.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +class Node { + public: + + int val; + Node* next; + + Node (int data){ + val = data; + next = NULL; + } +}; + + class linklist{ + public: + Node * head; + linklist(){ + + head = NULL; + } + +void insertattail(int value){ + Node* new_node = new Node(value); + if (head == NULL){ + head = new_node;// head ko hi new node maan liya + return; + } + Node * temp = head; + while(temp->next!=NULL){ + temp = temp->next; + } + temp->next= new_node; + + } +void display(){ + Node *temp = head; + while(temp!=NULL){ + cout<val<<"->"; + temp = temp->next; + + } cout<< "NULL"< +using namespace std; +#define INT_MIN (-2147483647 - 1) + +struct Node +{ + int val; + Node *next; + + Node(int data) + { + val = data; + next = nullptr; + } +}; + +void insert(Node *&head, int value) +{ + Node *newNode = new Node(value); + Node *temp = head; + if (!head) + { + head = newNode; + return; + } + + while (temp->next != nullptr) + temp = temp->next; + + temp->next = newNode; +} + +int cyclePoint(Node *&head) +{ + if (!head) + { + return INT_MIN; + } + Node *slow = head, *fast = head; + + bool check = false; + while (fast != nullptr && fast->next != nullptr) + { + fast = fast->next->next; + slow = slow->next; + if (fast == slow) + { + check = true; + break; + } + } + + if (!check) + return INT_MIN; + + fast = head; + while (fast != slow) + { + fast = fast->next; + slow = slow->next; + } + + return fast->val; +} + +void display(Node *head) +{ + while (head != nullptr) + { + cout << head->val << "->"; + head = head->next; + } + cout << "NULL\n"; +} +int main() +{ + Node *ll = NULL; + // for (int i = 0; i < 10; i++) + // { + // insert(ll, i); + // } + + insert(ll, 1); + insert(ll, 2); + insert(ll, 3); + insert(ll, 4); + insert(ll, 5); + insert(ll, 6); + insert(ll, 7); + + ll->next->next->next->next->next->next->next = ll->next->next->next->next; + cout << cyclePoint(ll) << endl; + + // display(ll); + return 0; +} \ No newline at end of file diff --git a/C++/LINKED LIST/deleteduplicate.cpp b/C++/LINKED LIST/deleteduplicate.cpp new file mode 100644 index 0000000..c32fe1c --- /dev/null +++ b/C++/LINKED LIST/deleteduplicate.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +class Node { + public: + + int val; + Node* next; + + Node (int data){ + val = data; + next = NULL; + } +}; + + class linklist{ + public: + Node * head; + linklist(){ + + head = NULL; + } + +void insertattail(int value){ + Node* new_node = new Node(value); + if (head == NULL){ + head = new_node;// head ko hi new node maan liya + return; + } + Node * temp = head; + while(temp->next!=NULL){ + temp = temp->next; + } + temp->next= new_node; + + } +void display(){ + Node *temp = head; + while(temp!=NULL){ + cout<val<<"->"; + temp = temp->next; + + } cout<< "NULL"<next && curr_node->val == curr_node->next->val){ + //DELETE CURRENT NEXT + Node* temp = curr_node->next; + curr_node->next= curr_node->next->next; + free(temp); + + } + // YE LOOP TAB LHATAM HOGA JAB VA;UE DIIFRENT AYEGI CURRENT KI AND USKE NEXT KI + // LL KHATAM + curr_node = curr_node->next; } + + }; + +int main() +{ +linklist ll; +ll.insertattail(1); +ll.insertattail(2); +ll.insertattail(2); +ll.insertattail(3); +ll.insertattail(3); +ll.insertattail(3); +ll.display(); +deleteduplicatenodes(ll.head); +ll.display(); + return 0; +} \ No newline at end of file diff --git a/C++/LINKED LIST/findMIDDLE.cpp b/C++/LINKED LIST/findMIDDLE.cpp new file mode 100644 index 0000000..88f4a63 --- /dev/null +++ b/C++/LINKED LIST/findMIDDLE.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +class Node{ + public: + + int val; + Node* next; + + Node( int data){ + + val = data; + next = NULL; + + } +}; + +class linklist{ + public: + + Node* head; + + linklist(){ + head = NULL; + + } + + void insertattail(int value){ + + Node* new_node = new Node (value); + if(head==NULL){ + head = new_node; + } +Node* temp = head; +while(temp->next != NULL){ + temp = temp->next; + +} +temp->next = new_node; + new_node->next= NULL; + + + } + + void display(){ + Node* temp = head; + while (temp!= NULL){ +cout<val<< "->"; +temp = temp->next; + + } + cout << "NULL"<< endl; +} +}; +Node* middleelement(Node* &head){ + Node* slow = head; + Node* fast = head; + +//STOPPING CONDITITON IF EVEN AND ODD HONGYE TAB + while (fast && fast->next){ + slow = slow->next; + fast = fast->next->next; + + } + return slow; + +} + +int main() +{ +linklist ll; + ll.insertattail(1); + ll.insertattail(2); + ll.insertattail(3); + ll.insertattail(4); + ll.insertattail(5); + ll.display(); + Node* middlenode = middleelement(ll.head); + cout<val< +using namespace std; + +class Node{ + public: + + int val; + Node* next; + + Node( int data){ + + val = data; + next = NULL; + + } +}; + +class linklist{ + public: + + Node* head; + + linklist(){ + head = NULL; + + } + + void insertattail(int value){ + + Node* new_node = new Node (value); + if(head==NULL){ + head = new_node; + } +Node* temp = head; +while(temp->next != NULL){ + temp = temp->next; + +} +temp->next = new_node; + new_node->next= NULL; + + + } + + void display(){ + Node* temp = head; + while (temp!= NULL){ +cout<val<< "->"; +temp = temp->next; + + } + cout << "NULL"<< endl; +} +}; + + Node* mergell(Node* head1, Node* head2){ + +Node* dummyhead = new Node(-1); + +Node* ptr1 = head1; +Node* ptr2 = head2; +Node* ptr3 = dummyhead; + +while (ptr1 && ptr2){ // JAB TAK DONO HOGA TAB TAK YE CHALEGA EK BHI NULL HUA TOH LOPP BREAK +if (ptr1->val < ptr2->val){ + +ptr3->next = ptr1; +ptr1 = ptr1->next ; + +} +else{ + +ptr3->next = ptr2 ; +ptr2 = ptr2->next; + +} + +ptr3 = ptr3->next; // LOOP (IF ELSE)SE BAHARA AA K PTR3 BHADYA BECAUSE YE TOH HAMESA BADHENGYE + +} +// AGAR KOI EK PEHELE HI NULL HO JAYE +if(ptr1 == NULL){ + + ptr3->next= ptr2; + +} +else{ + ptr3->next = ptr1; +} +return dummyhead->next; + + } + + int main() +{ + linklist ll1; + ll1.insertattail(1); + ll1.insertattail(2); + ll1.insertattail(7); + ll1.insertattail(9); + ll1.display(); + + linklist ll2; + ll2.insertattail(4); + ll2.insertattail(8); + ll2.display(); + + linklist ll3; + ll3.head = mergell(ll1.head, ll2.head); + ll3.display(); + + return 0; +} \ No newline at end of file diff --git a/C++/LINKED LIST/pallindrome.cpp b/C++/LINKED LIST/pallindrome.cpp new file mode 100644 index 0000000..19d01b4 --- /dev/null +++ b/C++/LINKED LIST/pallindrome.cpp @@ -0,0 +1,119 @@ +#include +using namespace std; + +class Node{ + public: + + int val; + Node* next; + + Node( int data){ + + val = data; + next = NULL; + + } +}; + +class linklist{ + public: + + Node* head; + + linklist(){ + head = NULL; + + } + + void insertattail(int value){ + + Node* new_node = new Node (value); + if(head==NULL){ + head = new_node; + } +Node* temp = head; +while(temp->next != NULL){ + temp = temp->next; + +} +temp->next = new_node; + new_node->next= NULL; + + + } + + void display(){ + Node* temp = head; + while (temp!= NULL){ +cout<val<< "->"; +temp = temp->next; + + } + cout << "NULL"<< endl; +} +}; + +bool pallindrome(Node* &head){ +//1. find middle + +Node* slow = head; +Node* fast = head; + +while (fast && fast->next){ + slow = slow->next; + fast = fast->next->next; +} + // now slow is pointing the middle node + //2. BREAK FROM THE MIDDLE + Node* curr = slow->next; + Node* prev = slow ; + slow->next = NULL; + + //3. REVERSE THE 2ND PART +while (curr){ + Node* nextNode = curr->next; + curr->next = prev; + prev = curr; + curr = nextNode; + +}// LL IS BREAK IN 2 PArt 1st is straight(head1) and 2nd is reverse(head2 = prev); + +// 4. CHECK THE VALUE OF LL + + +Node* head1 = head; +Node* head2 = prev; +while(head2){ +if (head1->val!= head2->val){ + return false; +} +head1 = head1->next; +head2 = head2->next; + + + +return true; +} +}; + + + +int main(){ + + linklist ll; + ll.insertattail(1); + ll.insertattail(2); + ll.insertattail(3); + ll.insertattail(3); + ll.insertattail(2); + ll.insertattail(1); + + ll.display(); +cout< +using namespace std; + +class Node { + public: + + int val; + Node* next; + + Node (int data){ + val = data; + next = NULL; + } +}; + + class linklist{ + public: + Node * head; + linklist(){ + + head = NULL; + } + +void insertattail(int value){ + Node* new_node = new Node(value); + if (head == NULL){ + head = new_node;// head ko hi new node maan liya + return; + } + Node * temp = head; + while(temp->next!=NULL){ + temp = temp->next; + } + temp->next= new_node; + + } +void display(){ + Node *temp = head; + while(temp!=NULL){ + cout<val<<"->"; + temp = temp->next; + + } cout<< "NULL"< next = prev; + // move all 3 ptr by one step ahead + while ( curr!=NULL){ + Node* nextptr = curr->next; + curr->next = prev; + prev = curr; + curr = nextptr; + // nextptr = next ->next; + + } + // jab looop khatam higa tab prev ptr pointing last node which is pointing new head;. + + Node * new_node = prev; + + return new_node; +}; + Node* reversellrecursion(Node* &head) { + // base case if empty ll + if(head == NULL || head->next == NULL) { + return head; + }// RECURSION CASE + Node* new_head = reversellrecursion(head->next); + head->next->next = head; + head->next = NULL; // jo head tha woh abbb last node ban gya reversell ka + return new_head; + + } + +int main() +{ + + + linklist ll; + + ll.insertattail(1); + ll.insertattail(2); + ll.insertattail(3); + ll.insertattail(4); + ll.insertattail(5); + ll.insertattail(6); + + ll.display(); + ll.head = reversellrecursion (ll.head); + ll.display(); + + return 0; +} \ No newline at end of file diff --git a/C++/LINKED LIST/swapadjacent.cpp b/C++/LINKED LIST/swapadjacent.cpp new file mode 100644 index 0000000..15fde47 --- /dev/null +++ b/C++/LINKED LIST/swapadjacent.cpp @@ -0,0 +1,80 @@ + #include +using namespace std; + + + class Node{ + public: + + int val; + Node* next; + + Node(int data){ + val = data; + next = NULL; + + } + + }; + + class linklist{ + public: + + Node* head; + + linklist(){ + head = NULL; + + } + +void insertattail(int value){ + Node* new_node = new Node(value); + if (head == NULL){ // linklist empty toh new node banaya uppar + head = new_node; + // head ko hi new node se poinnt kara diya + return; + +} // now hame last mai jana hai toh traverse krenegye + Node* temp = head; // temp banya last tak jana h temp _> next = null tk + while (temp->next!= NULL){ // tail mai add krna hai issi liye + temp = temp->next; + } + temp->next = new_node; +} +void display(){ + Node* temp = head; + while (temp != NULL){ // yaha pura travverse krna hia + cout<val<< "->"; + temp = temp->next; + + }cout<<"NULL"<next == NULL){ + return head; + } + // recursive call case + + Node* secondnode = head->next; // 1-2 (save kiya) firrr ageee -(recursive-4-3-6-5 ) + + head->next = swapadjacent(secondnode->next); + secondnode->next = head; // reversing the link between 1st and 2nd node + return secondnode; // becz new ll ka head hai 2-1-4-3-6-5 +} +int main(){ + linklist ll; + + ll.insertattail(1); + ll.insertattail(2); + ll.insertattail(3); + ll.insertattail(4); + ll.insertattail(5); + ll.insertattail(6); + ll. display(); + + ll.head = swapadjacent(ll.head); + ll.display(); +} \ No newline at end of file