node example. What is pointing? Is it a pointer, reference, value?

http://beastie.cs.ua.edu/cs150/book/index_16.html


I do not fully understand nodes

I am trying to debug code and look at pictures of pointing
but I can not understand how a variable is pointed to another location (value, reference)
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aboo_sCommented:
Each variable can obtain a value and a pointer to the next variable of the same kind.
For example you can have a list of variables that contain your favorite three colors in a list.
First would be a value of "Red", next on this var would be pointing to another var that has "Blue" in it's value and a memory address of a third var in it's next pointer. the third var's value would be "Yellow" and it's next pointer would be pointing to null.
0
peprCommented:
Value in computers is always represented as a memory place of some size that stores something. What the something is depends on interpretation, on agreement. The memory place has some address (i.e. where to look for the value). The only way to get the value is to use the address and look at the memory content at the address. The address is always a part of some machine instruction (stored explicitly in the machine code), or stored in some procesor register -- then the machine instruction uses the register and accesses the memory place indirectly, then the address in the register is already a reference value (or pointer value if you like -- it is equal at this level).

In compiled languages, you can think about the address of a memory place similarly as the machine instruction does. When using a variable name, it can be converted by the compiler/linker to the explicit memory address, and it can be used explicitly by the related machine instructions (the compiler decides if an instruction contains the address or if another instruction that works with a register will be used -- you do not care).

In Python, every value is implemented as an object (that is the instance of some class). The id(obj) is the function that returns the unique technical identification of the object -- the CPython implementation uses a physical address of the object as the identification. To put it together, you can always use id(obj) to illustrate for yourself where the object is placed, what is its true identity.

In Python, variables have a bit different meaning than in classical compiled languages. A Python variable is a string name bound with the target object via one of the Python's internal dictionaries. The value part of that dictionary item contains the reference value -- the address of the object. However, the binding between the string name and the target object is hidden behind the scene. The syntax does not show you that you access the memory place indirectly. It looks as if you accessed directly the memory place that was somehow given a name. This is what is called reference -- the reference value (the address) is used behind the scene to get to the target.

If a programming language makes the indirect access to the target object via its address visible, then we say we work with pointers. The target is also accessed indirectly, but you can also see it in your source code. This is not the case of Python. Anyway, when you want to understand how (say) a linked list is implemented, you have to think about addresses to the next item stored somewhere. This is obscured by Python, and actually this is the thing that makes explanation of graph structures (that is nodes connected via edges -- like linked lists, trees, general graphs) more difficult.

In Python, the single target object need not have its own name. Think about the element of the list. The list has its name, but the element does not have one. It can be accessed via the name of the list plus the index. Even the list need not to have its own name -- it can be a part of a bigger structure. In Python Illustrated - part 3, you can see that the list is actually the simplest structure that is capable to store more references to the target objects, and that is capable to modify the references. (A bit simpler is only tuple, but the inside stored references cannot be modified.)

For the example, a list with two elements can be used to implement the node (of the linked list or other graph structures). Usually, the first element is used for the value, and the second element is used to point to the next node. However, Python obscures the indirection. When printing the representation of the node, it looks as if the next node were contained inside the first node. But now you know, it is only an illusion. The [4, [3, [2, [1, None]]]] is actually something like:
(4, *)->(3, *)->(2, *)->(1, *)->None

Open in new window

When you assign the target object to a Python variable, you actually assign the reference to the target object (the address) to the value part of the internal Python dictionary. Then you can have in the case something like:
'var'->(2, *)->None

Open in new window

if you work with the node that contains the value 2. From the part 3 of the article you know, that actually also the value part is referenced indirectly. But one have to simplify the things for his own brain ;)
0
rgb192Author Commented:
aboo_s

is red pointing to value or reference or pointer of blue

pepr
This is what is called reference -- the reference value (the address) is used behind the scene to get to the target.

If a programming language makes the indirect access to the target object via its address visible, then we say we work with pointers. The target is also accessed indirectly,
is a pointer a reference pointing to a node

The [4, [3, [2, [1, None]]]] is actually something like:
is this a list, tuple
0
peprCommented:
The difference between pointers and references is that pointers make the things behind more visible. But this is related only to some languages, namely to C and C++, but also in Pascal, Delphi... The example in those languages would use pointer variables for implementation of links to the next node. Some of the languages do not support references. But those that do support references always require that the reference is always filled with value to some existing objects (i.e. some address). Pointers on the other hand can contain a special value named nil, NULL, nullptr or the like that says "pointing to nowhere".

For [4, [3, [2, [1, None]]]], you should already be able to tell me. Starting from outer [ ]... The square brackets means it is a list. Tuples are enclosed in parentheses. What contains that outer list? (Hint: two elements only.)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:

But those that do support references always require that the reference is always filled with value to some existing objects (i.e. some address). Pointers on the other hand can contain a special value named nil, NULL, nullptr or the like that says "pointing to nowhere".

so now I know one difference between pointer and reference

thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.