10-Topshiriq C++ tilida navbatni statik, ya’ni bir o
Download 36.7 Kb.
|
1 2
Bog'liq10-topshoriq javobi MTA
1-misol:
Dastur kodi #include using namespace std; int a[10],R=0,n;//bu yerda n navbatga kiritilishi kerak bo'lgan elementlar soni. int kiritish(int s) { a[R]=s; R++; } int chiqarish(){ int t=a[0]; for(int i=0;i } bool isEmpty() { if(R==0) return true; else return false; } bool isFull(){ if(R>=10)return true;else return false; } int print(){ int i=0; while(i { int k=chiqarish();i++; cout< kiritish(k);} } int main(){ int n,s; cout<<‘n=‘;cin>>n; for(int i=0;i if(!isFull()){cin>>s; kiritish(s);} else{cout<<‘navbat to'ldi’; n=i;break;} } cout<<‘\nnavbat elementlari: ‘; print(); for(int i=0;i } cout<<‘\nnatijaviy navbat elementlari: ‘; print(); system(‘PAUSE’); } Dasturning bajarilishi natijasi: n=5 6 7 9 8 11 navbat elementlari: 6 7 9 8 11 natijaviy navbat elementlari: 7 9 11 2-misol: #include #include using namespace std; struct queue{ int storage[100000]; int start; int finish; queue(){ start = 0; finish = 0; } void push(int n){ storage[finish] = n; finish++; } int pop(){ int a = storage[start]; start++; return a; } int front(){ return storage[start]; } int size(){ return finish - start; } string clear(){ finish = 0; start = 0; return ‘ok’; } string exit(){ return ‘bye’; } }; int main() { string a; queue x; while(cin >> a){ if(a == ‘push’){ int n; cin >> n; x.push; cout << ‘ok’ << endl; } if(a == ‘pop’){ cout << x.pop() << endl; } if(a == ‘front’){ cout << x.front() << endl; } if(a == ‘size’){ cout << x.size() << endl; } if(a == ‘clear’){ cout << x.clear() << endl; } if(a == ‘exit’){ cout << x.exit() << endl; return 0; } } return 0; } Download 36.7 Kb. Do'stlaringiz bilan baham: |
1 2
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling