0% found this document useful (0 votes)
48 views2 pages

Exercice 3

This C++ program defines a Player struct with number, next, and prev pointers to create a doubly linked list. A list struct contains a head pointer and size. Functions are defined to create Player nodes, fill a list from an array, print the list, check if it is empty left, and remove a node by number. The main function calls these to populate a list from an array, remove nodes in a loop based on user input, and check for a single remaining node.

Uploaded by

Yas Sine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

Exercice 3

This C++ program defines a Player struct with number, next, and prev pointers to create a doubly linked list. A list struct contains a head pointer and size. Functions are defined to create Player nodes, fill a list from an array, print the list, check if it is empty left, and remove a node by number. The main function calls these to populate a list from an array, remove nodes in a loop based on user input, and check for a single remaining node.

Uploaded by

Yas Sine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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