Vous êtes sur la page 1sur 4

#include <iostream>

#define dataType int


using namespace std;

struct Node{
Node(dataType value){
info = value;
next = NULL;
}
Node(){}
dataType info;
Node* next = NULL;
};

struct List{
Node *root;
int length = 0;

void insert_end(dataType info){ ///Exercitiul 1


if(length == 0){
root = new Node(info);
}
else{
Node* last = new Node();
last = root;
while(last->next != NULL){
last = last->next;
}
last->next = new Node(info);
}
length++;
}

void insert_begin(dataType info){ ///Exercitiul 2


if(length == 0){
root = new Node(info);
}else{
Node* aux = new Node(info);
aux->next = root;
root = aux;
}
length++;
}

void insertInfoBeforeValue(dataType info, dataType value){ ///Exercitiul 3


if(root->info == value){
insert_begin(info);
}else{
Node* prev = root;
Node* aux = root->next;
while(aux->next != NULL){
if(aux->info == value){
Node* temp = new Node(info);
temp->next = aux;
prev->next = temp;
length++;
}
prev = aux;
aux = aux->next;
}
}
}

void insertAfterEvenSumDiditsX2(){ ///Exercitiul 4


Node* aux = root;
while(aux != NULL){
if(aux->info % 2 == 0){
int sumDigits = 0, copy_info = aux->info;
while(copy_info){
sumDigits += copy_info%10;
copy_info /= 10;
}
Node* temp = new Node(sumDigits*2);
temp->next = aux->next;
aux->next = temp;
aux = temp;
length++;
}
aux = aux->next;
}
}

void insertAfterFirstValueInfo(dataType info, dataType value){


///Exercitiul 5
Node* aux = root;
while(aux->next != NULL){
if(aux->info == value){
Node* temp = new Node(info);
temp->next = aux->next;
aux->next = temp;
length++;
return;
}
aux = aux->next;
}
}

void print(){ ///Exercitiul 8


Node* aux = root;
while(aux != NULL){
cout << aux->info << " ";
aux = aux->next;
}
cout << '\n';
}

void printSumOddBetweenEven(){ ///Exercitiul 9


dataType sum = 0;
Node* aux = root;
while(aux->next->next != NULL){
if(aux->info % 2 == 0 && aux->next->info % 2 == 1 && aux->next->next-
>info % 2 == 0){
sum += aux->next->info;
}
aux = aux->next;
}
cout << "Suma elementelor impare aflate intre elemente pare este : " << sum
<< '\n';
}

void isPalindrome(){ /// Exercitiul 11


Node* left = root;
for(int i = 0; i < length/2; ++i){

Node* right = root;


for(int j = 0; j < length - i - 1; ++j){
right = right->next;
}
if(left->info != right->info){
cout << "Nu e palindrom!\n";
return;
}
left = left->next;
}
cout << "SIeul este palindrom!\n";
}

void deleteFirstElement(){ /// Exercitiul 12


Node* aux= root;
root = root->next;
delete(aux);
length--;
}
void deliteFirstElemAfterInfo(dataType info){ /// Exercitiul 13
Node* aux = root;
while(aux->next != NULL){
if(aux->next->info == info){
Node* temp = aux->next;
aux->next = aux->next->next;
delete(temp);
length--;
return;
}
aux = aux->next;
}
}

}list_;

int main(){
list_.insert_end(44);
list_.insert_end(3);
list_.insert_end(1);
list_.insert_end(5);
list_.insert_end(8);
list_.insertInfoBeforeValue(0, 3);
list_.print();
list_.insertAfterEvenSumDiditsX2();
list_.print();
list_.insertAfterFirstValueInfo(99, 0);
list_.print();
list_.printSumOddBetweenEven();
list_.isPalindrome();
list_.deleteFirstElement();
list_.print();
list_.deliteFirstElemAfterInfo(1);
list_.print();
return 0;
}