Created
June 23, 2025 18:03
-
-
Save Mirsalim3635/a35331b293ee3d1523b397fcbb54753b to your computer and use it in GitHub Desktop.
c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| template <typename T> | |
| class Node { | |
| public: | |
| T data; | |
| Node<T>* next; | |
| Node(T value) { | |
| data = value; | |
| next = nullptr; | |
| } | |
| }; | |
| template <typename T> | |
| class LinkedList { | |
| private: | |
| Node<T>* head; | |
| public: | |
| LinkedList() { | |
| head = nullptr; | |
| } | |
| void append(T value) { | |
| Node<T>* newNode = new Node<T>(value); | |
| if (head == nullptr) { | |
| head = newNode; | |
| } else { | |
| Node<T>* CopyValue = head; | |
| while (CopyValue->next != nullptr) { | |
| CopyValue = CopyValue->next; | |
| } | |
| CopyValue->next = newNode; | |
| } | |
| } | |
| void insert(int index, T value) { | |
| Node<T>* newNode = new Node<T>(value); | |
| if (index == 0) { | |
| newNode->next = head; | |
| head = newNode; | |
| return; | |
| } | |
| Node<T>* temp = head; | |
| for (int i = 0; i < index - 1 && temp != nullptr; i++) { | |
| temp = temp->next; | |
| } | |
| if (temp == nullptr) return; | |
| newNode->next = temp->next; | |
| temp->next = newNode; | |
| } | |
| T first() { | |
| if (head != nullptr) | |
| return head->data; | |
| throw out_of_range("Bo'sh ro'yxat"); | |
| } | |
| T last() { | |
| if (head == nullptr) | |
| throw out_of_range("Bo'sh royxat"); | |
| Node<T>* temp = head; | |
| while (temp->next != nullptr) { | |
| temp = temp->next; | |
| } | |
| return temp->data; | |
| } | |
| void remove(T value, bool all = false) { | |
| while (head != nullptr && head->data == value) { | |
| Node<T>* del = head; | |
| head = head->next; | |
| delete del; | |
| if (!all) return; | |
| } | |
| Node<T>* temp = head; | |
| while (temp != nullptr && temp->next != nullptr) { | |
| if (temp->next->data == value) { | |
| Node<T>* del = temp->next; | |
| temp->next = temp->next->next; | |
| delete del; | |
| if (!all) return; | |
| } else { | |
| temp = temp->next; | |
| } | |
| } | |
| } | |
| void clear() { | |
| while (head != nullptr) { | |
| Node<T>* del = head; | |
| head = head->next; | |
| delete del; | |
| } | |
| } | |
| void print() { | |
| Node<T>* temp = head; | |
| while (temp != nullptr) { | |
| cout << temp->data << " -> "; | |
| temp = temp->next; | |
| } | |
| cout << "NULL" << endl; | |
| } | |
| void print(int index) { | |
| Node<T>* temp = head; | |
| int i = 0; | |
| while (temp != nullptr) { | |
| if (i == index) { | |
| cout << "Index " << index << ": " << temp->data << endl; | |
| return; | |
| } | |
| temp = temp->next; | |
| i++; | |
| } | |
| cout << "Index topilmadi" << endl; | |
| } | |
| ~LinkedList() { | |
| clear(); | |
| } | |
| }; | |
| int main() { | |
| LinkedList<string> list; | |
| list.append("salom"); | |
| list.append("1"); | |
| list.append("1.25"); | |
| list.append("c"); | |
| list.insert(2, "true"); | |
| list.print(); | |
| cout << list.first() << endl; | |
| cout << list.last() << endl; | |
| list.remove("aziz"); | |
| list.print(); | |
| list.append("hayr"); | |
| list.remove("salom", true); | |
| list.print(); | |
| list.print(0); | |
| list.clear(); | |
| list.print(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment