123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- /*
- * File: object.c
- * Author: Arthur Brandao
- *
- * Created on 7 décembre 2018
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "object.h"
- /* --- Fonctions publiques --- */
- void object_ini(Object* this){
- this->first = NULL;
- this->last = NULL;
- this->size = 0;
- }
- obj_node* object_add(Object* this, int type, int x, int y){
- obj_node* objn;
- //Ajout des vthiseurs
- objn = malloc(sizeof(obj_node));
- objn->type = type;
- objn->x = x;
- objn->y = y;
- //Lien entre les noeuds
- objn->next = NULL;
- if (this->first == NULL) {
- objn->prev = NULL;
- this->first = objn;
- this->last = objn;
- } else {
- objn->prev = this->last;
- this->last->next = objn;
- this->last = objn;
- }
- this->size++;
- return objn;
- }
- obj_node* object_search(Object* this, int type, int x, int y){
- //Si liste vide
- if(this->size == 0){
- return NULL;
- }
- //Recherche
- obj_node* objn = this->first;
- while(objn != NULL){
- if(objn->type == type && objn->x == x && objn->y == y){
- return objn;
- }
- objn = objn->next;
- }
- return NULL;
- }
- void object_delete(Object* this, obj_node* objn){
- //Liste vide
- if(this->first == NULL){
- return;
- }
- //Si 1er et seul
- if (objn->prev == NULL && objn->next == NULL) {
- this->first = NULL;
- this->last = NULL;
- }
- //Si 1er et non seul
- else if (objn->prev == NULL && objn->next != NULL) {
- objn->next->prev = NULL;
- this->first = objn->next;
- }
- //Si dernier
- else if (objn->next == NULL) {
- objn->prev->next = NULL;
- this->last = objn->prev;
- }
- //Sinon si il est au milieu
- else {
- objn->prev->next = objn->next;
- objn->next->prev = objn->prev;
- }
- //Free
- free(objn);
- this->size--;
- }
- void object_clean(Object* this){
- //Liste vide
- if(this->first == NULL){
- return;
- }
- //Vide liste
- obj_node* tmp, * objn = this->first;
- while (objn != NULL) {
- tmp = objn->next;
- object_delete(this, objn);
- objn = tmp;
- }
- this->first = NULL;
- this->last = NULL;
- this->size = 0;
- }
|