online: 6; azi: 244; total: 50699 Manual clasa a xi a - Implementarea structurilor de date - Lista liniara

Probleme Rezolvate



Manual clasa a Xi a

Implementarea structurilor de date

Lista liniara

Se elimină din stiva numerele pare.
# include < iostream >
# include < fstream >
using namespace std ;
const int MAX_SIZE = 100 ;
struct Stack {
int top;
int arr [MAX_SIZE];
};
void init ( Stack & s) {
s.top = -1 ;
}
bool is_empty ( Stack & s) {
return s.top == -1 ;
}
bool is_full ( Stack & s) {
return s.top == MAX_SIZE - 1 ;
}
void push ( Stack & s, int value ) {
if ( is_full (s)) {
cout << " Error : Stack overflow " << endl ;
return ;
}
s.top ++;
s.arr [ s.top ] = value ;
}
int pop ( Stack & s) {
if ( is_empty (s)) {
cout << " Error : Stack underflow " << endl ;
return -1 ;
}
int value = s.arr [ s.top ];
s.top --;
return value ;
}
int peek ( Stack & s) {
if ( is_empty (s)) {
cout << " Error : Stack is empty " << endl ;
return -1 ;
}
return s.arr [ s.top ];
}
void remove_even_numbers ( Stack & s) {
Stack temp ;
init ( temp );
// Extragem numerele impare si le punem in stiva temporara
while (! is_empty (s)) {
if ( peek (s) % 2 != 0 ) {
push ( temp , peek (s));
}
pop (s);
}
// Inversam stiva temporara si o punem inapoi in stiva initiala
while (! is_empty ( temp )) {
push (s, peek ( temp ));
pop ( temp );
}
}
int main () {
Stack s;
init (s);
int num;
// Citim stiva dintr-un fisier text
ifstream in ( "input.txt" ) ;
while (in >> num) {
push (s, num);
}
in. close ();
// Eliminam numerele pare din stiva
remove_even_numbers (s);
// Afisam stiva fara numerele pare
while (! is_empty (s)) {
cout << peek (s) << " " ;
pop (s);
}
cout << endl ;
return 0 ;
}
# include < iostream >
# include < fstream >
# include < stack >
using namespace std ;
void remove_even_numbers ( stack < int >& s) {
stack < int > temp ;
// Extragem numerele impare si le punem in stiva temporara
while (! s. empty ()) {
if ( s. top () % 2 != 0 ) {
temp. push ( s. top ());
}
s. pop ();
}
// Inversam stiva temporara si o punem inapoi in stiva initiala
while (! temp. empty ()) {
s. push ( temp. top ());
temp. pop ();
}
}
int main () {
stack < int > s;
int num;
// Citim stiva dintr-un fisier text
ifstream in ( "input.txt" ) ;
while (in >> num) {
s. push (num);
}
in. close ();
// Eliminam numerele pare din stiva
remove_even_numbers (s);
// Afisam stiva fara numerele pare
while (! s. empty ()) {
cout << s. top () << " " ;
s. pop ();
}
cout << endl ;
return 0 ;
}