Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan2309 committed May 30, 2021
1 parent 7c86927 commit 6b063d9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
Binary file modified LinkedList/a.exe
Binary file not shown.
4 changes: 3 additions & 1 deletion LinkedList/kSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ if we find it is smaller then insert
* We are doing on O(nlogk) approach -> code below*
mean heap gives lowest value max heap gives highest value
*/

#include <bits/stdc++.h>
Expand Down Expand Up @@ -76,7 +78,7 @@ struct compare //{changing to min heap}
{
bool operator()(node *first, node *second)
{
return first->data > second->data;
return first->data > second->data; // gives ascending
}
};
node *kSort(node *&head, int k)
Expand Down
112 changes: 112 additions & 0 deletions LinkedList/rotaten.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Rotate Doubly linked list by N nodes
Given a doubly linked list, rotate the linked list counter-clockwise by N nodes.
Here N is a given positive integer and is smaller than the count of nodes in linked list.
input 1,2,3,4,5
output 3,4,5,1,2
*/

#include <bits/stdc++.h>
using namespace std;

class node
{
public:
int data;
node *next;
node *prev; //for dll

node(int val)
{ //constructor
data = val;
next = NULL;
prev = NULL; //for dll
}
};

void insertAtHead(node *&head, int val)
{
node *n = new node(val);
n->next = head;
if (head != NULL) //if head is there then only we can point to prev
{
head->prev = n; //for dll
}
head = n;
}

void insertAtTail(node *&head, int val) //head by ref and not by val because we have to mod ll
{
node *n = new node(val);
if (head == NULL)
{
insertAtHead(head, val);
return;
}
node *temp = head; //iterator (will traverse the ll)
while (temp->next != NULL)
{
temp = temp->next;
} //we are at last element now
temp->next = n; // n->next has already value NULL
n->prev = temp; // for dll
}

void display(node *head) //head by value because wea re not mod ll
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

node *rotateN(node *&head, int n)
{ //head is pointing 1
if (n == 0)
{
return head;
}
node *temp = head; //iterator
int count = 1;
while (count < n and temp != NULL)
{
temp = temp->next;
count++;
}
node *newHead = temp; //pointing 2 as per example
while (temp->next != NULL)
{
temp = temp->next; //sending to last element
} //temp pointing 5

//connecting points

temp->next = head; //5 next is 1
head->prev = temp; //1 prev is 5
head = newHead->next; //new head is 3
head->prev = NULL; //prev of 3 pointing NULL
newHead->next = NULL; //2 next is null

return head;
}

int main()
{
int n = 2;
node *head = NULL;
insertAtTail(head, 1);
insertAtTail(head, 2);
insertAtTail(head, 3);
insertAtTail(head, 4);
insertAtTail(head, 5);
display(head);
head = rotateN(head, n);
display(head);

return 0;
}

0 comments on commit 6b063d9

Please sign in to comment.