What's wrong with this code? I really don't know. I'm beginner. Sorry for my english.
#include <iostream>
using namespace std;
struct elem
{
int wartosc;
elem* nast;
elem* poprz;
};
class ListaDwukierunkowa
{
protected:
elem **lista;
public:
ListaDwukierunkowa(elem** lista)
{
this->lista = lista;
}
void dodaj_elem(int do_dodania)
{
if (*lista == nullptr)
{
*lista = new elem;
(*lista)->wartosc = do_dodania;
(*lista)->nast = nullptr;
(*lista)->poprz = nullptr;
}
else
{
elem *temp = *lista;
while (temp->nast != nullptr)
{
temp = temp->nast;
}
temp->nast = new elem;
temp->nast->wartosc = do_dodania;
temp->nast->nast = nullptr;
temp->nast->poprz = temp;
}
}
void wyswietl_elem()
{
cout << endl;
if (lista == nullptr)
{
cout << "Lista jest pusta" << endl;
}
while (lista != nullptr)
{
cout << "poprz: " << lista->poprz << " | ten: " << lista << " | war:" << lista->wartosc << " | nast:" << lista->nast << endl;
lista = lista->nast;
}
}
void usun_elem(int do_usun)
{
elem *temp = *lista;
if (*lista == nullptr)
return;
while (temp->wartosc != do_usun)
{
temp = temp->nast;
if (temp == nullptr)
return;
}
if (temp->poprz)
temp->poprz->nast = temp->nast;
else
*lista = temp->nast;
if (temp->nast)
temp->nast->poprz = temp->poprz;
delete temp;
}
int liczba_elem()
{
int liczba = 0;
while (lista != nullptr)
{
liczba += 1;
lista = lista->nast;
}
return liczba;
}
int liczba_elem_o_war(int x)
{
int liczba = 0;
while (lista != nullptr)
{
if (lista->wartosc == x)
{
liczba += 1;
}
lista = lista->nast;
}
return liczba;
}
bool czy_zawiera(int x)
{
bool czy = false;
while (lista != nullptr)
{
if (lista->wartosc == x)
{
czy = true;
}
lista = lista->nast;
}
return czy;
}
void zwolnij_liste()
{
while (*lista != nullptr)
{
elem* nast = (*lista)->nast;
delete *lista;
*lista = nast;
}
*lista = nullptr;
}
};
int main()
{
ListaDwukierunkowa lista;
lista.dodaj_elem(1);
lista.wyswietl_elem();
// dodaj_elem(&wsk, 12);
// dodaj_elem(&wsk, 9);
// dodaj_elem(&wsk, 8);
// dodaj_elem(&wsk, 9);
// dodaj_elem(&wsk, 9);
// wyswietl_elem(wsk);
// cout << liczba_elem(wsk) << endl;
// cout << liczba_elem_o_war(wsk, 9) << endl;
// cout << boolalpha << czy_zawiera(wsk, 12) << endl;
// usun_elem(&wsk, 1);
// wyswietl_elem(wsk);
// zwolnij_liste(&wsk);
}
error: request for member 'poprz' in '*((ListaDwukierunkowa*)this)->ListaDwukierunkowa::lista', which is of pointer type 'elem*' (maybe you meant to use '->' ?)
cout << "poprz: " << lista->poprz << " | ten: " << lista << " | war:" << lista->wartosc << " | nast:" << lista->nast << endl;
image of errors,
lista
is of type elem**
: a pointer to a pointer.
The ->
operator defererences one level of pointers. So lista->poprz
is the same as (*lista).poprz.
In this case you need to dereference twice: (*lista)->poprz
.
Note: I have not tried to understand why lista
is a pointer to a pointer. It could be that you simply want elem*
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.