0% found this document useful (0 votes)
28 views4 pages

Union of Linked

The document describes several algorithms for performing operations on linked lists: 1. Union of two linked lists adds all elements from two lists to a new list without duplicates. 2. Intersection of two linked lists adds elements that are common to both lists. 3. Merging two sorted linked lists combines the lists while maintaining sort order. 4. Concatenating two linked lists joins the second list to the end of the first. 5. Deleting duplicates from a linked list removes any repeated elements. 6. Checking if a linked list is a palindrome by reversing half and comparing elements. 7. Finding the frequency of one string in another by traversing linked lists.

Uploaded by

LoveBabbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Union of Linked

The document describes several algorithms for performing operations on linked lists: 1. Union of two linked lists adds all elements from two lists to a new list without duplicates. 2. Intersection of two linked lists adds elements that are common to both lists. 3. Merging two sorted linked lists combines the lists while maintaining sort order. 4. Concatenating two linked lists joins the second list to the end of the first. 5. Deleting duplicates from a linked list removes any repeated elements. 6. Checking if a linked list is a palindrome by reversing half and comparing elements. 7. Finding the frequency of one string in another by traversing linked lists.

Uploaded by

LoveBabbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Union of Linked-List :
void insertAtEnd(int d, node*&head, node*&tail)
{
if (head == 0)
head = tail = new node(d);
else
{
tail->next = new node(d);
tail = tail->next;
}
}
void xunion(node*head, node*head2, node*&ansHead, node*&ansTail)
{
while (head != 0)
{
insertAtEnd(head->data, ansHead, ansTail);
head = head->next;
}
while (head2 != 0)
{
bool t = false;
node*temp = ansHead;
while (temp != 0)
{
if (head2->data == temp->data)
{
t = true;
}
temp = temp->next;
}
if (t == false)
insertAtEnd(head2->data, ansHead, ansTail);
head2 = head2->next;
}
}

2.

Intersection of Linked-List :
void intersection(node*head, node*head2, node*&ansHead, node*&ansTail)
{
while (head != 0)
{
node*temp = head2;
bool t = false;
while (temp != 0)
{
if (head->data == temp->data)
{
insertAtEnd(head->data, ansHead, ansTail);
break;
}
temp = temp->next;
}
head = head->next;
}
}

3. Merging two sorted Linked-lists :


node*merge(node*a, node*b)
{
if (a == 0)
return b;

if (b == 0)
return a;
node*ansHead;
if (a->data < b->data)
{
ansHead = a;
a->next = merge(a->next, b);
}
else
{
ansHead = b;
b->next = merge(a, b->next);
}
return ansHead;
}

4. Concatenate two linked list :


node*concatenate(node*&first, node*&second)
{
node*temp = first;
while (temp->next != 0)
temp = temp->next;
temp->next = second;
return first;
}

5. Delete duplicates from a Linked list :


void DeleteDuplicate(node*&head)
{
node*temp = head, *nextNode = 0;
while (1)
{
if (temp == 0)
break;
if (temp->next != 0 && temp->data == temp->next->data)
{
nextNode = temp->next->next;
temp->next = nextNode;
}
else
temp = temp->next;
}
}

6.

Check Linked list is Palindrome or not :

void reversebyRE(node*&head, node*&tail)


{
if (head == tail)
return;

node*temp = head->next;
head->next = tail->next;
tail->next = head;
head = temp;
reversebyRE(head, tail);
}
void isPalindromeByRE(node*head, node*tail)
{
node*temp = head;
node*mid = midNode(temp);
node*newHead;
if (mid->data == mid->next->data)
{
newHead = mid->next;
}
else
{
newHead = mid->next;
node*n = new node(mid->data);
n->next = newHead;
newHead = n;
}
mid->next = 0;
reversebyRE(newHead, tail);
bool t = 1;
while (temp != 0 || newHead != 0)
{
if (temp->data != newHead->data)
{
t = 0;
break;
}
temp = temp->next;
newHead = newHead->next;
}
if (t == 0)
cout << "No.\n";
else

cout << "Yes.\n";


}

7. Find Frequency of a String in another string :


void findingFrequency(node*head, node*inputHead)
{
node*tinputHead = inputHead;
int count = 0,sizeofLL=returnSize(inputHead), freq = 0;
while (head != 0)
{
if (tinputHead->data == head->data)
{
count++;
tinputHead = tinputHead->next;
if (count == sizeofLL)
{
freq++;
tinputHead = inputHead;
count = 0;
}
}
head = head->next;
}
cout << "\nAnswer : " << freq << ".\n";
}

You might also like