Concept explainers
Purpose of the given code:
The given code is trying to insert the value of the variable “num” into the end of a linked list.
Given Code:
//Definition of function
void NumberList::appendNode(double num)//Line 1
{//Line 2
//Declaration of structure pointer variable
ListNode *newNode, *nodePtr;//Line 3
//Allocate a new node & store num
//Error #1
newNode = new listNode;//Line 4
newNode->value = num;//Line 5
// If there are no nodes in the list
// make newNode the first node
if (!head)//Line 6
head = newNode;//Line 7
// Otherwise, insert newNode at end
else //Line 8
{//Line 9
//Error #2
// Find the last node in the list
while (nodePtr->next)//Line 10
nodePtr = nodePtr->next;//Line 11
// Insert newNode as the last node
nodePtr->next = newNode;//Line 12
}//Line 13
}//Line 14
Want to see the full answer?
Check out a sample textbook solutionChapter 17 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
- Multiple choice in data structures void doo(node<int>*root){ if(root !=0) { node<int>*p=root; while(root->next!=0) root=root->next; p->data=root->data; } What is this code do? a. swap the first item with the last item in the linked list b. set the first item in the linked list as the last item c. doesn't do anything because the root parameter is passed by value d. change the root item in the binary tree with the farthest leaf itemarrow_forwardin cpp please AnyList.h #ifndef ANYLIST_H#define ANYLIST_H #include<iostream>#include <string> //Need to include for NULL class Node{public: Node() : data(0), ptrToNext(nullptr) {} Node(int theData, Node *newPtrToNext) : data(theData), ptrToNext(newPtrToNext){} Node* getPtrToNext() const { return ptrToNext; } int getData( ) const { return data; }void setData(int theData) { data = theData; } void setPtrToNext(Node *newPtrToNext) { ptrToNext = newPtrToNext; } ~Node(){}private:int data; Node *ptrToNext; //pointer that points to next node}; class AnyList{public: AnyList(); void insertFront(int); void print() const; void destroyList(); ~AnyList(); private: Node *ptrToFirst; //pointer to point to the first node in the list int count; //keeps track of number of nodes in the list}; #endif AnyList.cpp #include "AnyList.h" using namespace std; //constructorAnyList::AnyList(){ ptrToFirst = nullptr; count =…arrow_forwarddata structures-java language quickly plsarrow_forward
- struct remove_from_front_of_dll { // Function takes no parameters, removes the book at the front of a doubly // linked list, and returns nothing. void operator()(const Book& unused) { //// TO-DO (13) |||| // Write the lines of code to remove the book at the front of "my_dll", // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. ///// END-TO-DO (13) //// } std::list& my_dll; };arrow_forwardclass Node: def __init__(self, e, n): self.element = e self.next = n class LinkedList: def __init__(self, a): # Design the constructor based on data type of a. If 'a' is built in python list then # Creates a linked list using the values from the given array. head will refer # to the Node that contains the element from a[0] # Else Sets the value of head. head will refer # to the given LinkedList # Hint: Use the type() function to determine the data type of a self.head = None # To Do # Count the number of nodes in the list def countNode(self): # To Do # Print elements in the list def printList(self): # To Do # returns the reference of the Node at the given index. For invalid index return None. def nodeAt(self, idx): # To Doarrow_forward2. ID: A Name: 2. A list of numbers is considered increasing if each value after the first is greater than or equal to the preceding value. The following procedure is intended to return true if numberList is increasing and return false otherwise. Assume that numberList contains at least two elements. Line 1: PROCEDURE isIncreasing (numberList) Line 2: { Line 3: count 2 Line 4: REPEAT UNTIL(count > LENGTH(numberList)) Line 5: Line 6: IF(numberList[count] =. YA198ICarrow_forward
- struct node{ int a; struct node * nextptr; }; Write two functions. One for inserting new values to a link list that uses the given node structure. void insert(struct node **head, int value); Second function is called to count the number of even numbers in the link list. It returns an integer that represents the number of even numbers. int countEvenNumbers(struct node *head); Write a C program that reads a number of integers from the user and insert those integers into a link list (use insert function). Later pass the head pointer of this link list to a function called countEvenNumbers. This function counts and returns the number of even numbers in the list. The returned value will be printed on the screen. Note 1: Do not modify the function prototypes. Sample Input1: Sample Output1: 45 23 44 12 37 98 33 35 -1 3 Sample Input2: Sample Output2: 11 33 44 21 22 99 123 122 124 77 -1 4arrow_forwardIt is a to the next node in the linked list, which is the property after it in the Node class.arrow_forward6. Suppose that we have defined a singly linked list class that contains a list of unique integers in ascending order. Create a method that merges the integers into a new list. Note the additional requirements listed below. Notes: ● . Neither this list nor other list should change. The input lists will contain id's in sorted order. However, they may contain duplicate values. For example, other list might contain id's . You should not create duplicate id's in the list. Important: this list may contain duplicate id's, and other list may also contain duplicate id's. You must ensure that the resulting list does not contain duplicates, even if the input lists do contain duplicates.arrow_forward
- Circular linked list is a form of the linked list data structure where all nodes are connected as in a circle, which means there is no NULL at the end. Circular lists are generally used in applications which needs to go around the list repeatedly. struct Node * insertTONull (struct Node *last, int data) // This function is only for empty list 11 5 15 struct Node insertStart (struct Node +last, int data) In this question, you are going to implement the insert functions of a circular linked list in C. The Node struct, print function and the main function with its output is given below: struct Node { int data; struct Node *next; }; struct Node insertEnd (struct Node *last, int data) void print(struct Node *tailNode) struct Node *p; if (tailNode -- NULL) struct Node * insertSubseq (struct Node *last, int data, int item) puts("Empty"); return; p - tailNode → next; do{ printf("%d ",p→data); p - p > next; while(p !- tailNode →next); void main(void) { struct Node *tailNode - NULL; tailNode -…arrow_forwarddata structures in javaarrow_forwardInput code in the "Write code here" sectionsarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education