You are on page 1of 23

//removing the duplicate elements from the linked list

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void display(node *n);

void deletedupli(node *n);

int main()

int n;

cout<<"enter the value of n "<<endl;

cin>>n;

int a[n];

cout<<"enter the array elements "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first;

node *last;

node *temp;

first=new node();

first->data=a[0];

first->next=NULL;

last=first;

for(int i=1;i<n;i++)

{
temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"before deleting the duplicate numbers linked list is "<<endl;

display(first);

deletedupli(first);

cout<<"after deleting the duplicate numbers linked list is "<<endl;

display(first);

void display(node *n)

while(n)

cout<<n->data<<" ";

n=n->next;

cout<<endl;

void deletedupli(node *n)

node *p;

p=n->next;

while(p)

if(n->data==p->data)

n->next=p->next;

delete p;
p=n->next;

else

n=n->next;

p=p->next;

//reversing the given linked list


/*

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void display(node *n);

int main()

int n;

cout<<"enter the vlaue of n "<<endl;

cin>>n;

int a[n];

cout<<"enter the elements of the linked list "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

}
node *first;

node *temp;

node *last;

first=new node();

first->data=a[0];

first->next=NULL;

last=first;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"before reversing the linked list "<<endl;

display(first);

node *p;

p=first;

int i=0;

int b[n];

while(p)

b[i]=p->data;

p=p->next;

i++;

p=first;

i--;

while(p)

{
p->data=b[i--];

p=p->next;

cout<<"after reversing the linked list "<<endl;

display(first);

void display(node *n)

while(n)

cout<<n->data<<" ";

n=n->next;

cout<<endl;

*/

//reversing the linked list using sliding pointers


#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void display(node *n);

int main()

int n;

cout<<"enter the value of n "<<endl;

cin>>n;
int a[n];

cout<<"enter the elements of the linked list "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first;

node *temp;

node *last;

first=new node();

first->data=a[0];

first->next=NULL;

last=first;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"before revesing the linked list "<<endl;

display(first);

node *r=NULL;

node *q=NULL;

while(first)

r=q;

q=first;

first=first->next;

q->next=r;
}

first=q;

cout<<"after reversing the linked list "<<endl;

display(first);

void display(node *n)

while(n)

cout<<n->data<<" ";

n=n->next;

cout<<endl;

//concatenation of two linked lists

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void display(node *n);

int main()

int n;

cout<<"enter the no of nodes of the first linked list "<<endl;

cin>>n;

int a[n];
cout<<"enter the data of the first linked list "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first1;

node *temp;

node *last;

first1=new node();

first1->data=a[0];

first1->next=NULL;

last=first1;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"display of first linked list "<<endl;

display(first1);

int m;

cout<<"enter the no of nodes of second linked list "<<endl;

cin>>m;

int b[m];

cout<<"enter the data of the second linked list "<<endl;

for(int i=0;i<m;i++)

cin>>b[i];

}
node* first2;

first2=new node();

first2->data=b[0];

first2->next=NULL;

last=first2;

for(int i=1;i<m;i++)

temp=new node();

temp->data=b[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"display of second linked list "<<endl;

display(first2);

node *p;

node *q;

p=first1;

while(p)

q=p;

p=p->next;

q->next=first2;

cout<<"display of first and second linked list "<<endl;

display(first1);

void display(node *n)

while(n)
{

cout<<n->data<<" ";

n=n->next;

cout<<endl;

//merging of two sorted linked lists


#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void display(node *n);

int main()

int n;

cout<<"enter the no of node in the first linked list "<<endl;

cin>>n;

int a[n];

cout<<"enter the data in sorted form "<<endl;

cout<<"enter the data of the first linked list "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first;

node *last;

node *temp;
first=new node();

first->data=a[0];

first->next=NULL;

last=first;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"display of first linked list"<<endl;

display(first);

int m;

cout<<"enter no of nodes of the second linked list "<<endl;

cin>>m;

int b[m];

cout<<"enter the data of the second linked list "<<endl;

for(int i=0;i<m;i++)

cin>>b[i];

node *second;

second=new node();

second->data=b[0];

second->next=NULL;

last=second;

for(int i=1;i<m;i++)

temp=new node();
temp->data=b[i];

temp->next=NULL;

last->next=temp;

last=temp;

cout<<"display of second linked list"<<endl;

display(second);

node *third;

if(first->data<second->data)

third=last=first;

first=first->next;

last->next=NULL;

else

third=last=second;

second=second->next;

last->next=NULL;

while(first && second)

if(first->data<second->data)

last->next=first;

last=first;

first=first->next;

last->next=NULL;

else

{
last->next=second;

last=second;

second=second->next;

last->next=NULL;

if(first)

last->next=first;

else

last->next=second;

cout<<"display of merge linked list "<<endl;

display(third);

void display(node *n)

while(n)

cout<<n->data<<" ";

n=n->next;

cout<<endl;

//linked list is loop or linear


#include<iostream>

using namespace std;

class node

{
public:

int data;

node *next;

};

void display(node *n);

string isloop(node *n);

int main()

int n;

cout<<"enter the no of nodes in the linked list "<<endl;

cin>>n;

int a[n];

cout<<"enter the data of the linked list "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first;

node *temp;

node *last;

first=new node();

first->data=a[0];

first->next=NULL;

last=first;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;
}

display(first);

last->next=first->next->next;

cout<<isloop(first)<<endl;

// display(first); loop will run for infinite times

void display(node *n)

while(n)

cout<<n->data<<" ";

n=n->next;

cout<<endl;

string isloop(node *n)

node *p,*q;

p=q=n;

do

p=p->next;

q=q->next;

if(q!=NULL)

q=q->next;

else

}
while(p && q && p!=q);

if(p==q)

return "true";

else

return "false";

//display of circular linked list

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void display(node *n);

int main()

int n;

cout<<"enter the no of nodes "<<endl;

cin>>n;

int a[n];

cout<<"enter the data of the linked list "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];
}

node *head;

node *temp;

node *last;

head=new node();

head=new node();

head->data=a[0];

head->next=NULL;

last=head;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

last->next=head;

display(head);

void display(node *n)

node *p=n;

do

cout<<p->data<<" ";

p=p->next;

while(p!=head);

cout<<endl;

}
// inserting new node into the existing node in the circular linked list
#include <iostream>

using namespace std;

class node

public:

int data;

node *next;

};

void insert(node *n, int pos, int x);

void display(node *n);

int main()

int n;

cout << "enter the no of nodes " << endl;

cin >> n;

int a[n];

cout << "enter the data of the nodes " << endl;

for (int i = 0; i < n; i++)

cin >> a[i];

node *first;

node *last;

node *temp;

first = new node();

first->data = a[0];

first->next = NULL;

last = first;

for (int i = 1; i < n; i++)

{
temp = new node();

temp->data = a[i];

temp->next = NULL;

last->next = temp;

last = temp;

last->next = first;

cout << "display of circular linked list before inserting the new node " << endl;

display(first);

int pos, x;

cout << "enter the position where you want insert new node " << endl;

cin >> pos;

cout << "enter the data of the new node " << endl;

cin >> x;

insert(first, pos, x);

cout << "display of the circular linked list after inserting the new node " << endl;

display(first);

void display(node *n)

node *p;

p = n;

do

cout << p->data << " ";

p = p->next;

} while (p != n);

cout << endl;

void insert(node *n, int pos, int x)

{
node *t;

t = new node();

t->data = x;

if (pos == 0)

t->next = n;

n = t;

else

for (int i = 1; i < pos; i++)

n = n->next;

t->next = n->next;

n->next = t;

//deleting the node from the circular linked list

#include<iostream>

using namespace std;

class node

public:

int data;

node *next;

};

node* deletefirst(node *n);

void deletenode(node *n,int pos);

void display(node *n);


int main()

int n;

cout<<"enter the no of nodes "<<endl;

cin>>n;

int a[n];

cout<<"enter the data of the nodes "<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

node *first;

node *temp;

node *last;

first=new node();

first->data=a[0];

first->next=NULL;

last=first;

for(int i=1;i<n;i++)

temp=new node();

temp->data=a[i];

temp->next=NULL;

last->next=temp;

last=temp;

last->next=first;

display(first);

int pos;

cout<<"enter the position of the node which you want to delete "<<endl;

cin>>pos;
if(pos==1)

first=deletefirst(first);

last->next=first;

else

deletenode(first,pos);

display(first);

void display(node *n)

node *p;

p=n;

do

cout<<p->data<<" ";

p=p->next;

while(p!=n);

cout<<endl;

void deletenode(node *n,int pos)

node *p;

for(int i=0;i<pos-1;i++)

p=n;

n=n->next;

}
p->next=n->next;

delete n;

node* deletefirst(node *n)

node *p;

p=n;

n=n->next;

delete p;

return n;

You might also like