how do i insert a new node into a linked list that contains the given data value
ID: 3527457 • Letter: H
Question
how do i insert a new node into a linked list that contains the given data value, such that the new node occupies the offset indicated. any nodes that were already in the list at that offset and beyond are shifted down by one.Explanation / Answer
int main() { // // Init the list // nodeType *first; nodeType *last; nodeType *newNode; int num; first = NULL; last = NULL; // // Build the list the forward approach // for (int i=0; i num; newNode = new nodeType; // Create the new node newNode->data = num; // and assign its data value newNode->link = NULL; // make its link point to nothing if (first == NULL) // If there is nothing in the list, then make the { // newNode the first item and the last item first = newNode; last = newNode; } else // Else if first already has a value { // make the last item link to the newNode last->link = newNode; // and make newNode the last item last = newNode; } } // // Display the list // DisplayList(first); system("PAUSE"); return(0); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.