Qobiljonov Anvarjon Navbat bog’langan ro’yxatlar asosida enqueue funksiyasidan foydalanib 5,17,3,5,15,18 qiymatlar kiritilsin. Eng birinchi qiymat ekranga chiqarilsin, dequeue funksiyasidan foydalanib ta qiymat o’chirilsin


Download 233.21 Kb.
Sana24.12.2022
Hajmi233.21 Kb.
#1059493
Bog'liq
Tatu ff tt va kt telekomunikatsiya yo\'nalishi 632-20 guruh talab



640-21
Qobiljonov Anvarjon
Navbat bog’langan ro’yxatlar asosida enqueue() funksiyasidan foydalanib 15,17,3,5,15,18 qiymatlar kiritilsin. Eng birinchi qiymat ekranga chiqarilsin, dequeue() funksiyasidan foydalanib 2 ta qiymat o’chirilsin. O’zgartirilgandagi qolgan qiymatlar peek() va display() dan foydalangan xolda qayta ekranga chiqarilsin?
1.
#include
#include
#define SIZE 5

using namespace std;


class Queue{
private:
int items[SIZE], front, rear;
public:
Queue(){
front=-1;
rear=-1;
}
bool isFull(){
if(front==0&& rear==SIZE -1){
return true;
}
return false;
}
bool isEmpty(){
if (front==-1) return true;
else return false;
}
void enQueue (int element){
if (isFull()){
cout << "Queue to'lgan";
}
else {
if (front == -1) front=0;
rear++;
items[rear]=element;
cout<}
}
int dequeue(){
int element;
if (isEmpty()){
cout <<"Queue to'lgan"<return (-1);
}
else{
element=items[front];
if (front>=rear){
front =-1;
rear=-1;
}
else{
front++;
}
cout << endl<<"olib tashlash"<return(element);
}
}
void display()
{
int i;
if (isEmpty()){
cout << endl<< "Queue Bosh"<< endl;
}
else{
cout << endl << "front" << front;
cout << endl << "elementlar";
for (i=front; i<=rear; i++)
cout << items[i]<< "\t";
cout << endl << "front"<< front;
for (i=front; i<=rear; i++)
cout << endl<< "front"<cout << endl<<"elementlar";
for (i=front; i<=rear; i++)
cout <cout << endl<<"rear"<}
}
};
int main()
{
Queue q;
q.enQueue(15);
q.enQueue(17);
q.enQueue(3);
q.enQueue(5);
q.enQueue(15);
q.enQueue(18);
q.display();
q.dequeue();
q.display();
return 0;
}

2.
Ixtiyoriy nomdagi dek e’lon qilinsin, “5, 25, 18, 33” qiymatlari dekga kiritilsin, pop_back funksiyasi yordamida dekning oxirgi elementi o’chirilsin, natija ekranga chiqarilsin.


#include
#include
using namespace std;
void showdg(deque g)
{
deque :: iterator it;
for (it = g.begin(); it !=g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}

int main()


{
deque gquiz;
gquiz.push_back(5);
gquiz.push_front(25);
gquiz.push_back(18);
gquiz.push_front(33);
cout << "bu dek : ";
showdg(gquiz);
cout << "\ngquiz.size() : " << gquiz.size();
cout << "\ngquiz.max_size : " << gquiz.max_size();
cout << "\ngquiz.at(2) : " << gquiz.at(2);
cout << "\ngquiz.front() : " << gquiz.front();
cout << "\ngquiz.back() : " << gquiz.back();
cout << "\ngquiz.pop_front() : " ;
gquiz.pop_front();
showdg(gquiz);
cout << "\ngquiz.pop_back() : ";
gquiz.pop_back();
showdg(gquiz);
return 0;
}

3.
Binary tree strukturasidan foydalanib ushbu chizmani dasturiy kodini tartibi bo’yicha tuzing va ekranga chiqaring.

#include
#include
using namespace std;
class node{
public: int info;
node *left;
node *right;
};
int intrave(node *tree){
if (tree!=NULL){int a=0, b=0
if (tree->left!=NULL){a=tree->left->info};
if (tree->right!=NULL){a=tree->right->info;}
cout << tree->info<<"--chapida=>"<intrave(tree->left);
intrave(tree->right);}
return 0;
}
node *del(node *tree,int key){
node *p=new node;
node *next=tree;
node *q=NULL;
while (next!=NULL)
{if (next->info==key){cout<<"binar daraxtda" << "key" << "mavjud" << endl;
p=next; break;
}
if (next->info>key){q=next;next=next->left; }
}
if(next==NULL) cout <<"tuzilmada izlangan element yo'q"<node *v==NULL, *t=NULL, *s=NULL;
if(p->left==NULL) v=p->right;
else if(p->right==NULL) v=p->left;
if((p->left!=NULL)&&(p->right!=NULL)){t=p; v=p->right; s=v->left}
while(s!=NULL){
t=v; v=s; s=v->left;
}
if((t!=NULL)&&(t!=p)){
t->left=v->right;
v->right=p->right;
v->left=p->left;
}
if(t==p) v->left=p->left;
if(q==NULL){
cout <info<<"ildiz\n";
tree=v;
delete(p);
return tree;
}
}

int main()


{ int n,key,s; node *tree=NULL, *next=NULL;
cout << "n="; cin>> n;
for(int i=0; inode *p=new node;
node *last=new node;
cin>>s;
p->info=s;
p->left=NULL;
p->right=NULL;
if(i==0){tree=p; next=tree; continue;}
next=tree;
while(1)
{last=next;
if(p->infoinfo)next=next->left;
else next=next->right;
if(next==NULL)break;
}
if(p->infoinfo)last->left=p;
else last->right=p;
}
cout << endl;
intrave (tree);
cout << "delete qilinadigan elementni kiriting \n";
cout << "key="; cin >> key;
tree=del(tree, key);
intrave(tree);
getch();
return 0;
}

4.
Arrayni binary daraxtga implementatsiya qilgan xolda yuqoridagi chizmani dasturiy kodini tartibi bo’yicha tuzing va ekranga chiqaring.

#include
#include
using namespace std;
class node{
public: int info;
node Kleft;
node ?right;
};
int k=6;
int intrave(node *tree){
if (tree!=NULL){int a=1, b=2;
if (tree->1eft!=NULL){ a=tree->1eft->info; }
if (tree->right!=NULL){ b=tree->right->info; }
cout<info<<"--chapida=>"<"<intrave(tree->1eft);
intrave(tree—>right); }
return 0;
}
int height(node *tree){
int h1,h2;
if (tree==NULL) return (-1);
else {
h1 = height(tree—>1eft);
h2 = height(tree—>right);
if (h1>h2) return (1 + h1);
else return (1 + hZ);
}
}
int create_arr(node *tree,int xarr){
if(ltree) return 6;
else{
create_arr(tree->1eft,arr);
arr[k++]=tree->info;
create_arr(tree—>right,arr);
}
}
node *new_tree(int $arr, int start, int end)
{
else 1ast->right=p;}
cout<intrave(tree);
cout<<"\nya'ni\n";
vizua1(tree,6);
int h=height(tree);
cout<<"balandligi="<create_arr(tree,arr);
for(int i=6;itree=new_tree(arr,e,k-l);
vizua1(tree,e);
getch();
}

5.
Binar daraxt tuzilmasidan foydalanib yuqoridagi chizmani dasturiy kodi tuzilsin va ekranga inorder, preorder, postorder tartibida chiqarilsin.

#include
#include
using namespace std;
class node{
public: int info;
node Ileft;
node 3right;
};
int k=0,F1ag=1;
int height(node *tree){
int h1,h2;
if (tree==NULL) return (—1);
else {
h1 = height(tree->1eft);
h2 = height(tree->right);
if (h1>h2) return (1 + hl);
else return (1 + h2);
}
}
void vizua1(node $tree,int l)
{ int i;
if(tree1=NULL) {
vizua1(tree—>right,l+1);
for (i=1; i<=1; i++) cout<<" ";
cout<info<vizua1(tree->1eft,1+1);
} }
int AVLtree (node *tree){
int t;
if (tree!=NULL){
t = height (tree->1eft) - height (tree->right);
if ((t<-1) ]| (t>1)) { Flag = 6; return Flag; }
AVLtree (tree—>1eft); AVLtree (tree—>right);
} }
int GetFlag(){return Flag;}
int main()
{ int n,key,s; node *tree=NULL,3next:NULL;
cout<<"n="; cin>>n; int arr[n];
for(int i=9; inode xp=new node;
node Klast=new node;
cin>>s;
p->info=s;
p->1eft=NULL;
p->right=NULL;
if(i==@){tree=p; next=tree; continue; }
next=tree3
while(1)
{ last=next3
if(p->infoinfo)next=next->left;
else next=next~>right3
if(next==NULL)break; }
if(p->infoinfo)1ast->1eft=p;
else 1ast—>right=p;}
cout<cout<<"\nbinar daraxt:\n";
vizual(tree,@);
AVLtree(tree);
if(GetFlag()) cout<<"ha, muvozanatlangan daraxt";
else cout<<"yo’q, muvozanatlanmagan daraxt";
cout<getch();
}

6.
. Graflarning ro’yxat tuzilmasidan foydalanib yuqoridagi chizmani dasturiy kodi tuzilsin va ekranga chiqarilsin.
#include
#include
#define SIZE 6
int main()
{
int a[SIZE][SIZE];
int d[SIZE];
int v[SIZE];
int temp, minindex, min;
int begin_index = 9;
("chcp 1251");
(”cls");
for (int i = 6; i* {
a[i][i] = 6;
* for (int j = i + 1; j("Masofani kiriting %d - %d: ", i + 1, j + 1);
("%d", atemp);
ali][j] = temp;
aHi] = team
} }

for (int i = 6; i
{
for (int j = 9; j‘ ("%5d ", a[i][j]);
("\n");
}
for (int i = 0; i{
d[i] = 19999;
v[i] = 1;
}
d[begin_index] = 6;
do {
minindex = 16666;
min = 16666;
for (int i = 6; i{
if ((v[i] == 1) 86 (d[i]{
min = d[i];
minindex = i;
} }
if (minindex != 16666)
{
for (int i = 6; i{
if (a[minindex][i] > 6)
{
temp = min + a[minindex][i];
if (temp < d[i])
{
d[i] = temp;
} } }
v[minindex] = 6;
}
} while (minindex < 16666);
("\neng qisqa masofalar: \n");
for (int i = 6; i("%5d ", d[i]);
int ver[SIZE];
int end = 4;
ver[6] = end + 1;
int k = 1;
int weight = d[end];
while (end I: begin_index)
' {
for (int i = 6; iif (a[i][end] != 0)
{
int temp = weight — a[i][end];
if (temp == d[i])
' {
weight = temp;
end = i;
ver[k] = i + 1;
k++;
}}}
("\neng qisqa yol chiqishi\n");
for (int i = k - 1; i >2 6; i——)
("%3d ", ver[i]);
0; ()s
return 6;
}

Download 233.21 Kb.

Do'stlaringiz bilan baham:




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