समस्या विवरण
लिंक्ड लिस्ट में लगातार जोड़ियां क्रमबद्ध हैं या नहीं जांचें। विषम संख्या में तत्व होने पर अंतिम तत्व को नजरअंदाज करें।
उदाहरण: {8, 10, 18, 20, 5, 15} → जोड़ियां: (8,10), (18,20), (5,15) → सभी क्रमबद्ध → YES
उदाहरण कोड
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void append(Node** start, int key) {
Node* new_node = new Node;
new_node->data = key;
new_node->next = (*start);
(*start) = new_node;
}
bool isPairwiseSorted(Node* start) {
Node* temp = start;
while (temp != NULL && temp->next != NULL) {
if (temp->data > temp->next->data)
return false;
temp = temp->next->next;
}
return true;
}
int main() {
Node* start = NULL;
int arr[] = {8, 10, 18, 20, 5, 15};
int n = sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < n; i++)
append(&start, arr[i]);
if (isPairwiseSorted(start))
cout << "This is pairwise sorted";
else
cout << "This is not pairwise sorted";
return 0;
}
आउटपुट
This is pairwise sorted