Help Related Linked Lists Basics

This is a discussion on Help Related Linked Lists Basics within the C++ Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include"alloc.h" struct node { int data; struct node *link; }; void main() { struct node *p,*q,*r; clrscr(); p=(struct ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Help Related Linked Lists Basics

    Code:
    #include<stdio.h>
    #include"alloc.h"
    struct node
    {
    int data;
    struct node *link;
    };
    void main()
    {
    struct node *p,*q,*r;
    clrscr();
    p=(struct node *)malloc(sizeof(struct node));
    p->data=1;
    q=malloc(sizeof(struct node));
    q->data=2;
    p->link=&q->data;
    r=malloc(sizeof(struct node));
    r->data=3;
    q->link=&r->data;
    r->link=NULL;
    printf("p data=%d p address=%u",p->data,p->link);
    printf("\n%d",*p->link->link->link);
    printf("\n%d",*p->link->link);
    getch();
    }
    this is a simple piece of the code every thing is clear but i m having prob with p->link->link->link or p->link->link statements i m unable to understand how they are working
    i know that *p->link->link will print the value at r and p->link->link->link will print 0 "NULL" ascii but can someone explain actually what is happening internally in these p->link->link....link statements what are these and how these are working THANX in advance.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    everytime you see 'p' 'q' 'r' or 'link' they are all the same thing, they are all 'node*'. that is, pointer to a struct that contains an int data and a pointer to another node (named link). its really simple your probably just not thinking it is, its actually hard for me to explain.

    *p->link is accessing 'p's 'link' variable.
    *p->link->link is accessing 'p->link's 'link' variable, and so on.

    they are just accessing different variables that are floating around in different memory addresses. try and draw pictures of the code as you code through it, with squares representing 'nodes' and write random memory addresses on them, and use arrows to point to where each node's link points to. or search for it online im sure theres great descriptions, linkedlists are very popular and you would find many answers im sure.

    hope that helps a alittle, dont really know how to explain it!

    edit: i guess this isnt really c++ right? anyways you should at least have int main not void main
    Last edited by nadroj; 10-31-2006 at 10:59 AM.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by ketav
    this is a simple piece of the code every thing is clear
    It shouldn't, because your code has errors.

    Code:
    q->data
    is int
    Code:
    &q->data
    is pointer to int (int*)

    p->link has a type struct node*

    you cannot store variable of the type int* in the variable of the type struct node* (actually you can but here is not the case). You should get the warning from your compiler.

    The right assignment will be
    Code:
    p->link=q;
    here you connect the first node of the list named p with the second node named q

    Then the same is done for q and r.
    And the last node named r is not linked to any other node. To indicate this its link is set to NULL (correctly)

    Now the
    Code:
    p->link is q
    p->link->link is q->link is r
    p->link->link->link is q->link->link is r->link is NULL
    the last note
    *p->link has type struct node

    so you cannot print it with "%d" format

    but p->link->data is q->data is int - and this variable can be printed with the format above
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    16
    Assuming that your code worked properly, here is how it would work:

    Imagine a bunch of boxes in a row. Between each box is an arrow pointing to the next box. P is the first box in this row, and Q is the box that it points to. Each box holds a number; that's it's value.

    Where does this fit in your code? Well, P is your first node*. the value in the box is the value of p->data. And Q is the value that p->link points at. I'm assuming you understand pointers if you are using linked lists. Basically, a linked list points to another instance of the same structure, with the last item in the list pointing at NULL.

    So when you say p->link, you're saying q. When you say p->link->link, you're saying q->link, or r. When you say p->link->link->link, you're saying q->link->link, or r->link, or NULL.

    Hope that's what you wanted to know.

    EDIT: * is the de-reference operator when you use it in *p->link->link, so it will dereference the value of p->link->link. Except that I'm not sure which operator has higher priority, * or ->, so it might dereference p before trying to point to link? Just to be safe, I'd say *(p->link->link) to get an actual node object.

  5. #5
    Registered User Xeridanus's Avatar
    Join Date
    Oct 2006
    Location
    QLD, Aussieland
    Posts
    11
    I think the * operator has lower priority, isn't that the whole reason for the -> operator replacing the . operator for pointers.

    *looking up my C manual* ok, subscripts[], function calls(), field.selection, indirect->selection, ++(postfix), --(postfix) have the highest precedence. then ++(prefix), --(prefix), sizeof, ~!-+&*(unary prefix) are next. so that means that the *p->link->link will dereference p->link->link

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    thank you every body who tried to answer my question but specially @vart who cleared my doubt and i didnt read what is written below vart's post but thanx everybody i love this forum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21