Analyzing pointer program

I was readint hte text book and i came across this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Cnode{
float value;
struct Cnode* next;
};
struct Cnode* first = NULL;

struct Cnode *nodePtr = first, *prevNodePtr = NULL;
float temp;
if (nodePtr-> next == NULL)
     cout<<"can't perform this operation - only one node in list!";
eles{
   prevNodePtr = nodePtr;
   nodePtr = nodePtr->next;
 do{
    temp = nodePtr -> value
    nodePtr->value = prevNodePtr->value;
    prevNodePtr->value = temp;
    prevNodePtr = nodePtr;
    nodePtr = nodePtr->next;
    }while(nodePtr != NULL);
}

assume that the list contrains the value 34, 65 76, 89
My interpretation of this program is either
1) this code is a insertion by using pointer
2) this code change the a certain value in the list although incomplete
It moves the first value in the list to the back of the list

So you should end up with
65, 76, 89, 34


EDIT:
Although it is not the quickest way to do it.
Last edited on
Coincidentally, I think this construct is called a "linked list", if you want to look it up for more info.
Guestgulkan, can you explain to me why this code moves the first value to the end? I can't see that for some reason.
For problems like this, it's best to just write it on a sheet of paper and do the logic yourself. Btw, as guest said this isn't really the fastest way to do it- and by far not the clearest either. Is that really how your text book does this? If so, it is probably an example of how you do NOT do it, or the text book is just bad.
Last edited on
I saw similar examples while studying pointers.hunkeelin, as Xander said search for "linked list" to see its schematic how elements (or links) are connected.It will help you understand how above code moves the values in links.
Topic archived. No new replies allowed.