#Misol. 4 Navbat sinfi: bog'langan ro'yxatdan foydalanish
#include
using namespace std;
class Queue
{
public:
Queue();
~Queue();
void push(int);
int pop();
void print();
private:
typedef struct Node {
Node *next;
int data;
} NODE;
NODE* head;
};
Queue::Queue()
{
head = NULL;
}
Queue::~Queue()
{
if(head == NULL) return;
NODE *cur = head;
while(cur) {
Node *ptr = cur;
cur = cur->next;
delete ptr;
}
}
void Queue::push(int n)
{
if(head == NULL) {
head = new NODE;
head->data = n;
head->next = NULL;
return;
}
NODE *cur = head;
while(cur) {
if(cur->next == NULL) {
NODE *ptr = new NODE;
ptr->data = n;
ptr->next = NULL;
cur->next = ptr;
return;
}
cur = cur->next;
}
}
void Queue::print()
{
if(head==NULL) return;
Node *cur = head;
while(cur) {
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
int Queue::pop()
{
if(head == NULL) {
cout << " bo'sh estack!" << endl;
return NULL;
}
NODE *tmp = head;
int value = head->data;
if(head->next) {
head = head->next;
}
//oxirgi elementni ochamiz (head)
else {
delete tmp;
head = NULL;
}
cout << "pop: " << value << endl;;
return value;
}
int main()
{
Queue *que = new Queue();
que->push(10);
que->push(20);
que->push(30);
que->push(40);
que->push(50);
que->print();
que->pop();que->print();
que->pop();que->print();
que->pop();que->print();
que->pop();que->print();
que->pop();que->print();
que->pop();que->print();
return 0;
}
Natija:
10 20 30 40 50
pop: 10
20 30 40 50
pop: 20
30 40 50
pop: 30
40 50
pop: 40
50
pop: 50
#Misol. 5
2 ta alohida bog'langan ro'yxat berilgan. Berilgan ikkita bog'langan ro'yxatni birlashtirish uchun funktsiyani hosil qilamiz ketma-ket o’sish tartibda
List1: 10->15->17->20
List2: 5->9->13->19
Natija: 5->9->10->13->15->17->19->20
Dastur kodi
#include
#include
#define SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
using namespace std;
struct node {
int data;
struct node *next;
};
node *createList(int *arr, int n){
node *head, *p;
p = head = new node;
head->data = arr[0];
head->next = NULL;
for (int i = 1; i < n; ++i) {
p->next = new node;
p = p->next;
p->data = arr[i];
p->next = NULL;
}
return head;
}
void displayList(node *head){
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
node *mergeSortedLists(node *head1, node *head2){
node *result = NULL;
if (head1 == NULL) {
return head2;
}
if (head2 == NULL) {
return head1;
}
if (head1->data < head2->data) {
result = head1;
result->next = mergeSortedLists(head1->next, head2);
} else {
result = head2;
result->next = mergeSortedLists(head1, head2->next);
}
return result;
}
int main(){
int arr1[] = {10, 15, 17, 20};
int arr2[] = {5, 9, 13, 19};
node *head1, *head2, *result = NULL;
head1 = createList(arr1, SIZE(arr1));
head2 = createList(arr2, SIZE(arr1));
cout << " Birinchi tartiblangan ro'yxat:" << endl;
displayList(head1);
cout << " Ikkinchi tartiblangan ro'yxat:" << endl;
displayList(head2);
result = mergeSortedLists(head1, head2);
cout << " Yakuniy tartiblangan ro'yxat:" << endl;
displayList(result);
return 0;
}
Yuqoridagi dasturni kompilyatsiya qilganingizda va bajarganingizda. U quyidagi natijani hosil qiladi
Birinchi tartiblangan ro'yxat:
10 15 17 20
Ikkinchi tartiblangan ro'yxat:
5 9 13 19
Yakuniy tartiblangan ro'yxat:
5 9 10 13 15 17 19 20
Misol 6. Berilgan ikkita bog'langan ro'yxatdan har bir Node da kattaroq element bilan yangi bog'langan ro'yxat yaratish
Bir xil o'lchamdagi ikkita bog'langan ro'yxatni hisobga olgan holda, ushbu bog'langan ro'yxatlar yordamida yangi bog'langan ro'yxatni yaratish vazifasi qo'yilgan. Shart shundaki, ikkala bog'langan ro'yxat orasidagi kattaroq elementlar yangi ro'yxatiga qo'shiladi.
// Yangi bog'langan ro'yxatni yaratish uchun C ++ dasturi
// berilgan ikkita bog'langan ro'yxatdan
// bilan bir xil o'lchamdagi
// har bir Node dagi ikkitasi orasidagi katta element
#include
using namespace std;
// Node ning ifodalanishistruct Node {
int data;
Node* next;
};
// Bog'langan ro'yxatga nodeni kiritish funktsiyasi
void insert(Node** root, int item)
{
Node *ptr, *temp;
temp = new Node;
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
ptr = *root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
// Yangi bog'langan ro'yxatni qaytaradigan funktsiya
Node* newList(Node* root1, Node* root2)
{
Node *ptr1 = root1, *ptr2 = root2, *ptr;
Node *root = NULL, *temp;
while (ptr1 != NULL) {
temp = new Node;
temp->next = NULL;
// Katta nodeni taqqoslaymiz
if (ptr1->data < ptr2->data)
temp->data = ptr2->data;
else
temp->data = ptr1->data;
if (root == NULL)
root = temp;
else {
ptr = root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return root;
}
void display(Node* root)
{
while (root != NULL) {
cout << root->data << "->";
root = root->next;
}
cout << endl;
}
// Driver code
int main()
{
Node *root1 = NULL, *root2 = NULL, *root = NULL;
// Birinchi bog'langan ro'yxat
insert(&root1, 5);
insert(&root1, 2);
insert(&root1, 3);
insert(&root1, 8);
cout << " Birinchi ro'yxat:";
display(root1);
// Ikkinchi bog'langan ro'yxat
insert(&root2, 1);
insert(&root2, 7);
insert(&root2, 4);
insert(&root2, 5);
cout << " Ikkinchi ro'yxat:";
display(root2);
root = newList(root1, root2);
cout << " Yangi ro'yxat:";
display(root);
return 0;
}
Natija:
Kirish: list1 = 5-> 2-> 3-> 8
list2 = 1-> 7-> 4-> 5
Chiqish: Yangi ro'yxat = 5-> 7-> 4-> 8
Do'stlaringiz bilan baham: |