Doubt with linked lists (pointers) C ++

0

The exercise in question is this:

I already have this code in the header:

#ifndef HEADER_H
#define HEADER_H7

//Tipo dato Nodo
struct Nodo
{
    int dato;
    Nodo* izq;
    Nodo* der;
};

//Tipo dato Estructura
struct Estructura
{
   Nodo* inicio;
   Nodo* fin;
};


void inicializarEstructura(Estructura& est);
//{PRE: 'est.inicio' es nullptr y ‘est.fin’ es nullptr}

#endif // HEADER_H

And this is the code that I have at the moment of the main:

//Tema 1. Ejercicios punteros.

#include <iostream>
#include <header.h>

using namespace std;

int main()
{

    return 0;
}

void inicializarEstructura(Estructura& est)
{
     //1º Aquí asigno memoria dinámica para cada nodo
     Nodo* p = new Nodo;
     Nodo* q = new Nodo;
     Nodo* h = new Nodo;

     //2º Asigno los punteros correspondientes
     //2.1º Primero los punteros de la Estructura "est" a nodo
     *est.inicio = *q->der;
     *est.fin = *h->izq;
     //2.2º Segundo los punteros de nodo a nodo
     *q->der = *est.fin;
     *p->izq = *est.inicio;
     est.fin->izq = p->dato;
}

I miss this error:

I only get the error when I try to point with the left variable (node type) of the third node, to the data variable (int type) of the 1st node.

How do I solve it?

Thanks in advance.

PS: Receiving an answer is important to me, I need to understand the concept of pointer correctly, and I think that by seeing this exercise solved I will understand this topic a lot better.

    
asked by AguaSal 21.03.2017 в 01:41
source

2 answers

2
est.fin->izq = p->dato;

If dato is an integer, no you can assign it to a pointer.

What you want to do, initialize the list, is very simple

void inicializarEstructura( Estructura& est ) {
  nodo *izquierda = new Nodo;
  nodo *centro = new Nodo;
  nodo *derecha = new Nodo;

  izquierda->izq = nullptr;
  izquierda->dato = 2;
  izquierda->der = centro;

  centro->izq = izquierda;
  centro->dato = 1;
  dentro->der = nullptr;

  derecha->izq = centro;
  derecha->dato = 3;
  derecha->der = nullptr;

  est.inicio = izquierda;
  est.fin = derecha.
}

Questions about pointers there are a few on the site. You can consult them, to see several and varied examples.

    
answered by 21.03.2017 / 06:19
source
0

Try changing the points (.) that appear in the errors by arrow (->)

  

To access the members of a structure, use the dot operator. To access the members of a structure through a pointer, use the arrow operator.

Arrow Operator

Arrows page in English

Spanish pointer operators

    
answered by 21.03.2017 в 02:59