Exercice 3

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <iostream>

using namespace std;

struct Player{
int Num;
Player* next;
Player* prev;
};
struct list{
Player* head;
int numberList;;
};

Player* CreatePlayer(int Value){


Player* p = new Player;
if (!p){
cout << "Problem Allocation !" << endl;
exit(1);
}
p->Num = Value;
p->next = NULL;
p->prev = NULL;
}
list* remplir_tab(int tab[],int size){
list* li = new list;
Player* tempPrev = NULL;
if (li == NULL){
cout << "Problem Allocation !" << endl;
exit(1);
}
li->numberList= size ;
li->head = CreatePlayer(tab[0]);
Player* temp = li->head;
for (int i=1 ; i<size ; i++,temp=temp->next){
temp->next = CreatePlayer(tab[i]);
temp->next->prev = temp;
}
temp->next = li->head;
li->head->prev = temp;
return li;
}
void Affichage(list* List){
if (List->head == NULL){
cout << "Liste Vide !" << endl;
return ;
}
cout << "Affichage A droite : " << endl;
Player* temp = List->head;
int HeadValue = List->head->Num;
while (temp != NULL){
cout << "->" << temp->Num ;
temp = temp->next;
}
cout << endl;
}
bool is_left(list* List){
return List->head->next == NULL ;
}
void eliminer(list* List,int P){
if (List->head == List->head->next){
List->head->next = NULL;
cout << " Gagnant Est : " << List->head->Num << endl;
return ;
}
Player* temp = List->head;
int indice = 1;
while (indice != P){
temp = temp->next;
indice++;
}
if (temp == List->head){

List->head->prev->next = List->head->next;

List->head->next->prev = List->head->prev;

List->head = List->head->next;
cout << "Le Nombre " << temp->Num << " Est Supprimee de la Liste "
<< endl;
return ;
} else {
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
List->head = temp->next;
temp->next = NULL;
temp->prev = NULL;
cout << "Le Nombre " << temp->Num << " Est Supprimee de la Liste "
<< endl;
return ;
}

int main(){
int tab[] = {1,2,3,4,5};
int Nmbpas;

list* li = remplir_tab(tab,sizeof(tab)/sizeof(int));
cout << "Entrer Nombre de Pas : ";
cin >> Nmbpas ;
while (!is_left(li)){
eliminer(li,Nmbpas);
}

You might also like