// File Guardaroba.cpp
#include <iostream.h>
#include "Guardaroba.h"

Guardaroba::Guardaroba()
{
  tokenCorrente = 0;
  primo = NULL;
}

Guardaroba::~Guardaroba()
{ distruggi(primo); }

Guardaroba::Guardaroba(const Guardaroba& g)
{
  tokenCorrente = g.tokenCorrente;
  primo = copia(g.primo);
}

Guardaroba& Guardaroba::operator=(const Guardaroba& g)
{
  if (this != &g) {
    distruggi(primo);
    tokenCorrente = g.tokenCorrente;
    primo = copia(g.primo);
  }
  return *this;
}

int Guardaroba::TokenCorrente()
{ return tokenCorrente; }


int Guardaroba::AggiungiOggetto(Oggetto& o)
{
  rec* aux = new rec;
  aux->token = tokenCorrente;
  aux->info = &o;
  aux->next = primo;
  primo = aux;
  tokenCorrente++;
  return primo->token;
}


bool Guardaroba::EstInGuardaroba(int t)
{
  rec* l = primo;
  while (l != NULL) {
    if (l->token == t) return true;
    l = l->next;
  }
  return false;
}


Oggetto* Guardaroba::RestituisciOggetto(int t)
{
  if (primo == NULL) return NULL;
  Oggetto* ris = NULL;
  if (primo->token == t) {
    ris = primo->info;
    rec* aux = primo;
    primo = primo->next;
    delete aux;
  }
  else {
    rec* l = primo;
    while (l->next != NULL) {
      if (l->next->token == t) {
        ris = l->next->info;
        rec* aux = l->next;
        l->next == l->next->next;
        delete aux;
      }
      l = l->next;
    }
  }
  return ris;
}

rec* Guardaroba::copia(rec* l)
{
  if (l == NULL) return NULL;
  else {
    rec* ris = new rec;
    ris->token = l->token;
    ris->info = l->info;
    ris->next = copia(l->next);
    return ris;
  }
}

void Guardaroba::distruggi(rec* l)
{
  while (l != NULL) {
    rec* aux = l;
    l = l->next;
    delete aux;
  }
}

