Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. bool FruitBox::putFruit(Fruit *f) {
  2.  
  3.     if (this->nbFruit >= this->size)
  4.         return false;
  5.  
  6.     if (this->list == NULL) {
  7.         this->list = new FruitNode();
  8.         this->list->fruit = f;
  9.         this->list->next = NULL;
  10.         this->nbFruit++;
  11.         return true;
  12.     }
  13.  
  14.     FruitNode* tmp = this->list;
  15.     FruitNode* elem = new FruitNode();
  16.  
  17.     while (tmp->next != NULL) {
  18.         if (tmp->fruit == f)
  19.             return false;
  20.         tmp = tmp->next;
  21.     }
  22.  
  23.     elem->fruit = f;
  24.     elem->next = NULL;
  25.     tmp->next = elem;
  26.     this->nbFruit++;
  27.     return true;
  28. }
  29.  
  30. Fruit *FruitBox::pickFruit() {
  31.     Fruit *f;
  32.  
  33.     if (this->list) {
  34.         f = this->list->fruit;
  35.         this->list = this->list->next;
  36.     } else f = 0;
  37.     this->nbFruit--;
  38.     return f;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement