You are on page 1of 7

#include "pch.

h"
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int val)
{
data = val;
next = NULL;
}
};

class LinkList
{
public:
Node *head;
LinkList()
{
head = NULL;
}
void insertatbeg(int val)
{
Node *tmp;
tmp = new Node(val);
if (head == NULL)
{
head = tmp;
}
else
{
tmp->next = head;
head = tmp;
}
}
void insertatend(int val)
{
Node *tmp = new Node(val);
if (head == NULL)
{
head = tmp;
}
else
{
Node *ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = tmp;
}
}
void Display()
{
if (head == NULL)
{
cout << "\nThe list is empty";
}
else
{
cout << endl;
Node *ptr;
ptr = head;
while (ptr != NULL)
{
cout << ptr->data << " ";
ptr = ptr->next;
}
}
cout << endl;
}
void Nthelement(int val)
{
int count = 1;
Node *ptr1, *ptr2;
ptr1 = ptr2 = head;
while (ptr1->next != NULL)
{
count++;
ptr1 = ptr1->next;
}
if (val > 0 && val <= count)
{
ptr1 = head;
for (int i = 0; i < val-1; i++)
{
ptr2 = ptr2->next;
}
cout << "\nThe value at " << val << " node is: " << ptr2->data <<
endl;
}
else
{
cout << "Invalid Input";
}
}
void appnode(int val)
{
int count = 1;
Node *ptr1, *ptr2;
ptr1 = ptr2 = head;
while (ptr1->next != NULL)
{
count++;
ptr1 = ptr1->next;
}
count = count - val;
for (int i = 0; i < count - 1; i++)
{
ptr2 = ptr2->next;
}
ptr1->next = head;
head = ptr2->next;
ptr2->next = NULL;
}
void reverse()
{
Node *ptr = head;
Node *head2 = NULL;
while (ptr != NULL)
{
Node *temp = new Node(ptr->data);
if (head2 == NULL)
{
head2 = temp;
}
else
{
temp->next = head2;
head2 = temp;
}
ptr = ptr->next;
}
cout << endl;
while (head2 != NULL)
{
cout << head2->data << " ";
head2 = head2->next;
}
}
void swap()
{
Node *ptr1, *ptr2, *ptr3;
ptr1 = ptr2 = ptr3 = head;
ptr1 = head->next;
head->next = ptr1->next;
ptr1->next = NULL;
ptr1->next = head;
head = ptr1;

ptr1 = ptr2 = ptr3 = head->next;


ptr1 = ptr1->next; ptr1 = ptr1->next; ptr2 = ptr2->next;
while (ptr1 != NULL)
{
ptr2->next = ptr1->next;
ptr1->next = ptr2;
ptr3->next = ptr1;

ptr3 = ptr1 = ptr2;


if (ptr1 != NULL)
{
ptr1 = ptr1->next;
}
if (ptr1 != NULL)
{
ptr1 = ptr1->next;
}

if (ptr2->next != NULL)
{
ptr2 = ptr2->next;
}
}
}
void sort()
{
int num = 0;
int count = 1;
Node *ptr1 = head;
while (ptr1->next != NULL)
{
count++;
ptr1 = ptr1->next;
}
int *ptr = new int[count];
ptr1 = head;
for (int i = 0; i < count; i++)
{
ptr[i] = ptr1->data;
ptr1 = ptr1->next;
}
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
if (ptr[i] < ptr[j])
{
num = ptr[i];
ptr[i] = ptr[j];
ptr[j] = num;
}
}
}
Node *head2 = NULL;
for (int i = 0; i < count; i++)
{
Node *tem = new Node(ptr[i]);
if (head2 == NULL)
{
head2 = tem;
}
else
{
Node *ptr;
ptr = head2;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = tem;
}
}
head = head2;
}
void Remove_Duplicates()
{
sort();
Node *ptr1, *ptr2;
ptr2 = head;
ptr1 = head->next;
while (ptr1 != NULL)
{
if (ptr2->data == ptr1->data)
{
ptr2->next = ptr1->next;
ptr1->next = NULL;
ptr1 = head;
ptr1 = ptr2->next;
}
ptr2 = ptr1;
if (ptr1 != NULL)
{
ptr1 = ptr1->next;
}
}
}
Node* Gethead()
{
return head;
}
int nodcount()
{
int count = 1;
Node *ptr1 = head;
while (ptr1->next != NULL)
{
count++;
ptr1 = ptr1->next;
}
return count;
}
};
LinkList Merge(LinkList l3, LinkList l4)
{
Node *ptr1, *ptr2;
ptr1 = l3.Gethead();
ptr2 = l4.Gethead();
int count = l3.nodcount() + l4.nodcount();
int *ptr = new int[count];
for (int i = 0; i < count; i++)
{
if (ptr1 != NULL)
{
ptr[i] = ptr1->data;
ptr1 = ptr1->next;
}
else
{
ptr[i] = ptr2->data;
ptr2 = ptr2->next;
}
}
LinkList l;
for (int i = 0; i < count; i++)
{
l.insertatend(ptr[i]);
}
l.sort();
return l;
}
LinkList Union(LinkList l3, LinkList l4)
{
LinkList l;
l = Merge(l3, l4);
l.Remove_Duplicates();
return l;
}
LinkList Intersection(LinkList l3, LinkList l4)
{
Node *ptr1, *ptr2;
ptr1 = l3.Gethead();
ptr2 = l4.Gethead();
int *ptr3 = new int[l3.nodcount()];
int *ptr4 = new int[l4.nodcount()];
int count;
if (l3.nodcount() >= l4.nodcount())
{
count = l3.nodcount();
};
if (l3.nodcount() <= l4.nodcount())
{
count = l4.nodcount();
}
for (int i = 0; i < count; i++)
{
ptr3[i] = ptr1->data;
if (i < l4.nodcount())
{
ptr4[i] = ptr2->data;
}
ptr1 = ptr1->next;
if (i < l4.nodcount())
{
ptr2 = ptr2->next;
}
}
LinkList l;
for (int i = 0; i < l3.nodcount(); i++)
{
for (int j = 0; j < l4.nodcount(); j++)
{
if (ptr3[i] == ptr4[j])
{
l.insertatend(ptr3[i]);
}
}
}
return l;
}

int main()
{
LinkList l, l2, l1;
l.insertatend(1);
l.insertatend(2);
l.insertatend(3);
l.insertatend(4);
l.insertatend(5);
l.insertatend(6);
l.insertatend(7);
l.insertatend(8);
l2.insertatend(2);
l2.insertatend(4);
l2.insertatend(6);
l2.insertatend(8);
//l1 = Intersection(l, l2);
//l1 = Union(l, l2);
//l1 = Merge(l, l2);
//l.insertatend(80);
//l.insertatend(90);
l.Display();
l.swap();
//l.sort();
//l.Remove_Duplicates();

l.Display();
l2.Display();
//l1.Display();
//l.Nthelement(5);
//l.reverse();
}

You might also like