Professional Documents
Culture Documents
To Check Whether The Given Linked List Is Sorted or Not
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";
}