From d7c1a4cd7b74b350e33dbaa4fff6e940a333e9c8 Mon Sep 17 00:00:00 2001 From: Vamika Arya Date: Tue, 14 Jan 2025 15:20:27 +0530 Subject: [PATCH 1/2] added file for Split_Circular_List_into_2_halves --- .../Split_Circular_List_into_2_halves.cpp | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp diff --git a/Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp b/Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp new file mode 100644 index 00000000..a4dd0a96 --- /dev/null +++ b/Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; +class Node{ + public: + int data; + Node* next; + Node(int data){ + this->data=data; + this->next=NULL; + } +}; +void print_LL (Node* &head){ + Node* temp=head; + while(temp->next!=head){ + cout<data<<" "; + temp=temp->next; + } + cout<data<next=head; + tail->next=new_node; + head=new_node; +} +int length(Node* &head){ + int length=1; + Node* temp=head; + while(temp->next!=head){ + length++; + temp=temp->next; + } + return length; +} +void splitinto2(Node* &head,Node* &tail){ + Node* temp=head; + int pivot=0; +//finding node pos about which to split CLL + int size=length(head); + if(size%2==0){pivot=size/2;} + else{pivot=(size+1)/2;} + int count=1; + while(count!=pivot){ + temp=temp->next; + count++; + } + Node* head2=temp->next; + temp->next=head; + cout<<"First Half of given CLL"<next=head2; + cout<<"Second Half of given CLL"< Date: Tue, 14 Jan 2025 19:06:33 +0530 Subject: [PATCH 2/2] added file/approach-1/sort_LL of 0s,1s,2s --- .../sort_approach-1.cpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Lecture049 Linked List Day6/sort_approach-1.cpp diff --git a/Lecture049 Linked List Day6/sort_approach-1.cpp b/Lecture049 Linked List Day6/sort_approach-1.cpp new file mode 100644 index 00000000..8c95c6f7 --- /dev/null +++ b/Lecture049 Linked List Day6/sort_approach-1.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; +class Node{ + public: + int data; + Node* next; + Node(int data){ + this->data=data; + this->next=NULL; + } +}; +void print_LL (Node* &head){ + Node* temp=head; + while(temp->next!=NULL){ + cout<data<<" "; + temp=temp->next; + } + cout<data<next=head; + head=new_node; +} +void sort(Node* &head){ + int count_0=0; + int count_1=0; + int count_2=0; + Node* temp=head; + while(temp!=NULL){ + if(temp->data==0){count_0++;} + else if(temp->data==1){count_1++;} + else{count_2++;} + temp=temp->next; + } + temp=head; + for(int i=1;i<=count_0;i++){ + temp->data=0; + temp=temp->next; + + } + for(int i=1;i<=count_1;i++){ + temp->data=1; + temp=temp->next; + } + for(int i=1;i<=count_2;i++){ + temp->data=2; + temp=temp->next; + } +} +int main(){ + Node* node1=new Node(0); + Node* head=node1; + Node* tail=node1; + InsertAtHead(1,head); + InsertAtHead(0,head); + InsertAtHead(0,head); + InsertAtHead(2,head); + InsertAtHead(0,head); + cout<<"LINKED LIST BEFORE SORTING"<