Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 18, 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
What is Windows Server and what is the Differences between Windows Server 2019 and the Windows Operating System that people have installed on their personal computer.
solve and show the tree on paper
I need help to resolve the following activity
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning