bartleby

Concept explainers

Question
Book Icon
Chapter 18, Problem 12PC
Program Plan Intro

Double Merge

Program Plan:

“NumberList.h”:

  • Include the required specifications into the program.
  • Define a class named “NumberList”.
    • Declare the member variables “value” and “*next” in structure named “ListNode”.
    • Declare the constructor, copy constructor, destructor, and member functions in the class.

“NumberList.cpp”:

  • Include the required header files into the program.
  • Define a function named “appendNode()” to insert the node at end of the list.
    • Declare the structure pointer variables “newNode” and “nodePtr” for the structure named “ListNode”.
    • Assign the value of received variable “num” into “newNode” and assign the “newNode” address into null.
    • Using “if…else” condition check whether the list is empty or not, if the “head” is empty and make a new node into “head” pointer. Otherwise, make a loop find last node in the loop.
    • Assign “newNode” into the variable “nodePtr” value.
  • Define a function named “displayList()” to print the values in the list.
    • Declare the structure pointer “nodePtr” for the structure named “ListNode”.
    • Initialize the variable “dataPtr” with the “head” pointer.
    • Make a loop “while” to display the values of the list.
  • Define a function named “insertNode()” used to insert a value into the list.
    • Declare the structure pointer variables “newNode”, “dataPtr”, and “prev” for the structure named “ListNode”.
    • Make a “newNode” value into the received variable value “num”.
    • Using “if…else” condition to check whether the list is empty or not.
      • If the list is empty, initialize a value into “head” pointer with the value of “newNode” variable.
      • Otherwise, make a “while” loop to test the “num” value is less than the list values.
      • Using “if…else” condition to initialize the value into list.
  • Define a function named “deleteNode()” to delete a value from the list.
    • Declare the structure pointer variables “nodePtr”, and “previousNode” for the structure named “ListNode”.
    • Using “if…else” condition to check the “head” value equal to “num”.
      • Initialize the address of “nodePtr” to the “head”.
      • Remove the value using “delete” operator and reassign the “head” value into the “nodePtr”.
      • If the “num” value not equal to the “head” value, define the “while” loop to assign the “nodePtr” into “previousNode”.
      • Using “if” condition to delete the “previousNode” pointer.
  • Define a destructor named “~NumberList()” to destroy the list values from the memory.
    • Declare the structure pointer variables “nodePtr”, and “nextNode” for the structure named “ListNode”.
    • Initialize the “head” value into the “nodePtr”.
    • Define a “while” loop to make the links of node into “nextNode” and remove the node using “delete” operator.
  • Define a member function named “mergeArray()” to merge the array into list.
    • Using “for” loop to insert a value of the array into list.
    • Make a call to “insertNode()” function to insert an index value of received array “dArray” into list.

“main.cpp”:

  • Include the required header files into the program.
  • Declare a constant variable “n” and initialize a value as “5”.
  • Declare and define a variable “arr” in type of array.
  • Make an object named “obj” for the class “NumberList” and call the member functions “append()” and “display()” using the object.
  • Make a call to “mergeArray()” with arguments of “arr” and “n” and then print the list using “displayList()” function.

Blurred answer
Students have asked these similar questions
Remove Duplicates This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list.
C++ function Linked list   Write a function, to be included in an unsorted linked list class, called replaceItem, that will receive two parameters, one called olditem, the other called new item. The function will replace all occurrences of old item with new item (if old item exists !!) and it will return the number of replacements done.
5. List Member Deletion Modify the list class you created in the previous programming challenges by adding a function to remove an item from the ist and by adding a destructor: void zemove (double x) : Linkedtist (02 Test the class by adding a sequence of instructions that mixes operations for adding items, removing items, and printing the list.

Chapter 18 Solutions

Starting Out with C++ from Control Structures to Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning