#include <stdio.
h>
#include <stdlib.h>
#include <string.h>
//1) =====================================
typedef struct produit{
char label [50];
int Qnt;
double PrixU;
}produit;
//2)======================================
//a)
/*
typedef struct maillon{
produit pro;
struct maillon *next;
}maillon;
//b)
typedef struct stock{
maillon *head;
maillon *queue;
}ListStockS;
*/
//3)=================================
//a)
typedef struct maillonD{
produit pro;
struct maillonD *next;
struct maillonD *prev;
}maillonD;
//b)
typedef struct ListStockD{
maillonD *first;
maillonD *last;
int taille;
}stock;
//c)
stock *creer_stock(){
stock *pm=malloc(sizeof(stock));
if(pm==NULL)
return NULL;
pm->first=NULL;
pm->last=NULL;
pm->taille=0;
return pm;
}
maillonD* creer_maillon(produit prod){
maillonD *m=malloc(sizeof(maillonD));
if(m==NULL)
return NULL;
m->pro=prod;
m->next=NULL;
m->prev=NULL;
return m;
stock *ajouter(stock *st,produit pro,int Qnt,double prixU){
maillonD *m=creer_maillon(pro);
maillonD *temp=st->first;
if(st->first==NULL){
m->[Link]=Qnt;
m->[Link]=prixU;
st->first=st->last=m;
st->taille++;
return st;
}
else
while(temp!=NULL){
if(temp->[Link]==m->[Link]){
temp->[Link]=(temp->[Link])+(m->[Link]);
temp->[Link]=m->[Link];
free(m);
return st;
}
temp=temp->next;
}
m->[Link]=Qnt;
m->[Link]=prixU;
m->next=st->first;
m->prev=NULL;
st->first->prev=m;
st->first=m;
st->taille++;
return st;
stock* supprimer(stock *st,produit pro){
if(st->first==NULL)
return NULL;
maillonD *temp=st->first;
//delet first
if(st->first->[Link]==[Link]){
st->first=st->first->next;
st->first->prev=NULL;
free(temp);
st->taille--;
return st;
while(temp!=NULL){
if(temp->[Link]==[Link]){
break;
temp=temp->next;
//delete last
if(temp->next==NULL){
st->last=st->last->prev;
st->last->next=NULL;
free(temp);
st->taille--;
return st;
//delete middle
else{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
st->taille--;
return st;
}
return NULL;
}
//existe
stock* Existe(stock *st,produit pro){
if(st->first==NULL)
return NULL;
maillonD *temp=st->first;
while(temp!=NULL){
if(temp->[Link]==[Link])
return st;
temp=temp->next;
}
return NULL;
//vendre
stock* vendre(stock *st,produit pro,int Nb_unites){
if(st->first==NULL)
return NULL;
maillonD *temp=st->first;
if(Existe(st,pro)==NULL){
printf("le produit n'existe pas dans le stock !");
}
else{
while(temp!=NULL){
if(temp->[Link]==[Link])
break;
temp=temp->next;
temp->[Link]=(temp->[Link])-Nb_unites;
if(temp->[Link]==0)
st=supprimer(st,pro);
return st;
}
}
double PrixStock(stock *st){
if(st->first==NULL)
return 0;
double k=0;
maillonD *temp=st->first;
while(temp!=NULL){
k=k+temp->[Link];
temp=temp->next;
}
return k;
void affichage(stock *st){
if(st->first==NULL)
printf("le stock est vide !");
maillonD *temp=st->first;
while(temp!=NULL){
printf("|%s| |%d| |%.0lf|->",temp->[Link],temp->[Link],temp-
>[Link]);
temp=temp->next;
//fonction menu des choix
int menu (stock *st){
int choix;
menu :
printf("\n\n");
printf("1. acheter produit\n");
printf("2. supprimer un produit\n");
printf("3. chercher si un livre existe ou non\n");
printf("4. vendre un livre\n");
printf("5. calculer le prix totale du stock\n");
printf("6. Quitter\n");
printf("\n---->Faites votre choix : ");
scanf("%d",&choix);
while(choix<1 && choix>6){
goto menu;
}
return choix;
}
// programme principale
int main() {
stock *st=creer_stock();
produit pro;
int choix=0;
int Qnt=0;
double PrixU=0;
int Nb_unites=0;
printf("%%%%%%%%%%%% Liste Doublement chainee : %%%%%%%%%%%%\n");
printf("inserer le label:");
scanf("%s",&[Link]);
printf("inserer la Qnt:");
scanf("%d",&Qnt);
printf("inserer le prix:");
scanf("%lf",&PrixU);
ajouter(st,pro,Qnt,PrixU);
affichage(st);
while(choix != 6){
choix=menu (st);
switch(choix){
case 1:
printf("inserer le label:");
scanf("%s",&[Link]);
printf("inserer la Qnt:");
scanf("%d",&Qnt);
printf("inserer le prix:");
scanf("%lf",&PrixU);
st=ajouter(st,pro,Qnt,PrixU);
affichage(st);
break;
case 2:
printf("inserer le nom du label:");
scanf("%s",&[Link]);
st=supprimer(st,pro);
if(st==NULL)
printf("le produit n'existe pas pour l'a
supprimer !");
affichage(st);
break;
case 3:
printf("inserer le label:");
scanf("%s",&[Link]);
printf("inserer la Qnt:");
scanf("%d",&Qnt);
printf("inserer le prix:");
scanf("%lf",&PrixU);
st=Existe(st,pro);
if(st==NULL)
printf("le produit n'existe pas !");
else
printf("le produit est trouver !");
affichage(st);
break;
case 4:
printf("inserer le label:");
scanf("%s",&[Link]);
printf("inserer la Qnt:");
scanf("%d",&Qnt);
printf("inserer le prix:");
scanf("%lf",&PrixU);
printf("entrer le nombre unites a vendre :");
scanf("%d",&Nb_unites);
st=vendre(st,pro,Nb_unites);
affichage(st);
break;
case 5:
printf(" le prix total du stock est :
%lf",PrixStock(st));
break;
}
}
return 0;
}