Node *next;
// Standart argumentli parametrlangan konstruktor
Node(int val=0) :data(val),next(nullptr){}
// Parametrlash konstruktori
Node(int val, Node *tempNext):data(val),next(tempNext){}
}; class LinkedList
{
// bosh ko’rsatkich Node* head; public:
// standart konstruktor. Bosh ko'rsatgich ishga tushirilmoqda
LinkedList():head(nullptr)
{
} //elementlarni kiritish (ro'yxat boshida) void insert(int val)
{
// yangi node yaratish
Node* new_node = new Node(val);
//Agar ro'yxat bo'sh bo'lsa, yangi node, boshni yarating if (head == nullptr)
{ head = new_node;
}
// Aks holda, new_node bosh va keyingi, oldingi qilib qo'ying
// bosh else
{ new_node->next = head; head = new_node;
}
}
//ro'yxat bo'ylab aylantiring. element topilsa, true qiymatini qaytaring bool search(int val)
{
Node* temp = head; while(temp != nullptr)
{ if (temp->data == val) return true; temp = temp->next;
} return false;
} void remove(int val)
{
Node* temp = head;
// Agar bosh o'chirilishi kerak bo’lsa if (temp != nullptr && temp->data == val)
{ head = temp->next; delete temp; return;
}
// Aks holda, ro'yxatni aylantiring va o'chiriladigan tugunni qidiring else
{
Node* curr = head; while(temp != nullptr && temp->data != val)
{
//Tugun topilsa, tugunni o'chiring va ko'rsatkichlarni o'zgartiring curr = temp;
temp = temp->next;
}
// Agar bog'langan ro'yxatda qiymatlar topilmasa if(!temp)
{ cout << "Value not found" << endl; return;
} curr->next = temp->next; delete temp;
}
}
|
Bir bog’lamli ro’yhat orqali Quick Sort ya’ni teskor saralash algoritimini ko’rib chiqamiz:
#include #include using namespace std;
// bir bog'lamli ro'yhatning class orqali structurasini xosil qilsh class singlyLinkedList
{ public:
int data; singlyLinkedList *next;
};
// bir bog'lamli ro'yhatga element qo'shish
void pushList(singlyLinkedList **linked, int newData)
{ singlyLinkedList *newList = new singlyLinkedList; newList->data = newData; newList->next = *linked;
*linked = newList;
}
// elementlarni chiqarish void showList(singlyLinkedList *linked)
{ if (linked == NULL)
{ cout << "\b \n"; return;
} cout << " " << linked->data << ","; showList(linked->next);
}
// element olish
|
Do'stlaringiz bilan baham: |