Dasturlash 2
2-tajriba ishi
Matyoqubov Suhrob
28. Stek o‘rtasidagi element o‘chirib tashlansin. Agar stek elementi toq bo‘lsa, bitta element, aks holda ikkita element o‘chirilsin.
#include
using namespace std;
int main() {
int n;
cout << "Stack elementlari sonini kiriting:\n";
cin >> n;
stack s, r;
cout << "Stack elementlarini kiriting:\n";
for(int i = 1; i <= n; i ++) {
int x;
cin >> x;
s.push(x);
r.push(x);
}
cout << "Stackni dastlabki holati:\n";
while(r.size()) {
cout << r.top() << '\n';
r.pop();
}
int x = n/2 + 1, y = n/2 + 1;
if(n % 2 == 0)
y --;
int c = 1;
stack t;
while(s.size()) {
if(c == x || c == y) {
}
else
t.push(s.top());
s.pop();
c ++;
}
while(t.size()){
s.push(t.top());
t.pop();
}
cout << "Stackni keyingi holati:\n";
while(s.size()){
cout << s.top() << '\n';
s.pop();
}
}
Do'stlaringiz bilan baham: |