I am only experimenting on the basics of Data Structure and I got curious about something. As you can see in my code, after messing around with the p
pointer I decided to delete it. I deleted the p pointer, i assumed that it would also delete p->proximo
('proximo' is 'next' in Portuguese) but it didn't apparently. The result of Acesso 3 (Access 3 in Portuguese) was 0 22.
So first of all, if I deleted the pointer, why can I still connect to the p->proximo
pointer? Okay, but since it didn't, it is a good thing, it means I can delete things now. So then I decided to make the 22 value go to the first Nod. So I wouldn't have broken nodes on the list. I tried using p = p->proximo
as you can see in line 28. But it didn't work. Of course it didn't. I deleted the pointer. is there any way I can recreate the first pointer so I can fix the list, so the first node would have the value of 22? I tried with
p = new IntNode();
p = p->proximo;
but it also failed. Probably because of the existing following node maybe?
I appreciate very much the help of any of you
Code:
#include <iostream>
using namespace std;
class IntNode{
public:
IntNode(){
proximo = 0;
}
IntNode(int file, IntNode *next = 0)
{
number = file;
proximo = next;
}
int number;
IntNode *proximo;
};
int main()
{
IntNode *p = new IntNode(15);
cout << "Acesso 1: " << p->number;
p->proximo = new IntNode(22);
cout << endl << "Acesso 2: " << p->number << " " << p->proximo->number;
delete p;
cout << endl << "Acesso 3: " << p->number << " " << p->proximo->number << endl;
p = new IntNode(5);
cout << endl << "Acesso 4: " << p->number << " " << p->proximo->number << endl;
p = p->proximo;
cout << endl << "Acesso 5: " << p->number << " " << p->proximo->number << endl;
delete p->proximo;
cout << endl << "Acesso 6: " << p->number << " " << p->proximo->number << endl;
return 0;
}