Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort_LL-approach-1 #572

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Lecture048 Linked List Day5/Split_Circular_List_into_2_halves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
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<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
void InsertAtHead(int data,Node* &head,Node* tail){
Node* new_node=new Node(data);
new_node->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"<<endl;
print_LL(head);
tail->next=head2;
cout<<"Second Half of given CLL"<<endl;
print_LL(head2);
}
int main(){
Node* node1=new Node(100);
Node* head=node1;
Node* tail=node1;
InsertAtHead(10,head,tail);
InsertAtHead(20,head,tail);
InsertAtHead(30,head,tail);
InsertAtHead(40,head,tail);
InsertAtHead(50,head,tail);
cout<<"COMPLETE CIRCULAR LINKED LIST"<<endl;
print_LL(head);
splitinto2(head,tail);
return 0;
}
66 changes: 66 additions & 0 deletions Lecture049 Linked List Day6/sort_approach-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
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<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
void InsertAtHead(int data,Node* &head){
Node* new_node=new Node(data);
new_node->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"<<endl;
print_LL(head);
cout<<"LINKED LIST AFTER SORTING"<<endl;
sort(head);
print_LL(head);
return 0;
}