Question

C++ write a program that calls a function to check if a link list is in...

C++

write a program that calls a function to check if a link list is in ascending order without using recursion. the function returns true or false.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

struct node {
    int data;
    node *next;
};

bool is_sorted(node *head) {
    while (head != NULL && head->next != NULL) {
        if (head->data > head->next->data) {
            return false;
        }
        head = head->next;
    }
    return true;
}

int main() {
    node *head = new node;
    head->data = 5;
    head->next = new node;
    head->next->data = 7;
    head->next->next = new node;
    head->next->next->data = 8;
    head->next->next->next = NULL;

    cout << "is sorted? " << is_sorted(head) << endl;


    head = new node;
    head->data = 5;
    head->next = new node;
    head->next->data = 1;
    head->next->next = new node;
    head->next->next->data = 7;
    head->next->next->next = NULL;

    cout << "is sorted? " << is_sorted(head) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ write a program that calls a function to check if a link list is in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT