online: 5; azi: 201; total: 50656 Manual clasa a xi a - Implementarea structurilor de date - Lista liniara

Probleme Rezolvate



Manual clasa a Xi a

Implementarea structurilor de date

Lista liniara

Într o stivă sunt memorate numere din intervalul [1,10], ordonate crescător, iar într-o altă stivă, numere din intervalul [20,30], ordonate crescător. Să se concateneze cele două şiruri de numere, păstrând ordonarea crescătoare. (Indicație. Se rästoarnă a doua stivă într-o a treia stivă, se răstoarnă şi prima stivă în a treia, peste numerele din prima stivă, şi se extrag numerele din a treia stivă.)
#include < iostream >
#include < fstream >
using namespace std ;
struct Node {
int data;
Node * next ;
};
void push ( Node ** top_ref , int new_data ) {
Node * new_node = new Node ;
new_node ->data = new_data ;
new_node -> next = (* top_ref );
(* top_ref ) = new_node ;
}
int pop ( Node ** top_ref ) {
if (* top_ref == NULL) {
return INT_MIN;
}
int result = (* top_ref )->data;
Node * temp = * top_ref ;
(* top_ref ) = (* top_ref )-> next ;
delete ( temp );
return result ;
}
int top ( Node * top_ref ) {
if ( top_ref == NULL) {
return INT_MIN;
}
return top_ref ->data;
}
bool is_empty ( Node * top_ref ) {
return top_ref == NULL;
}
void print_stack ( Node * top_ref ) {
while (! is_empty ( top_ref )) {
cout << top ( top_ref ) << " ";
pop (& top_ref );
}
cout << endl ;
}
int main () {
Node * s1 = NULL;
Node * s2 = NULL;
Node * s3 = NULL;
int num;
// Citim stiva s1 dintr-un fisier text
ifstream in ("input.txt");
while (in >> num) {
push (&s1, num);
}
in .close ();
// Citim stiva s2 dintr-un fisier text
ifstream in2 ("input2.txt");
while (in2 >> num) {
push (&s2, num);
}
in2 .close ();
// Inversam stiva s2 in stiva s3
while (! is_empty (s2)) {
push (&s3, top(s2));
pop (&s2);
}
// Inversam stiva s1 in stiva s3, concatenand elementele din s1 si s2
while (! is_empty (s1)) {
while (! is_empty (s3) && top (s1) < top (s3)) {
push (&s2, top(s3));
pop (&s3);
}
push (&s3, top(s1));
pop (&s1);
}
// Concatenam stiva s3 cu stiva s2
while (! is_empty (s2)) {
push (&s3, top(s2));
pop (&s2);
}
// Afisam stiva concatenata
print_stack (s3);
return 0 ;
}
# include < iostream >
# include < fstream >
# include < stack >
using namespace std ;
int main () {
stack < int > s1, s2, s3;
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();
// Inversam stiva s2 in stiva s3
while (!s2.empty()) {
s3.push(s2.top());
s2.pop();
}
// Inversam stiva s1 in stiva s3, concatenand elementele din s1 si s2
while (!s1.empty()) {
while (!s3.empty() && s1.top() < s3.top()) {
s2.push(s3.top());
s3.pop();
}
s3.push(s1.top());
s1.pop();
}
// Concatenam stiva s3 cu stiva s2
while (!s2.empty()) {
s3.push(s2.top());
s2.pop();
}
// Afisam stiva concatenata
while (!s3.empty()) {
cout << s3.top() << " " ;
s3.pop();
}
cout << endl ;
return 0 ;
}