online: 3; azi: 279; total: 50734 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ă în care ordinea de acces este cea în care sunt citite numerele din fişier . Se divizează lista în două liste: una care conţine numere care sunt palindrom şi una care contine numerele care nu sunt palindrom. Se salvează lista cu numere palindrom într-un alt fişier . Dacă nu au existat numere palindrom, în fişier se va scrie un mesaj de informare. În lista care nu conţine numere palindrom se inserează după fiecare număr inversul său.
# include < iostream >
# include < fstream >
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 isPalindrome ( int num) {
int temp = num;
int reversedNum = 0 ;
while ( temp != 0 ) {
reversedNum = reversedNum * 10 + ( temp % 10 );
temp = temp / 10 ;
}
return (num == reversedNum );
}
void splitList ( Node * head , Node ** headPal , Node ** headNotPal ) {
Node * temp = head ;
while ( temp != NULL ) {
if ( isPalindrome ( temp ->data)) {
insert( headPal , temp ->data);
} else {
insert( headNotPal , temp ->data);
}
temp = temp -> next ;
}
}
int reverseNum ( int num) {
int reversedNum = 0 ;
while (num != 0 ) {
reversedNum = reversedNum * 10 + (num % 10 );
num = num / 10 ;
}
return reversedNum ;
}
void reverseList ( Node ** headRef ) {
Node * prev = NULL ;
Node * curr = * headRef ;
Node * next = NULL ;
while ( curr != NULL ) {
next = curr -> next ;
curr -> next = prev ;
prev = curr ;
curr = next ;
}
* headRef = prev ;
}
void printList ( Node * head ) {
Node * temp = head ;
while ( temp != NULL ) {
cout << temp ->data << " " ;
temp = temp -> next ;
}
cout << endl ;
}
void saveListToFile ( Node * head , string fileName ) {
ofstream fout ( fileName ) ;
Node * temp = head ;
while ( temp != NULL ) {
fout << temp ->data << " " ;
temp = temp -> next ;
}
fout.close ();
}
int main () {
ifstream fin ( "input.txt" ) ;
Node * list = NULL ;
int val;
while (fin >> val) {
insert(& list , val);
}
fin.close ();
Node * headPal = NULL ;
Node * headNotPal = NULL ;
splitList ( list , & headPal , & headNotPal );
if ( headPal != NULL ) {
cout << " Palindromic list : " ;
printList ( headPal );
reverseList (& headPal );
saveListToFile ( headPal , "palindromic_list.txt" );
} else {
cout << "No palindromic numbers found " << endl ;
}
if ( headNotPal != NULL ) {
cout << "Non- palindromic list : " ;
printList ( headNotPal );
Node * temp = headNotPal ;
while ( temp != NULL ) {
int reversedNum = reverseNum ( temp ->data);
Node * newNode = createNode ( reversedNum );
Node * nextNode = temp -> next ;
temp -> next = newNode ;
newNode -> next = nextNode ; temp = nextNode ; }
}
return 0 ;
}