Skip to content
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
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
127 changes: 127 additions & 0 deletions Lecture048 Linked List Day5/split_CircularLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <iostream>
using namespace std;

class Node {

public:
int data;
Node* next;

Node(int data){
this-> data = data;
this -> next = NULL;
}

};

void print(Node* tail){
if (tail == NULL) return;

Node* temp = tail;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != tail);
cout << endl;
}

int getcount(Node* head){

Node* temp = head;
int count=1;
while(temp->next != head){
count++;
temp = temp -> next;
}

return count;
}

void split(Node* head, Node*& head1, Node*& head2) {
if (head == NULL) return;

int count = getcount(head);
int half = count / 2+ 1;

Node* temp = head;
int i = 1;
while (i < half) {
temp = temp->next;
i++;
}


head1 = head;
head2 = temp->next;

Node* tail1 = temp;
Node* tail2 = head2;

// Complete first circular list
tail1->next = head1;

// Traverse to last node of second list
while (tail2->next != head) {
tail2 = tail2->next;
}

// Complete second circular list
tail2->next = head2;
}



void insertnode( Node* &tail, int element, int d){
//asssuming that the element is present in the list

//empty list
if(tail == NULL){
Node* newNode = new Node(d);
tail = newNode;
newNode -> next = newNode;
}
else{
//non-empty list
Node* curr = tail;


while(curr-> data != element ){
curr = curr -> next;
}

Node* temp = new Node(d);
temp -> next = curr -> next;
curr -> next = temp;
}

}



int main() {
Node *tail = NULL;

insertnode(tail, 5, 3);
insertnode(tail, 3, 10);
insertnode(tail, 10, 9);
insertnode(tail, 3, 11);
insertnode(tail, 3, 56);

cout << "Original list: ";
print(tail);

cout << "Count is: " << getcount(tail) << endl;

Node* head1 = NULL;
Node* head2 = NULL;

split(tail, head1, head2);

cout << "First half: ";
print(head1);

cout << "Second half: ";
print(head2);

return 0;
}