0% found this document useful (0 votes)
47 views

To Check Whether The Given Linked List Is Sorted or Not

The document contains code to check if a linked list is sorted and remove duplicate elements from a sorted linked list. It defines a node class with data and next pointer. It takes array elements as input, creates a linked list and passes it to functions to check if it is sorted and remove duplicates. The sorted() function traverses the list and returns true if nodes are in ascending order, false otherwise. The deletedupli() function traverses the list and deletes any node that has a duplicate data value in the next node.

Uploaded by

Pate Vishnu
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)
47 views

To Check Whether The Given Linked List Is Sorted or Not

The document contains code to check if a linked list is sorted and remove duplicate elements from a sorted linked list. It defines a node class with data and next pointer. It takes array elements as input, creates a linked list and passes it to functions to check if it is sorted and remove duplicates. The sorted() function traverses the list and returns true if nodes are in ascending order, false otherwise. The deletedupli() function traverses the list and deletes any node that has a duplicate data value in the next node.

Uploaded by

Pate Vishnu
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/ 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