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
C++ Programing Create an application to keep track of a list of students and their grades in a data structures course using a linked list. You should use the C++ STL library. You need to create a class called student that contains 2 data members: name and grade and then create a linked list of objects of this class. The application should allow the user to add a student, remove a student, update the grade for a student, and search for a student. Please separate each file in your answer so it can be understood well.
card_t * moveCardBack (card t *head); The moveCardBack function will take the card in front of the pile and place it in the back. In coding terms, you are taking the head of the linked list and moving it to the end. The function has one parameter which is the head of the linked list. After moving the card to the back, the function returns the new head of the linked list.
Concatenate Map This function will be given a single parameter known as the Map List. The Map List is a list of maps. Your job is to combine all the maps found in the map list into a single map and return it. There are two rules for addingvalues to the map. You must add key-value pairs to the map in the same order they are found in the Map List. If the key already exists, it cannot be overwritten. In other words, if two or more maps have the same key, the key to be added cannot be overwritten by the subsequent maps.   Signature:   public static HashMap<String, Integer> concatenateMap(ArrayList<HashMap<String, Integer>> mapList) Example: INPUT: [{b=55, t=20, f=26, n=87, o=93}, {s=95, f=9, n=11, o=71}, {f=89, n=82, o=29}]OUTPUT: {b=55, s=95, t=20, f=26, n=87, o=93} INPUT: [{v=2, f=80, z=43, k=90, n=43}, {d=41, f=98, y=39, n=83}, {d=12, v=61, y=44, n=30}]OUTPUT: {d=41, v=2, f=80, y=39, z=43, k=90, n=43} INPUT: [{p=79, b=10, g=28, h=21, z=62}, {p=5, g=87, h=38}, {p=29,…
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