Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 18, Problem 11PC
Program Plan Intro
List Search using template
Program Plan:
“IntList.h”:
- Include the required specifications into the program.
- Define a class template named “IntList”.
- Declare the member variables “value” and “*next” in structure named “ListNode”.
- Declare the constructor, copy constructor, destructor, and member functions in the class.
- Define a copy constructor named “IntList()” as a template which takes an address of object for the “IntList” class as “const”.
- Declare a structure pointer variable “nodePtr” and initialize it to be “nullptr”.
- Assign “obj.head” value into the received variable “nodePtr”.
- Make a “while” loop to copy the received values into “nodePtr”.
- Make a call to “appendNode()” to insert values to “nodePtr” and initialize address of “next” into “nodePtr”.
- Define a function named “appendNode()”as a template to insert the node at end of the list.
- Declare the structure pointer variables “newNode” and “dataPtr” for the structure named “ListNode”.
- Assign the value “num” to the variable “newNode” and assign null to the variable “newNode”.
- 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”.
- Define a function named “print()”as a template 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()”as a template 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.
- Define a function named “deleteNode()” as a template 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.
- Define a function named “reverse()”as a template to reverse the values in a list.
- Declare the pointer variables “newNode”, “newHead”, “nodePtr”, and “tempPtr” for the structure named “ListNode”.
- Initialize the variable “nodePtr” with the value of the variable “head”.
- Define a “while” loop to allocate “newNode” variable.
- Create a “newNode” for the structure “ListNode”.
- Store the value of “nodePtr” into “newNode” and assign address as null to the “newNode” pointer.
- Using “if…else” condition swap the values of “newHead” and “newNode”.
- Assign the address of “next” node into “nodePtr”.
- Initialize the variable “head” with the value of the variable “newHead”.
- Define a function named “destroy()”as a template 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 a function named “insert()” as a template with the arguments of “value” and “pos” to insert a value at specified location.
- Declare a pointer variable “newNode” for the structure “ListNode”.
- Assign the value of received variable “value” into “newNode” value and make address of “newNode” into “nullptr”.
- Using “if” condition to check whether the list is empty or not.
- If list is empty, initialize the variable “head” with the value of the variable “newNode”.
- Using “if” condition to insert the value of received variable “pos” into the list.
- Assign the “head” node into address of “newNode” .
- Initialize the variable “head” with the value of the variable “newNode”.
- Using “while” loop to insert the value at specified position in the list.
- Define a function named “removeByPos()” as a template with an argument “pos” to remove a value at specified position in list.
- Declare a pointer variable “temp” for the structure “ListNode”.
- Using “if” condition, check whether the list is “empty” or not. If the list is empty, return “null” to “main()” function.
- Otherwise, using “while” loop to traverse the list to find the “pos” in list.
- Using “if…else” condition, check whether the received value of “pos” is value of “head” or not.
- If the condition is true, delete “head” node from the list.
- Otherwise, assign pointers to the next node of removable value then delete the node using “delete” operator.
- Define the destructor to call the member function “destroy()” in the list.
- Define a function named “search()” as a template to find the value of “num” in the list.
- Declare a variable “count” in type of “int”.
- Declare a structure pointer variable “*dataPtr” for the structure named “ListNode”.
- Define a “while” loop to search the value in the list.
- Using “if…else” statement, check the value of “dataPtr” in the list.
- If the condition is “true”, return the value “count” variable.
- Otherwise, point the “next” value of “dataPtr” and then increment the value of “count” variable.
- Using “if…else” statement, check the value of “dataPtr” in the list.
- Return a value “-1” to the function call.
“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 and append operations.
- Make a call to “print()” function to display the list on the screen.
- Make a call to “search()” function to find the value in the list.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
C++ function Linked list
Write a function, to be included in an unsorted linked list class, called replaceItem, that will receive two parameters, one called olditem, the other called new item. The function will replace all occurrences of old item with new item (if old item exists !!) and it will return the number of replacements done.
struct Node {
int data;
Node * next;
};
Node • head;
a. Write a function named addNode that takes in a variable of type int and inserts it at the head of
the list.
b. Write a function named removeNode that removes a node at the head of the list.
Data structures
find_color(colors:set, values:list) -> list
The function will have two parameters. The first parameter is a set of strings known as Colors. A second parameter is a list of tuple-2 known as Values. Colors will contain a set of randomly selected colors. Values will contain a list of tuples of size 2. Each tuple will contain color (str) and a number (int). The function should look at each tuple in Values. For each tuple, add the number (the second value in the tuple) to a list of numbers if the color in the tuple (the first value in the tuple) is in Colors. In other words, find all tuples that have a color in the Colors and add the tuples numbers to a list. Finally, the function should return the list of numbers collected in the order they are found in the values list.
Example:
Colors: {'black', 'pink', 'yellow'}
values: [('green', 100), ('yellow', 13), ('red', 6)]
Expected: [13]
Colors: {'yellow'}
values: [('black', 54), ('pink', 5)]
Expected: []
Colors: {'black',…
Chapter 18 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Ch. 18.1 - Prob. 18.1CPCh. 18.1 - Prob. 18.2CPCh. 18.1 - Prob. 18.3CPCh. 18.1 - Prob. 18.4CPCh. 18.2 - Prob. 18.5CPCh. 18.2 - Prob. 18.6CPCh. 18.2 - Prob. 18.7CPCh. 18.2 - Prob. 18.8CPCh. 18.2 - Prob. 18.9CPCh. 18.2 - Prob. 18.10CP
Ch. 18 - Prob. 1RQECh. 18 - Prob. 2RQECh. 18 - Prob. 3RQECh. 18 - Prob. 4RQECh. 18 - Prob. 5RQECh. 18 - Prob. 6RQECh. 18 - Prob. 7RQECh. 18 - Prob. 8RQECh. 18 - Prob. 9RQECh. 18 - Prob. 10RQECh. 18 - Prob. 11RQECh. 18 - Prob. 12RQECh. 18 - Prob. 13RQECh. 18 - Prob. 14RQECh. 18 - Prob. 15RQECh. 18 - Prob. 16RQECh. 18 - Prob. 17RQECh. 18 - Prob. 18RQECh. 18 - Prob. 19RQECh. 18 - Prob. 20RQECh. 18 - Prob. 21RQECh. 18 - Prob. 22RQECh. 18 - Prob. 23RQECh. 18 - Prob. 24RQECh. 18 - Prob. 25RQECh. 18 - T F The programmer must know in advance how many...Ch. 18 - T F It is not necessary for each node in a linked...Ch. 18 - Prob. 28RQECh. 18 - Prob. 29RQECh. 18 - Prob. 30RQECh. 18 - Prob. 31RQECh. 18 - Prob. 32RQECh. 18 - Prob. 33RQECh. 18 - Prob. 34RQECh. 18 - Prob. 35RQECh. 18 - Prob. 1PCCh. 18 - Prob. 2PCCh. 18 - Prob. 3PCCh. 18 - Prob. 4PCCh. 18 - Prob. 5PCCh. 18 - Prob. 6PCCh. 18 - Prob. 7PCCh. 18 - List Template Create a list class template based...Ch. 18 - Prob. 9PCCh. 18 - Prob. 10PCCh. 18 - Prob. 11PCCh. 18 - Prob. 12PCCh. 18 - Prob. 13PCCh. 18 - Prob. 14PCCh. 18 - Prob. 15PC
Knowledge Booster
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
- C++ Programming Language ::::::: Redo the same functions this time as nonmember functions please : NOTE: You can add only one function into the linked list class get_at_position which will return value of element at given position. 1) Insert before tail : Insert a value into a simply linked list, such that it's location will be before tail. So if a list contains {1, 2, 3}, insert before tail value 9 is called, the list will become {1, 2, 9, 3}. 2) Insert before value : Insert a value into a simply linked list, such that it's location will be before a particular value. So if a list contains {1, 2, 3}, insert before 2 value 9 is called, the list will become {1, 9, 2, 3}. 3)Count common elements : Count common values between two simply linked lists.So if a list1 contains {1, 2, 3, 4, 5}, and list2 contains {1, 3, 4, 6}, number of common elements is 3. 4) Check if sorted : Check if elements of simply linked lists are sorted in ascending order or not.So if a list contains {1, 3, 7, 8, 9}…arrow_forwardRemove Duplicates This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list.arrow_forwardQ3 Racket Number Function Suppose that into the Racket evaluator we enter the definition: (define dozen 12) Give the value of the Racket expression: (number? dozen). Note that the value should be consistent with the Racket language. Enter your answer here Save Answer Q4 Racket Empty Lists Give two ways to write a Racket expression of which the value is a one-element list with the empty list as its element. Enter your answer here Save Answerarrow_forward
- struct remove_from_front_of_dll { // Function takes no parameters, removes the book at the front of a doubly // linked list, and returns nothing. void operator()(const Book& unused) { //// TO-DO (13) |||| // Write the lines of code to remove the book at the front of "my_dll", // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. ///// END-TO-DO (13) //// } std::list& my_dll; };arrow_forwardc++ data structures, linked list. write a function that rearrange the linked list of integers such as the odd items comes first don''t use different data structures.arrow_forwardConcatenate 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,…arrow_forward
- Domino cycledef domino_cycle(tiles):A single domino tile is represented as a two-tuple of its pip values, such as (2,5) or (6,6). This function should determine whether the given list of tiles forms a cycle so that each tile in the list ends with the exact same pip value that its successor tile starts with, the successor of the last tile being the first tile of the list since this is supposed to be a cycle instead of a chain. Return True if the given list of domino tiles form such a cycle, and False otherwise. tiles Expected result [(3, 5), (5, 2), (2, 3)] True [(4, 4)] True [] True [(2, 6)] False [(5, 2), (2, 3), (4, 5)] False [(4, 3), (3, 1)] Falsearrow_forward5. List Member Deletion Modify the list class you created in the previous programming challenges by adding a function to remove an item from the ist and by adding a destructor: void zemove (double x) : Linkedtist (02 Test the class by adding a sequence of instructions that mixes operations for adding items, removing items, and printing the list.arrow_forwardLINKED LIST IMPLEMENTATION Linked list Write a C++ program to implement insertion, deletion, and display operations in a Linked List Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, member variable names, and function names should be the same as specified in the problem statement.The class Node has the following member variable Datatype Variable Usage int data to store data Node* next to store the next node Define the following public member functions in the class LinkedList. Member function Function description void insertNode(int value) This function inserts the data into the linked list at the end void deleteNode(int value) This function deletes the node from the linked list void display() This function is used to display the nodes in the linked list In the main() function, read inputs and call the functions of the LinkedList class based on the inputs. Note: If the Linked list is empty while…arrow_forward
- 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 4arrow_forwardProgramming Language: Python Limitations: Not allowed to use sklearn libraryarrow_forwarda) Write a function to get the value of the Nu node in a Linked List. [Note: The first (N=1) item in the list means the item at index 0.] It takes two parameters: the list or its head, and N. Return False if the list has fewer than N elements. The Linked List structure supports the following function. def getlead(self): return selt.head # it points to a Node structure The Node structure supports the following functions. def getData(self): return self.data # it returns the value stored in the Node def getNext(self): return self next # it points to the next Node b) Write a function that counts the number of times a given integer occurs in a Linked List. Assume similar structures as defined in 1.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr