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 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
Question: struct node  {  char *name;  int marks;  node *next;  };  By using the above declaration for the Linked List, write a program in C++ to display the summary report regarding the pass/fail ratio of the subject Data Structure for the class BSI-3 that have only 15 students.  The required Report Summary for the exam will be like wise:  The No. of A grades........???  The No. of B grades........???  And so on by applying the grading criteria of COMSATS-University Islamabad. Question: struct node  {  char *name;  int marks;  node *next;  };  By using the above declaration for the Linked List, write a program in C++ to display the summary report regarding the pass/fail ratio of the subject Data Structure for the class BSI-3 that have only 15 students.  The required Report Summary for the exam will be like wise:  The No. of A grades........???  The No. of B grades........???  And so on by applying the grading criteria of COMSATS-University Islamabad.
Write a struct ‘Student’ that has member variables: (string) first name, (int) age and (double) fee. Create a sorted (in ascending order according to the age) linked list of three instances of the struct Student. The age of the students must be 21, 24 and 27. Write a c++ function to insert the new instances (elements of the linked list), and insert three instance whose member variable have age (i) 20, (ii) 23 and (iii) 29. Write a c++ function to remove the instances from the linked list, and remove the instances whose member variables have age (i) 21 and (ii) 29. Write a c++ function to count the elements of the linked list. Write a c++ function to search the elements from the linked list, and implement the function to search an element whose age is 23. Write a c++ function to print the linked list on the console.
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