Ma’lumotlar tuzilmasi va algoritmi F. I. Sh. Matyakubov Odamboy Guruh: 912-21 Amaliyot


Download 56.36 Kb.
Sana23.01.2023
Hajmi56.36 Kb.
#1112339
Bog'liq
MTA 9


Ma’lumotlar tuzilmasi va algoritmi
F.I.SH._Matyakubov Odamboy
Guruh: _______912-21______________________________
Amaliyot: __9__X_______
Misol: ______X_________
Dasturlash tili: _____java___________X__________________
Navbatnoi o’rtadagi elementini o’chirish
package com.company.Navbatlar;

import java.util.LinkedList;


import java.util.Queue;

public class Navbat {


static Queue q;

static void remove(int t) {


Queue ref = new LinkedList<>();
int s = q.size();
int cnt = 0;
while (!q.isEmpty() && q.peek() != t) {
ref.add(q.peek());
q.remove();
cnt++;
}
if (q.isEmpty()) {
System.out.print("element not found!!" + "\n");
while (!ref.isEmpty()) {
q.add(ref.peek());
ref.remove();
}
}
else {
q.remove();
while (!ref.isEmpty()) {
q.add(ref.peek());
ref.remove();
}
int k = s - cnt - 1;
while (k-- > 0) {
int p = q.peek();
q.remove();
q.add(p);
}
}
}
static void print() {
Queue qr = new LinkedList<>(q);
while (!qr.isEmpty()) {
System.out.print(qr.peek() + " ");
qr.remove();
}

System.out.println();


}

public static void main(String[] args)


{
q = new LinkedList<>();
q.add(10);
q.add(20);
q.add(30);
q.add(40);
q.add(50);
q.add(60);
print();
remove(30);
print();
}
}


Navbatni bir xil elementini chiqarish
package com.company.Navbatlar;

public class Navbat12 {


public static int firstElement(int[] arr, int n, int k) {
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count == k) {
return arr[i];
}
}

return -1;


}
public static void main(String[] args)
{
int[] arr = { 1, 7, 4, 3, 4, 8, 7 };
int n = arr.length;
int k = 2;
System.out.print(firstElement(arr, n, k));
}
}

Navbatni max elementini toppish
package com.company.Navbatlar;

import java.util.Collections;


import java.util.PriorityQueue;

public class NavbatMax {


public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();
queue.add(10);
queue.add(15);
queue.add(30);
queue.add(20);
queue.add(5);
System.out.println("Dastlabki PriorityQueue: " + queue);
queue.remove(Collections.max(queue));
System.out.println("o'chirilgandan keyingi PriorityQueue "
+ "element: " + queue);
}
}

Navbatni min elementini toppish


package com.company.Navbatlar;
import java.util.Collections;
import java.util.PriorityQueue;

public class Navbatmin {


public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();
queue.add(10);
queue.add(15);
queue.add(30);
queue.add(20);
queue.add(5);
System.out.println("Dastlabki PriorityQueue: " + queue);
queue.remove(Collections.min(queue));
System.out.println("o'chirilgandan keyingi PriorityQueue "
+ "element: " + queue);
}
}

Download 56.36 Kb.

Do'stlaringiz bilan baham:




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