Hi everyone,
I'm studying for my final exam and I am stuck on a review question.

The question is as follows:

Consider this code fragment and answer the questions, which follow.

NODE *p1, *p2,*p3;
NODE *p, *pFirst, *pNew, *pTemp;

Question:
Write the code to allocate memory for nodes at p1, p2, and p3. Do not declare any new variables. Assume that your code will appear after the above fragment, and write only the necessary statements.

This is what I have come up with. Let me know if I am right or where I went wrong.

pFirst = NULL;

pNew = new NODE;
if (pFirst == NULL)
{
pFirst = pNew;
pTemp = pNew;
}
else
{
pTemp = pNode;
}

2. >Write the code to allocate memory for nodes at p1, p2, and p3.
All this question asks you to do is allocate memory for those three pointers:
Code:
```try {
p1 = new NODE;
p2 = new NODE;
p3 = new NODE;
}
cerr<<"Memory allocation failure\n";
}```
Done.

-Prelude

Originally posted by cheeisme123
Code:
```pFirst = NULL;

if(pFirst == NULL)
{
...
}```
Why check if it's NULL in this case where it's always NULL?

4. Write additional statements to make the nodes at p1, p2, p3 into a forrward-linked list, such that p3 is the first node, p1 is the second, and p2 is last. Assign the address of the first node to pFirst.

How does this sound:

I'm studying for my final exam and I am stuck on a review question.

The question is as follows:

Consider this code fragment and answer the questions, which follow.

NODE *p1, *p2,*p3;
NODE *p, *pFirst, *pNew, *pTemp;

try {
p1 = new NODE;
p2 = new NODE;
p3 = new NODE;
}
cerr<<"Memory allocation failure\n";
}

QUESTION:

Write additional statements to make the nodes at p1, p2, p3 into a forrward-linked list, such that p3 is the first node, p1 is the second, and p2 is last. Assign the address of the first node to pFirst.

How does this sound:

6. >Write additional statements to make the nodes at p1, p2, p3 into a forrward-linked list, such that p3 is the first node, p1 is the second, and p2 is last.
Code:
```p3->next = p1; // p3 points to p1
p1->next = p2; // p1 points to p2```
>Assign the address of the first node to pFirst.
Code:
`pFirst = p3; // pFirst points to the start, p3`
-Prelude

7. >How does this sound:
It sounds like you should keep the same questions to a single thread. Double posting is rude, please don't do it.

-Prelude