File
File
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
public:
Node(int data2)
{
data = data2;
next = nullptr;
}
};
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
public:
Node(int data2)
{
data = data2;
next = nullptr;
}
};
if(prev==NULL&&front==NULL)
{
return NULL;
}
else if(prev==NULL)
{
return deleteheadofDLL(head);
}
else if(front==NULL)
{
return deletetailofDLL(head);
}
prev->next = front;
front->back = prev;
temp->next = nullptr;
temp->back = nullptr;
delete temp;
return head;
}
int main()
{
vector<int> a = {5,6,1,2,3,4};
vector<int> b = {1};
vector<int> c = {};
Node* head = convertarraytoDoublyLinkedList(a);
print(head);
//cout<<lengthofDLL(head)<<endl;
head = insertbeforeKthelementinDLL(head,10,5);
print(head);
//head = deletekthekthElementofDLL(head,10);
//print(head);
//head = deletetailofDLL(head);
//print(head);
return 0;
}
int main()
{
vector<int> a = {3,5};
vector<int> b = {4,5,9,9};
Node* head1 = convertarraytoDoublyLinkedList(a);
Node* head2 = convertarraytoDoublyLinkedList(b);
print(head1);
print(head2);
//cout<<lengthofDLL(head)<<endl;
Node* head3 = sumoftwonumbers(head1,head2);
print(head3);
//head = deletekthekthElementofDLL(head,10);
//print(head);
//head = deletetailofDLL(head);
//print(head);
return 0;
}
odd = odd->next;
even = even->next;
}
odd->next = evenHead;
return head;
}
int main()
{
vector<int> a = {1,2,3,4,5,6,7};
Node* head1 = convertarraytoDoublyLinkedList(a);
print(head1);
Node* head = oddEvenList(head1);
print(head);
//cout<<lengthofDLL(head)<<endl;
//head = deletekthekthElementofDLL(head,10);
//print(head);
//head = deletetailofDLL(head);
//print(head);
return 0;
}
//reverse a linkedlist
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};
Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}
void display(Node* head)
{
Node* temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
// Recursive step:
// Reverse the linked list starting
// from the second node (head->next).
Node* newHead = reverseLinkedList(head->next);
int main()
{
vector<int> a = {9,0,1,3,6};
Node* head = arraytoLL(a);
display(head);
head = reverseLinkedList(head);
display(head);
}
class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};
Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}
int main()
{
vector<int> a = {1,4,6,6,4,1};
Node* head = arraytoLL(a);
display(head);
cout<<optimisedpalindrome(head);
}
//sort 0 1 2
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};
Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}
int main()
{
vector<int> a = {9,8,7,6,5};
Node* head = arraytoLL(a);
display(head);
head = reverseLinkedList(head);
display(head);
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a = {-1,4,4,5,-2,4,9};
int i=0,j=0;
list<int>l;
while(j<a.size())
{
if(a[j]<0)
{
l.push_back(a[j]);
}
if(j-i+1<3)
{
j++;
}
else
{
cout<<l.front()<<" ";
if(a[i]<0)
{
l.pop_front();
}
i++;
j++;
}
}
}
int main()
{
vector<string> a = {"ate","bat","cat","tae","act","eat"};
display(a);
groupanagram(a);
}
//relative sort
#include <bits/stdc++.h>
using namespace std;
void relativesort(vector<int> a, vector<int> b)
{
map<int, int> mp;
for(int i=0;i<a.size();i++)
{
mp[a[i]]++;
}
for(int i=0;i<b.size();i++)
{
if(mp.find(b[i])!=mp.end())
{
auto iter = mp.find(b[i]);
int count = iter->second;
while(count!=0)
{
cout<<iter->first<<" ";
count--;
}
mp.erase(b[i]);
}
}
for(auto iter = mp.begin();iter!=mp.end();iter++)
{
int count = iter->second;
int val = iter->first;
while(count!=0)
{
cout<<iter->first<<" ";
count--;
}
}
}
void customsort(string s, string order)
{
map<char, int> mp;
for(int i=0;i<s.size();i++)
{
mp[s[i]]++;
}
for(int i=0;i<order.size();i++)
{
if(mp.find(order[i])!=mp.end())
{
auto iter = mp.find(order[i]);
int count = iter->second;
while(count!=0)
{
cout<<iter->first;
count--;
}
mp.erase(order[i]);
}
}
for(auto iter = mp.begin();iter!=mp.end();iter++)
{
int count = iter->second;
int val = iter->first;
while(count!=0)
{
cout<<iter->first;
count--;
}
}
}
void display(vector<int> a)
{
for(auto val:a)
{
cout<<val<<" ";
}
cout<<endl;
}
int main()
{
vector<int> a = {2,3,6,1,3,2,4,8,6,7,9,2};
vector<int> b = {2,1,4,3,9};
display(a);
display(b);
relativesort(a,b);
string s = "adbhcadh";
string order = "cba";
cout<<endl<<s<<endl<<order<<endl;
customsort(s,order);
}
class Node
{
public:
int data;
Node* next;
public:
Node(int data1, Node* next1)
{
data = data1;
next = next1;
}
Node(int data1)
{
data = data1;
next = nullptr;
}
};
Node* arraytoLL(vector<int> a)
{
Node* head = new Node(a[0]);
Node* mover = head;
for(int i=1;i<a.size();i++)
{
Node* temp = new Node(a[i]);
mover->next=temp;
mover=temp;
}
return head;
}