Starting Out with C++
Starting Out with C++
8th Edition
ISBN: 9780133888201
Author: GADDIS
Publisher: PEARSON CUSTOM PUB.(CONSIGNMENT)
bartleby

Concept explainers

Question
Book Icon
Chapter 17, 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
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.
(Circular linked lists) This chapter defined and identified various operations on a circular linked list.a. Write the definitions of the class circularLinkedList and its member functions. (You may assume that the elements of the circular linked list are in ascending order.)b. Write a program to test various operations of the class defined in (a).
Unordered Sets |As explained in this chapter, a set is a collection of distinct elements of the same type. Design the class unorderedSetType, derived from the class unorderedArrayListType, to manipulate sets. Note that you need to redefine only the functions insertAt, insertEnd, and replaceAt. If the item to be inserted is already in the list, the functions insertAt and insertEnd output an appropriate message, such as 13 is already in the set. Similarly, if the item to be replaced is already in the list, the function replaceAt outputs an appropriate message. Also, write a program to test your class.
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