bartleby

Concept explainers

Question
Book Icon
Chapter 17, Problem 15PC
Program Plan Intro

Pop( ) and push( ) Member functions

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 a variable named “size” in type of integer and initialize the variable as “0” in the constructor.
    • Define the constructor, destructor, and member functions in the class.
    • Declare the functions prototypes of  “pop_back()”, “pop_front()”, “push_back()”, and “push_front()”.

“IntList.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 “dataPtr” for the structure named “ListNode”.
    • Assign the “newNode” value into received variable “num” and assign the “newNode” address into null.
    • 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”.
    • Increment the value of the variable “size” by “1”.
  • Define a function named “display()” 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.
    • Increment the value of the variable “size” by “1”.
  • 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.
    • Decrement the value of the variable “size” by “1”.
  • Define the function named “getSize()” to return the current value of the “size”.
  • Define the destructor 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 the function named “pop_back()”  to insert a value at end of the list.
    • Declare the pointer variables for structure named “ListNode” and assign null to the “previousNode”.
    • Using “if” condition check whether the list is empty or not.
      • If the list is empty then throw a message on the screen.
    • Using “while” loop to traverse the list.
      • Initialize the variable “previousNode” with the value of the variable “node”.
      • Assign null to the address of “previousNode”.
    • Delete the value of “node” using “delete” operator.
    • Return the value of “temp” to the calling function.
  • Define the function named “pop_front()”  to insert a value at front of the list.
    • Declare the pointer variable “node” for a structure “ListNode”.
    • Using “if” condition check whether the list is empty or not.
      • If the list is empty then throw a message on the screen.
    • Using “while” loop to traverse the list.
      • Initialize the variable “temp” with the value of the variable “head”.
      • Assign null to the address of “head”.
    • Delete the value of “node” using “delete” operator.
    • Return the value of “temp” to the calling function.
  • Define the function named “push_back()” to insert the value at end of the list.
    • Make a call to the “appendNode()” function to insert the received value of variable “value” at end of the list.
  • Define the function named “push_front()” to insert the value at front of the list.
    • Declare a pointer variable “newNode” for the structure.
    • Initialize the variable “newNode” with the value of received variable “value”.
    • Using “if…else” condition check the list is empty.
      • If the list is empty then add a value into “head” pointer.
      • Otherwise, add a value into address of “newNode”.
    • Increment the “size” variable by “1”.

“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 append and display operations.
  • Make a call to function “pop_back()”, “pop_front()”, “push_front()”, and “push_back()” and display the values using a function “display()”.

Blurred answer
Students have asked these similar questions
The following is code for a disc golf program written in C++:   // player.h #ifndef PLAYER_H #define PLAYER_H   #include <string> #include <iostream>   class Player { private:     std::string courses[20]; // Array of course names     int scores[20];          // Array of scores     int gameCount;          // Number of games played   public:     Player();               // Constructor     void CheckGame(int playerId, const std::string& courseName, int gameScore);     void ReportPlayer(int playerId) const; };   #endif // PLAYER_H   // player.cpp #include "player.h" #include <iomanip>   Player::Player() : gameCount(0) {}   void Player::CheckGame(int playerId, const std::string& courseName, int gameScore) {     for (int i = 0; i < gameCount; ++i) {         if (courses[i] == courseName) {             // If course has been played, then check for minimum score             if (gameScore < scores[i]) {                 scores[i] = gameScore; // Update to new minimum…
In this assignment, you will implement a multi-threaded program (using C/C++) that will check for Prime Numbers and Palindrome Numbers in a range of numbers. Palindrome numbers are numbers that their decimal representation can be read from left to right and from right to left (e.g. 12321, 5995, 1234321). The program will create T worker threads to check for prime and palindrome numbers in the given range (T will be passed to the program with the Linux command line). Each of the threads works on a part of the numbers within the range. Your program should have some global shared variables: • numOfPrimes: which will track the total number of prime numbers found by all threads. numOfPalindroms: which will track the total number of palindrome numbers found by all threads. numOfPalindromic Primes: which will count the numbers that are BOTH prime and palindrome found by all threads. TotalNums: which will count all the processed numbers in the range. In addition, you need to have arrays…
How do you distinguish between hardware and a software problem? Discuss theprocedure for troubleshooting any hardware or software problem. give one reference with your answer.

Chapter 17 Solutions

Starting Out with C++ from Control Structures to Objects, Student Value Edition plus MyProgrammingLab with Pearson eText -- Access Card Package (8th 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