online: 2; azi: 341; total: 50796 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 compară două stive fără a pierde conținutul lor în urma extragerii de noduri.
# include < iostream >
# include < fstream >
using namespace std ;
const int MAX_SIZE = 100 ;
void push ( int arr [], int & top, int value , int size ) {
if (top >= size - 1 ) {
cout << "Stiva este plina!" << endl ;
} else {
top++;
arr [top] = value ;
}
}
int pop ( int arr [], int & top) {
int value = -1 ;
if (top < 0 ) {
cout << "Stiva este goala!" << endl ;
} else {
value = arr [top];
top--;
}
return value ;
}
bool is_empty ( int top) {
return top < 0 ;
}
bool is_full ( int top, int size ) {
return top >= size - 1 ;
}
bool compare_stacks ( int s1[], int s2[], int & top1, int & top2) {
if (top1 != top2) {
return false ;
}
int temp1[MAX_SIZE], temp2[MAX_SIZE];
int temp_top1 = -1 , temp_top2 = -1 ;
bool result = true ;
// Comparam elementele din stive, extragand nodurile si verificand daca sunt egale
while (! is_empty (top1)) {
int value1 = pop(s1, top1);
int value2 = pop(s2, top2);
push (temp1, temp_top1, value1, MAX_SIZE);
push (temp2, temp_top2, value2, MAX_SIZE);
if (value1 != value2) {
result = false ;
}
}
// Restauram stivele initiale
while (! is_empty (temp_top1)) {
int value1 = pop(temp1, temp_top1);
int value2 = pop(temp2, temp_top2);
push (s1, top1, value1, MAX_SIZE);
push (s2, top2, value2, MAX_SIZE);
}
return result ;
}
int main () {
int s1[MAX_SIZE], s2[MAX_SIZE];
int top1 = -1 , top2 = -1 ;
int num;
// Citim stiva s1 dintr-un fisier text
ifstream in ( "input.txt" ) ;
while (in >> num) {
push (s1, top1, num, MAX_SIZE);
}
in.close ();
// Citim stiva s2 dintr-un fisier text
ifstream in2 ( "input2.txt" ) ;
while (in2 >> num) {
push (s2, top2, num, MAX_SIZE);
}
in2.close();
// Comparam stivele
if ( compare_stacks (s1, s2, top1, top2)) {
cout << "Stivele sunt egale!" << endl ;
} else {
cout << "Stivele nu sunt egale!" << endl ;
}
return 0 ;
}
# include < iostream >
# include < fstream >
# include < stack >
using namespace std ;
bool compare_stacks ( stack < int >& s1, stack < int >& s2) {
if (s1.size() != s2.size()) {
return false ;
}
stack < int > temp1, temp2;
bool result = true ;
// Comparam elementele din stive, extragand nodurile si verificand daca sunt egale
while (!s1.empty()) {
temp1.push(s1.top());
temp2.push(s2.top());
if (s1.top() != s2.top()) {
result = false ;
}
s1.pop();
s2.pop();
}
// Restauram stivele initiale
while (!temp1.empty()) {
s1.push(temp1.top());
s2.push(temp2.top());
temp1.pop();
temp2.pop();
}
return result ;
}
int main () {
stack < int > s1, s2;
int num;
// Citim stiva s1 dintr-un fisier text
ifstream in ( "input.txt" ) ;
while (in >> num) {
s1.push(num);
}
in.close ();
// Citim stiva s2 dintr-un fisier text
ifstream in2 ( "input2.txt" ) ;
while (in2 >> num) {
s2.push(num);
}
in2.close();
// Comparam stivele
if ( compare_stacks (s1, s2)) {
cout << "Stivele sunt egale!" << endl ;
} else {
cout << "Stivele nu sunt egale!" << endl ;
}
R eturn
}