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 7PC
Program Plan Intro

Member Removal by Position

Program Plan:

“IntList.h”:

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

“IntList.cpp”:

  • Include the required header files into the program.
  • Define a copy constructor named “IntList()” which takes an address of object for the “IntList” class as “const”.
    • Declare a structure pointer variable “nodePtr” and initialize it to be “nullptr”.
    • Assign “obj.head” value into the received variable “nodePtr”.
    • Make a “while” loop to copy the received values into “nodePtr”.
      •  Make a call to “appendNode()” to insert values to “nodePtr” and initialize address of “next” into “nodePtr”.
  • Define a function named “appendNode()” to insert the node at end of the list.
    • Declare the structure pointer variables “newNode” and “dataPtr” for the structure named “ListNode”.
    • Assign the value “num” to the variable “newNode” and assign null to the variable “newNode”.
    • Using “if…else” condition check whether the list is empty or not, if the “head” is empty then make a new node into “head” pointer. Otherwise, make a loop to find last node in the loop.
    • Assign the value of “dataPtr” into the variable “newNode”.
  • Define a function named “print()”to print the values in the list.
    • Declare the structure pointer “dataPtr” 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()” 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”.
    • Use “if…else” condition to check whether the list is empty or not.
      • If the list is empty then initialize “head” pointer with the value of “newNode” variable.
      • Otherwise, make a “while” loop to test whether the “num” value is less than the list values or not.
      • Use “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 “dataPtr”, and “prev” for the structure named “ListNode”.
    • Use “if…else” condition to check whether the “head” value is equal to “num” or not.
      • Initialize the variable “dataPtr” with the value of the variable “head”.
      • Remove the value using “delete” operator and reassign the “head” value into the “dataPtr”.
      • If the “num” value not equal to the “head” value, then define the “while” loop to assign the “dataPtr” into “prev”.
      • Use “if” condition to delete the “prev” pointer.
  • Define a function named “reverse()” to reverse the values in a list.
    • Declare the pointer variables “newNode”, “newHead”, “nodePtr”, and “tempPtr” for the structure named “ListNode”.
    • Initialize the variable “nodePtr” with the value of the variable “head”.
    • Define a “while” loop to allocate “newNode” variable.
      • Create a “newNode” for the structure “ListNode”.
      • Store the value of “nodePtr” into “newNode” and assign address as null to the “newNode” pointer.
      • Using “if…else” condition swap the values of “newHead” and “newNode”.
        • Assign the address of “next” node into “nodePtr”.
      • Initialize the variable “head” with the value of the variable “newHead”.
  • Define a function named “destroy()” to destroy the list values from the memory.
    • Declare the structure pointer variables “dataPtr”, and “nextNode” for the structure named “ListNode”.
    • Initialize the “head” value into the “dataPtr”.
    • Define a “while” loop to make the links of node into “nextNode” and remove the node using “delete” operator.
  • Define a function “search()” to find the argument value of “num” in the list.
    • Declare a variable “count” in type of “int”.
    • Declare a structure pointer variable “*dataPtr” for the structure named “ListNode”.
    • Define a “while” loop to search the value in the list.
      • Using “if…else” statement, check the value of “dataPtr” in the list.
        • If the condition is “true”, return the value “count” variable.
        • Otherwise, point the “next” value of “dataPtr” and then increment the value of “count” variable.
    • Return a value “-1” to the function call.
  • Define a function named “insert()” with the arguments of “value” and “pos” to insert a value at specified location.
    • Declare a pointer variable “newNode” for the structure “ListNode”.
    • Assign the value of received variable “value” into “newNode” value and make address of “newNode” into “nullptr”.
    • Using “if” condition to check whether the list is empty or not.
      • If list is empty, initialize the variable “head” with the value of the variable “newNode”.
    • Using “if” condition to insert the value of received variable “pos” into the list.
      • Assign the “head” node into address of “newNode” .
      • Initialize the variable “head” with the value of the variable “newNode”.
      • Using “while” loop to insert the value at specified position in the list.
  • Define a function named “removeByPos()” with an argument “pos” to remove a value at specified position in list.
    • Declare a pointer variable “temp” for the structure “ListNode”.
    • Using “if” condition, check whether the list is “empty” or not. If the list is empty, return “null” to “main()” function.
    • Otherwise, using “while” loop to traverse the list to find the “pos” in list.
    • Using “if…else” condition, check whether the received value of “pos” is value of “head” or not.
      • If the condition is true, delete “head” node from the list.
      • Otherwise, assign pointers to the next node of removable value then delete the node using “delete” operator.
  • Define the destructor to call the member function “destroy()” in the list.

“Main.cpp”:

  • Include the required header files into the program.
  • Declare an object named “obj” for the class “IntList”.
  • Make a call to functions for insert and append operations.
  • Make a call to “print()” function to display the list on the screen.
  • Make a call to “removeByPos()” function to remove the value at specified position from the list and print the list using “print()” 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).
C++ The List class represents a linked list of dynamically allocated elements. The list has only one member variable head which is a pointer that leads to the first element. See the following code for the destructor to List. ~ List () {      for (int i = 0; i <size (); i ++)       {             pop_back ();          } } What problems does the destructor have? Select one or more options: 1. There are no parameters for the destructor. 2. The return value from pop_back (if any) is nerver handled. 3. The destructor will create a stack overflow. 4. The destructor will create dangling pointers. 5.The destructor will create memory leaks. 6.The destructor will create undefined behavior (equivalent to zero pointer exception). 7.The condition must be: i <size () - 1 8. There is at least one problem with the destructor, but none of the above.
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