2

I am aware of how to use pointers in order to point to other objects such as arrays, but in the scheme of knowing how to use them in linked lists, I am confused. For Example:

 struct node {
    int info;
    struct node *next;
}
typedef struct node Node

And assume that later on in the program, dataList is set to the address of the first node of a list that holds these data in this order: 25,30,45,60,65,80,90

I am to write C statements that set ptr1 to the address of the node holding 45 and set ptr2 to the address of the node holding 80.

Given that dataList is initially the head of the list, my idea was:

  ptr1= dataList.next.next 

but I am sure that is incorrect formatting or I also thought that

ptr1->next->next 

might be correct, but again, I am not sure.

2
  • ptr1= (dataList.next)->next Commented Mar 4, 2014 at 6:25
  • 1
    ptr1= dataList->next->next,Assuming dataList is the pointer to the head element. Commented Mar 4, 2014 at 6:30

5 Answers 5

2

ptr1->next is equivalent to (*ptr1).next, i.e. it gets the member called next from the struct that ptr1 points to.

Structure dereference ("member b of object pointed to by a") a->b

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

Comments

1

As you said, dataList, ptr1 and ptr2 are pointers to struct node. So it should be ptr1 = dataList->next->next. -> is just a shortcut for a pointer to reference to the member of the struct it points to.

Comments

1

dataList is the one that is holding the start address of the list. So you access them all using dataList variable. Your idea of using ->next->next to point to 45 is correct just that it should be ptr1 = dataList->next->next, but we dont go about doing ->next->next->next... all over to get every element, but use a pointer to hold every next address and loop till we have got desired value.

Comments

0

Doing next->next->next will work, but that isn't what we would want to achieve by using linked lists. Compare the info of each node while traversing and point ptr1 and prt2 to the node where you want to point. A sample code is given below.

PS: I am just a beginner, correct me if I am wrong.

if ( dataList != 0 ) {
        while ( dataList->next != 0)
        {
            dataList = dataList->next;
            if (dataList->info == 25 ) {
                ptr1 = dataList;
            }
            if (dataList->info == 80 ) {
                ptr2 = dataList;
            }

        }
}

Comments

0

Assume the following declaration for dataList

   struct node anode1, anode2, anode3, anode4, anode5, anode6, anode7;
   struct node *dataList = &anode1;

   anode1.info = 25;
   anode1.next = &anode2;

   anode2.info = 30;
   anode2.next = &anode3;

   anode3.info = 45;
   anode3.next = &anode4;

   anode4.info = 60;
   anode4.next = &anode5;

   anode5.info = 65;
   anode5.next = &anode6;

   anode6.info = 80;
   anode6.next = &anode7;

   anode7.info = 90;
   anode7.next = NULL;

   printf("Third node = %d\n", dataList->next->next->info); // prints 45
   printf("Sixth node = %d\n", dataList->next->next->next->next->next->info); // prints 80

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.