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

Overloaded [ ] operator

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.

“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()” used to return the current value of the “size”.
  • Define the function for “operator[]” overloading  used to access the list values using subscript.
    • Declare the pointer variable “p” for the structure named “ListNode” and initialize the “head” pointer into “p”.
    • Declare the variable named “pos” in type of integer and initialize it to be “0”.
    • Using “if” condition, check the value of received variable “sub” is greater than or equal to value of “size”.
      • If it is “true”, throw an exception message on the screen.
    • Using “while” loop to traverse the list from first to last node.
    • Return the value of the node in the given position of the list.
  • 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.

“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, append, and display operations.
  • Using “for” loop to display the list values using index position.
  • Using the subscript “[]” operator add a value “99” into the list and display the list values using “for” loop.

Blurred answer
Students have asked these similar questions
class Node:  def __init__(self, e, n):    self.element = e    self.next = n class LinkedList:    def __init__(self, a):  #  Design the constructor based on data type of a. If 'a' is built in python list then  #  Creates a linked list using the values from the given array. head will refer  #  to the Node that contains the element from a[0]  #  Else Sets the value of head. head will refer  # to the given LinkedList   # Hint: Use the type() function to determine the data type of a    self.head = None    # To Do        # Count the number of nodes in the list  def countNode(self):    # To Do        # Print elements in the list  def printList(self):    # To Do       # returns the reference of the Node at the given index. For invalid index return None.  def nodeAt(self, idx):    # To Do
struct node{ int a; struct node * nextptr; };   Write two functions. One for inserting new values to a link list that uses the given node structure. void insert(struct node **head, int value); Second function is called to count the number of even numbers in the link list. It returns an integer that represents the number of even numbers. int countEvenNumbers(struct node *head); Write a C program that reads a number of integers from the user and insert those integers into a link list (use insert function). Later pass the head pointer of this link list to a function called countEvenNumbers. This function counts and returns the number of even numbers in the list. The returned value will be printed on the screen. Note 1: Do not modify the function prototypes.   Sample Input1:          Sample Output1: 45 23 44 12 37 98 33 35 -1 3   Sample Input2:          Sample Output2: 11 33 44 21 22 99 123 122 124 77 -1 4
Lab 19 Building a linked list Start this lab with the code listed below. The LinkedList class defines the rudiments of the code needed to build a linked list of Node objects. You will first complete the code for its addFirst method. This method is passed an object that is to be added to the beginning of the list. Write code that links the passed object to the list by completing the following tasks in order: 1. Create a new Node object. 2. Make the data variable in the new Node object reference the object that was passed to addELKEt, 3. Make the next variable in the new Node object reference the object that is currently referenced in variable first. 4, Make variable first reference the new Node. Test your code by running the main method in the LinkedLiatBunner class below. Explain, step by step, why each of the above operations is necessary. Why are the string objects in the reverse order from the way they were added? public class LinkedList private Node first; public LinkedList () {…
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education