Question: Write a function, DeleteNode , that will find a value and remove that node from a list. Write a function, AddNode , that will add
Write a function, DeleteNode, that will find a value and remove that node from a list.
Write a function, AddNode, that will add a given node to a list in a sorted position.
This is what i have so far
#include
using namespace std;
struct NodeType { int component; NodeType *next; }; void PrintList(NodeType *currPtr);
NodeType *CreateNode(int value);
void DeleteNode(NodeType * &head, int num){ NodeType *currPtr = head; NodeType *prevPtr = nullptr; //loop (currptr != nullptr) and (currptr->component != num) //prevPtr = currPtr cout << "node:" << currPtr->component << endl; //currPtr = currPtr->next
//if currptr != nullptr
//if prev = null //head = currptr->next //delete currPtr;
//else if currptr->next == null //prevptr->next = null //delete currptr;
//else //prevPtr->next = currPtr->next //delete currPtr;
}
int main() { NodeType *lastPtr; //Keep track of last pointer in list NodeType *head; // Keep track of beginning of list NodeType *currPtr; //Current working pointer NodeType *newNodePtr; //Keep track of newly allocated note
// 1) Create a new node, assign the value 200 to the component, // set head to point to the node, also set currPtr and lastPtr to // point to the node. Set next to nullptr newNodePtr = CreateNode(200); head = currPtr = lastPtr = newNodePtr; PrintList (head);
// 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 = CreateNode(300); lastPtr->next = newNodePtr; lastPtr = newNodePtr; PrintList (head);
// 3) Using head, print out the component value of both nodes //cout << head->component << endl; //cout << head->next->component << endl;
// 4) Using currPtr, print out the component value of the first node //curPtr = head; //cout << curPtr->component << endl;
// 5) Advance currPtr to point to the second node and print out the component //currPtr = currPtr->next; //cout << currPtr->component << endl;
// 6) Create a new node with component value 400 and add it to the // end of the list. newNodePtr = CreateNode(400); lastPtr->next = newNodePtr; lastPtr = newNodePtr; PrintList (head);
// 7) Create a new node with component value 100 and add it to the // front of the list. newNodePtr = new NodeType; newNodePtr->component = 100; newNodePtr->next = head; head = newNodePtr; PrintList (head);
// 8) Write a loop that will use currPtr to traverse through the list and // print out each component currPtr = head; while (currPtr != nullptr){ cout << currPtr->component << " "; currPtr = currPtr->next; } cout << endl;
// 9) Move your loop from the last step into a function. PrintList (head);
// 10) Use your function to print out the list after adding each node in // the steps above DeleteNode(head, 200); PrintList(head); DeleteNode(head, 300); PrintList(head); DeleteNode(head, 400); PrintList(head); DeleteNode(head, 100); PrintList(head);
return 0; }
void PrintList(NodeType *currPtr){ while (currPtr != nullptr){ cout << currPtr->component << " "; currPtr = currPtr->next; } cout << endl;
}
NodeType *CreateNode(int value){ NodeType *newNodePtr;
newNodePtr = new NodeType; newNodePtr->component = value; newNodePtr->next = nullptr;
return newNodePtr; }
can someone please finish this program for me i am stuck
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
