Insert an object in a simple linked list

1

I have to do a project in c ++, where I create figures (in a graphic environment) and these figures have to be inserted in a linked list.

At the time of seeing the coordinates of the figures that are inserted in the list, it shows me only the coordinate of the last inserted figure I do not understand how to correct that, I have time to look at examples of the course and internet documentation, I do not know if they are inserting badly or it is at the time of printing.

This is the code where I create the figure and insert the figure to the list: .h

 class Circulo
    {
        private:
            int x,y,r1,r2;
            Figuras *fig=new Figuras();//reservo espacio de memoria para figura
            Lista_Figuras *ini=NULL;
        public:
            Circulo();
            ~Circulo();
            void crear(int,int,int,int,int);

    };

void Circulo::crear(int x,int y,int r1,int r2,int cls)
{
    setcolor(0);
    setfillstyle(1,cls);
    fillellipse(x,y,r1,r2);

    fig->setX(x);
    fig->setY(y);
    fig->setX1(r1);
    fig->setY1(r2);
    fig->setCls(cls);
    fig->setT(1);
    ini=ini->insertar(ini,fig);
    cout<<"Ini: "<<ini->getFigura()->getX()<<endl;
    ini->mostrarLista(ini);

}

This is the Figures class:

class Figuras
{
    private:
        int x,y,x1,y1,x2,y2,cls,t;
    public:
        Figuras();
        ~Figuras();
        void setX(int);
        void setY(int);
        void setX1(int);
        void setY1(int);
        void setX2(int);
        void setY2(int);
        void setCls(int);
        void setT(int);
        int getX();
        int gety();
        int getX1();
        int getY1();
        int getX2();
        int getY2();
        int getCls();
        int getT();

};

Figuras::Figuras()
{
    //ctor
    x=0;
    y=0;
    x1=0;
    y1=0;
    x2=0;
    y2=0;
    cls=0;
    t=0;
}

Figuras::~Figuras()
{
    //dtor
}
void Figuras::setX(int x)
{
    this->x=x;
}
void Figuras::setY(int y)
{
    this->y=y;
}
void Figuras::setX1(int x1)
{
    this->x1=x1;
}
void Figuras::setY1(int y1)
{
    this->y1=y1;
}
void Figuras::setX2(int x2)
{
    this->x2=x2;
}
void Figuras::setY2(int y2)
{
    this->y2=y2;
}
void Figuras::setCls(int cls)
{
    this->cls=cls;
}
void Figuras::setT(int t)
{
    this->t=t;
}
int Figuras::getX()
{
    return x;
}
int Figuras::gety()
{
    return y;
}
int Figuras::getX1()
{
    return x1;
}
int Figuras::getY1()
{
    return y1;
}
int Figuras::getX2()
{
    return x2;
}
int Figuras::getY2()
{
    return y2;
}
int Figuras::getCls()
{
    return cls;
}
int Figuras::getT()
{
    return t;
}

This is my "List" class:

class Lista_Figuras
{
    private:
        Figuras *fig;
        Lista_Figuras *sig;
    public:
        Lista_Figuras();
        ~Lista_Figuras();
        void setFigura(Figuras*);
        void setSig(Lista_Figuras*);
        Figuras *getFigura();
        Lista_Figuras *getSig();
        Lista_Figuras *insertar(Lista_Figuras*,Figuras*);
        void mostrarLista(Lista_Figuras*);

};
 void Lista_Figuras::setFigura(Figuras *f)
{
    fig=f;
}
void Lista_Figuras::setSig(Lista_Figuras *s)
{
    sig=s;
}
Figuras *Lista_Figuras::getFigura()
{
    return fig;
}
Lista_Figuras *Lista_Figuras:: getSig()
{
    return sig;
}
Lista_Figuras *Lista_Figuras::insertar(Lista_Figuras *ini ,Figuras *fig)
{
    Lista_Figuras *N_Figura=new Lista_Figuras();
    Lista_Figuras *aux;
    N_Figura->setFigura(fig);
    if (ini==NULL)
    {
        ini=N_Figura;
        N_Figura->setSig(NULL);
        cout<<"Se inserto, estaba vacio\n"<<ini<<endl;
    }
    else
    {
        aux=ini;
        N_Figura->setSig(aux);
        ini=N_Figura;

    }
    return ini;
}
void Lista_Figuras::mostrarLista(Lista_Figuras *ini)
{
    Lista_Figuras *aux=ini;
    Figuras *aux1;
    while(aux!=NULL)//recorre la lista asta que sea Null
    {
        aux1=aux->getFigura();
        cout<<aux->getFigura()->getX()<<" ";//muestra el numero actual de la lista conforme se valla moviendo
        if(aux->getSig()!=NULL)//si el siguiente es diferente de null dibueja un Flecha
        {
            cout<<"->";
        }
        else
        {
            cout<<endl;
        }
        aux=aux->getSig();//aunmenta de uno en uno la lista
    }
    if(ini==NULL){
        cout<<"Lista Basida"<<endl;
    }

}

This image shows the error better:

On the right side is where I print the list, ini->mostrarLista (ini);

    
asked by jaron cascante Pérez 30.01.2018 в 06:53
source

1 answer

1

Based on the fact that the code you are showing is, as indicated in the comments, incomplete, I see two basic problems:

Problem 1

Lista_Figuras is intended to be a list and a node of the list at the same time. This creates a dark and complex interface ... is it really necessary to add a node to the list?

ini=ini->insertar(ini,fig);

Where ini appears nothing more and nothing less than three times ?? The homologue in the STL would be that, to add an element to a vector you had to do something like this:

std::vector<int> datos;
datos = datos.push_back(datos,1);

In a well-constructed list, the line that I have commented on should look more like this:

ini->insertar(fig);

In this new line anyone with a minimum of experience programming understands that ini is some kind of container in which an element is added, fig .

Another example of the complexity of the interface is found in the implementation of insertar :

if (ini==NULL)
{
  ini=N_Figura; // <<--- AQUI!!!
  N_Figura->setSig(NULL);
  cout<<"Se inserto, estaba vacio\n"<<ini<<endl;
}
else
{
  aux=ini;
  N_Figura->setSig(aux);
  ini=N_Figura; // <<--- AQUI!!!
}

The problem here is that ini is a local variable, then the commented lines are local changes. The original pointer does not know about this change. It is tempting to think, when you pass a pointer to a function, that the pointer will be updated with the new information and in this case it is not, then if someone gives to do this:

ini->insertar(ini,fig);

You will have a difficult problem to find. In addition to allowing other uses the less worthy of a book of dark arts. How would the program look after such an instruction?

ini = ini->insertar(otra_lista,fig);

The solution to this design problem is to divide Lista_Figuras into two objects (assuming it is necessary): container and node. The container is limited to managing the list of nodes and each node ... because that, is a node and stores information about itself only ... the same until you discover that you do not need the class node .

Problem 2

Let's review the following code carefully:

class Circulo
{
private:
  int x,y,r1,r2;
  Figuras *fig=new Figuras();//reservo espacio de memoria para figura
  Lista_Figuras *ini=NULL;
public:
  Circulo();
  ~Circulo();
  void crear(int,int,int,int,int);
};

void Circulo::crear(int x,int y,int r1,int r2,int cls)
{
  setcolor(0);
  setfillstyle(1,cls);
  fillellipse(x,y,r1,r2);

  fig->setX(x);
  fig->setY(y);
  fig->setX1(r1);
  fig->setY1(r2);
  fig->setCls(cls);
  fig->setT(1);
  ini=ini->insertar(ini,fig);
  cout<<"Ini: "<<ini->getFigura()->getX()<<endl;
  ini->mostrarLista(ini);
}

Failing to indicate the constructor's code ... Do not you see anything weird or dangerous? Reread it and pay attention to variable ini . Do you see anything now? I have found this (eliminating the noise):

class Circulo
{
private:
  Lista_Figuras *ini=NULL;
public:
  void crear(int,int,int,int,int);
};

void Circulo::crear(int x,int y,int r1,int r2,int cls)
{
  ini=ini->insertar(ini,fig);
}

That is, ini begins to point to NULL , then there is no memory reserved for the list ... and yet% is called ini->insertar happily ... Are you sure the application does not fail? Very careful with the use of pointers. It's great that you initialize them ... but before using them you have to be very sure that they point to a valid object or memory reserve.

This problem would be solved only by applying a solution to problem 1 , since dividing Lista_Figuras is normal is that you realize that it is not necessary to have a pointer to manage the list of figures or, as there will only be one list, the object can be initialized in the constructor without problems.

This does not have to solve the problem, but your question still leaves a lot to be desired and I notice you quite stuck. Ideally, you should reconstruct the question and present a minimal and complete example that reproduces the problem. And with complete I mean that, complete, a code that can copy on my computer and verify your problem without having to invent lines of code.

    
answered by 31.01.2018 / 09:09
source