0

Image of Singly Linked List

Hy Guys, I'm litle bit confused with the image above. 1000, 800, 1400, and 1100 is memory address, so let's make it H1000, H800, H1400, H1100.

My Questions are:
1. Content of All of memory address above only INFO, or both INFO and LINK?
2. P only pointing to INFO, or to both?
3. P pointing to H1000?
4. If I print P, it'd shows me H1000?
5. R only pointing to LINK, or to both LINK and INFO?
6. If I print R, it'd shows me memory address of LINK or H800?
7. is P->LINK a correct syntax, since P is only a pointer?
8. is R->LINK a correct syntax, since R is only point to LINK?

3
  • The memory address is for the node which includes the payload (info) and the pointer to the next node (link). p points to the node (the struct that you use for the node) which contains the payload and link. 1000 or 1000hex are just different references to the same address. Commented Oct 11, 2015 at 9:11
  • C is not the same as C++... In C++, use some standard container, e.g. std::list Commented Oct 11, 2015 at 9:12
  • It's a terrible diagram. Does it help if you assume the addresses are of the whole struct, and all the pointers really point to the start of the struct and not fields within it? Commented Oct 11, 2015 at 9:13

4 Answers 4

1

The pointers point to the structures with INFO and LINK packed together. From a pointer, the compiler will know how to access both the fields INFO and LINK, denoted as P->INFO and P->LINK.

Most probably, the value in P and the address of P->INFO will be the same, and the address of P->LINK will be a constant offset from the address of P->INFO, but you shouldn't care about this.

Sign up to request clarification or add additional context in comments.

4 Comments

Right, you get it. The arrow R is somewhat misleading, unless they really mean "the address of the LINK field", but this is very unlikely.
If i write R->LINK, it'll take me to the next element which is pointed by S?
Yes, if R points to the structure. If R directly points to the LINK field, the syntax *R applies (but once again, this is unlikely).
if R pointing to LINK to the LINK field. That's mean, R is pointing H800+Offset.
1

Wow. That's an unhelpful image, without any context.

But generally speaking a linked list in C is a set of nodes, each node containing pointers to some useful information (the elements you want to store in the list) and a pointer to the next element in the list - if any.

struct node {
    void*   info;
    void*   link;
};

so, P (and Q and R and S) is/are a pointer(s) to struct node, so via p->info you can access the element and via p->link you know what the next element in the list is.

3 Comments

Probably you're right. But that can't easily be done in C - what would the type of info be? A bit more context on the image would've been helpful.
INFO is integer. What confuse me the most is the R pointer. I don't know if Q=R, or R is pointing to LINK field
I looks like it's pointing to the/a LINK field. But yes, it's quite confusing.
0

i assume the data structure for your quiestion is

struct Node {
  Info info;
  Node *link;
}

Node* p = new Node(15); // node 1
Node* q = new Node(27); // node 2
Node* r = new Node(22); // node 3
Node* s = new Node(17); // node 4

I'm not going to answer one by one.

p, q r, s and link have the same type, the are a pointer. So p will pointing to a node which is have an info and link. You have misconception in reading the picture.

When you print p, Of course you will see the address beacuse they all pointer. They can only contain an address not a value.

When you see p->link we can say it "pointer p is pointing to pointer link ". You will see an address that link have. it have the same address as a node 1

3 Comments

but in the picture, pointer R is pointing to LINK at the second element of the list.
No.. noo.. just like everyone said. It its terrible picture, don't trust the picture. menyesatkan!! . R is pointing to node. So that you can do like R->link or R->info.
so, R->link will take me to the next element?
0
R->link 

is pointing to the same address as

S->link 

.

R->link 

also pointing to the same address as

Q->link 

and

P->link->link

which is a node 3

if you understand this concept, it means you can pointing to node 4 by :

P->link->link->link
Q->link->link
R->link->link
S->link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.