O‘ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA
KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI
TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
Malumotlar tuzulmasi
va
algoritmlash
fanida
Amaliy ish -3
Bajardi: Jamoldinov Ravshanbek
Tekshirdi: Axmadjonov islomjon
AMALIYOTY ISHI-2
Mavzu: QIDIRISH VA SARALASH ALGORITMLARI
VARIANT-7
7.Ro’yhat yarating, Ro’yhatga N ta haqiqiy son kiriting. Ro’yhatning maksimum elementini ro’yhat oxiriga qo’shish dasturini tuzing.
DASTUR KODI
#include
using namespace std;
class Node {
public:
int data;
Node* link;
Node(int n)
{
this->data = n;
this->link = NULL;
}
};
class Stack {
Node* top;
public:
Stack() { top = NULL; }
void push(int data)
{
Node* temp = new Node(data);
if (!temp) {
cout << "\nStack Overflow";
exit(1);
}
temp->data = data;
temp->link = top;
top = temp;
}
bool isEmpty()
{
return top == NULL;
}
int peek()
{
if (!isEmpty())
return top->data;
else
exit(1);
}
void pop()
{
Node* temp;
if (top == NULL) {
cout << "\nStack Underflow" << endl;
exit(1);
}
else {
temp = top;
top = top->link;
free(temp);
}
}
void display()
{
Node* temp;
if (top == NULL) {
cout << "\nStack Underflow";
exit(1);
}
else {
temp = top;
while (temp != NULL) {
cout << temp->data;
temp = temp->link;
if (temp != NULL)
cout << " -> ";
}
}
}
};
int main()
{
Stack s,stemp;
int n;
cout<<"N=";
cin>>n;
int max;
cin>>max;
s.push(max);
for (int i = 1; i < n; i++) {
int temp;
cin>>temp;
s.push(temp);
if (temp>max){
max=temp;
}
}
int counter = 0;
while (!s.isEmpty()){
int t=s.peek();
s.pop();
if (t==max){
counter++;
}
else{
stemp.push(t);
}
}
for (int i = 0; i < counter; i++) {
s.push(max);
}
while(!stemp.isEmpty()){
int t = stemp.peek();
stemp.pop();
s.push(t);
}
s.display();
return 0;
}
NATIJA
Do'stlaringiz bilan baham: |