online: 2; azi: 182; total: 52188 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 creează o listă numai cu numerele prime din fişier . Se afişează cel mai mare număr prim şi cel mai mic număr prim. Se verifică dacă lista conține numai numere distincte şi se afişează un mesaj de informare. Dacă lista nu conţine numai numere distincte, se elimină numere din listă astfel încât să conţină numai numere distincte. Se salvează lista creată într-un alt fişier . Dacă nu au existat numere prime în fişier , se va scrie un mesaj de informare. (indicație. Se va crea o listă ordonată crescător şi se vor afişa numerele prime din primul nod şi din ultimul nod).
# include < iostream >
# include < fstream >
# include < cmath >
using namespace std ;
struct Node {
int data;
Node * next ;
};
Node * createNode ( int val) {
Node * newNode = new Node ();
newNode ->data = val;
newNode -> next = NULL ;
return newNode ;
}
void insert ( Node ** headRef , int val) {
Node * newNode = createNode (val);
if (* headRef == NULL ) {
* headRef = newNode ;
} else {
Node * temp = * headRef ;
while ( temp -> next != NULL ) {
temp = temp -> next ;
}
temp -> next = newNode ;
}
}
bool isPrime ( int num) {
if (num < 2 ) {
return false ;
}
for ( int i = 2 ; i <= sqrt (num); i++) {
if (num % i == 0 ) {
return false ;
}
}
return true ;
}
bool contains ( Node * head , int val) {
Node * temp = head ;
while ( temp != NULL ) {
if ( temp ->data == val) {
return true ;
}
temp = temp -> next ;
}
return false ;
}
void processList ( Node ** headRef ) {
Node * temp = * headRef ;
Node * prev = NULL ;
Node * distinctList = NULL ;
while ( temp != NULL ) {
if ( contains ( distinctList , temp ->data)) {
if ( prev == NULL ) {
* headRef = temp -> next ;
} else {
prev -> next = temp -> next ;
}
Node * toDelete = temp ;
temp = temp -> next ;
delete toDelete ;
} else {
insert(& distinctList , temp ->data);
prev = temp ;
temp = temp -> next ;
}
}
* headRef = distinctList ;
}
void printList ( Node * head ) {
Node * temp = head ;
while ( temp != NULL ) {
cout << temp ->data << " " ;
temp = temp -> next ;
}
cout << endl ;
}
int main () {
ifstream fin ( "input.txt" ) ;
Node * list = NULL ;
int val;
while (fin >> val) {
if ( isPrime (val)) {
insert(& list , val);
}
}
fin.close ();
cout << " List of prime numbers : " ;
printList ( list );
Node * temp = list ;
int maxPrime = INT_MIN;
int minPrime = INT_MAX;
while ( temp != NULL ) {
if ( temp ->data > maxPrime ) {
maxPrime = temp ->data;
}
if ( temp ->data < minPrime ) {
minPrime = temp ->data;
}
temp = temp -> next ;
}
if ( list == NULL ) {
cout << " There are no prime numbers in the file." << endl ;
} else {
cout << "Maximum prime number : " << maxPrime << endl ;
cout << "Minimum prime number : " << minPrime << endl ;
}
processList (& list );
if ( list == NULL ) {
cout << " There are no distinct prime numbers in the file." << endl ;
} else {
cout << "Distinct prime numbers : " ;
printList ( list );
ofstream fout ( "output.txt" ) ;
temp = list ;
while ( temp != NULL ) { fout << temp ->data << " ";
temp = temp -> next ;
}
fout.close ();
}
return 0;
}