Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

//to check whether the given linked list is sorted or not

#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
};
string sorted(node *n);
int main()
{
int n;
cout << "enter the no of elements " << 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;
}
/*
if(sorted(first)==0)
{
cout<<"the given linked list is not sorted "<<endl;
}
else
{
cout<<"the given linked list is sorted "<<endl;
}
*/
cout << sorted(first) << endl;
}
string sorted(node *n)
{
int x = -32768;
while (n)
{
if (n->data < x)
{
return "false";
}
else
{
x = n->data;
n = n->next;
}
}
return "true";
}

//removing the duplicate elements from the sorted 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;
}
}

You might also like