do steps 1 trhough 10 in c++ #include <iostream> using namespace std; struct Nod
ID: 3814825 • Letter: D
Question
do steps 1 trhough 10 in c++ #include <iostream> using namespace std; struct NodeType { int component; NodeType *link; }; int main() { NodeType *lastPtr; NodeType *listPtr; NodeType *currPtr; NodeType *newNodePtr; // 1) Create a new node, assign the value 200 to the component, // set listPtr to point to the node, also set currPtr and lastPtr to // point to the node. Set link to nullptr // 2) Create a new node with component value 300. Add this element to // the end of the list. (i.e., Set link of node from Step 1 to point to // this node.) What else do you need to set? // 3) Using listPtr, print out the component value of both nodes // 4) Using currPtr, print out the component value of the first node // 5) Advance currPtr to point to the second node and print out the component // 6) Create a new node with component value 400 and add it to the // end of the list. // 7) Create a new node with component value 100 and add it to the // front of the list. // 8) Write a loop that will use currPtr to traverse through the list and // print out each component // 9) Move your loop from the last step into a function. // 10) Use your function to print out the list after adding each node in // the steps above return 0; }
Explanation / Answer
#include <iostream>
using namespace std;
struct NodeType
{
int component;
NodeType *link;
};
void print_list(NodeType *listPtr){
cout<<"Printing from function"<<endl;
cout<<"---Start---"<<endl;
NodeType *currPtr = listPtr;
while(currPtr){
cout<<currPtr->component<<endl;
currPtr = currPtr->link;
}
cout<<"---End---"<<endl;
}
int main(){
NodeType *lastPtr;
NodeType *listPtr;
NodeType *currPtr;
NodeType *newNodePtr;
// 1) Create a new node, assign the value 200 to the component,
// set listPtr to point to the node, also set currPtr and lastPtr to
// point to the node. Set link to nullptr
newNodePtr = new NodeType();
newNodePtr->component = 200;
newNodePtr->link = NULL;
listPtr = lastPtr = currPtr = newNodePtr;
print_list(listPtr);
// 2) Create a new node with component value 300. Add this element to
// the end of the list. (i.e., Set link of node from Step 1 to point to
// this node.) What else do you need to set?
newNodePtr = new NodeType();
newNodePtr->component = 300;
newNodePtr->link = NULL;
currPtr->link = newNodePtr;//Set the link of newPtr to the currPtr
lastPtr = newNodePtr;
print_list(listPtr);
// 3) Using listPtr, print out the component value of both nodes
cout<<listPtr->component<<endl;
cout<<listPtr->link->component<<endl;
print_list(listPtr);
// 4) Using currPtr, print out the component value of the first node
cout<<currPtr->component<<endl;
// 5) Advance currPtr to point to the second node and print out the component
currPtr = currPtr->link;
cout<<currPtr->component<<endl;
// 6) Create a new node with component value 400 and add it to the
// end of the list.
newNodePtr = new NodeType();
newNodePtr->component = 400;
newNodePtr->link = NULL;
lastPtr->link = newNodePtr;
lastPtr = newNodePtr;
print_list(listPtr);
// 7) Create a new node with component value 100 and add it to the
// front of the list.
newNodePtr = new NodeType();
newNodePtr->component = 200;
newNodePtr->link = listPtr;
listPtr = newNodePtr;
print_list(listPtr);
// 8) Write a loop that will use currPtr to traverse through the list and
// print out each component
currPtr = listPtr;
while(currPtr){
cout<<currPtr->component<<endl;
currPtr = currPtr->link;
}
// 9) Move your loop from the last step into a function.
// 10) Use your function to print out the list after adding each node in
// the steps above
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.