Fanidan ish topshirig’i Bajardi : Tekshirdi : #include class Node { public: int data; Node* next; Node(int value) : data


Download 18.03 Kb.
Sana12.11.2023
Hajmi18.03 Kb.
#1768875
Bog'liq
714-22


O’ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALAR UNIVERSITETI


fanidan
ish topshirig’i

Bajardi :


Tekshirdi :.

#include

class Node {
public:
int data;
Node* next;

Node(int value) : data(value), next(nullptr) {}
};

class LinkedList {
private:
Node* head;

public:
LinkedList() : head(nullptr) {}

// Insert a node in descending order
void insert(int value) {
Node* newNode = new Node(value);

if (!head || value > head->data) {
newNode->next = head;
head = newNode;
} else {
Node* current = head;
while (current->next && value < current->next->data) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}

// Display the linked list
void display() {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};

int main() {
LinkedList myList;

// Insert elements in descending order
myList.insert(5);
myList.insert(3);
myList.insert(8);
myList.insert(1);

// Display the linked list
std::cout << "Linked List in Descending Order: ";
myList.display();

return 0;
}
Download 18.03 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling