Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 20, Problem 7PC
Program Plan Intro
Queue Converter
Program Plan:
DynIntQueue.h:
- Include required header files.
- Declare a class named “DynIntQueue”; inside the class,
- Inside the “private” access specifier,
- Create a structure named “QueueNode”.
- Declare a variable “value”.
- Create a pointer named “next”.
- Create two pointers named “front” and “rear”.
- Declare a variable “numItems”.
- Create a structure named “QueueNode”.
- Inside “public” access specifier,
- Declare constructor and destructor.
- Declare the functions “enqueue()”, “dequeue()”, “isEmpty()”, “isFull()”, and “clear()”.
- Inside the “private” access specifier,
DynIntQueue.cpp:
- Include required header files.
- Give definition for the constructor.
- Assign the values.
- Give definition for the destructor.
- Call the function “clear()”.
- Give function definition for “enqueue()”.
- Make the pointer “newNode” as null.
- Assign “num” to “newNode->value”.
- Make “newNode->next” as null.
- Check whether the queue is empty using “isEmpty()” function.
- If the condition is true then, assign “newNode” to “front” and “rear”.
- If the condition is not true then,
- Assign “newNode” to “rear->next”.
- Assign “newNode” to “rear”.
- Increment the variable “numItems”.
- Give function definition for “dequeue()”.
- Assign “temp” pointer as null.
- Check if the queue is empty using “isEmpty()” function.
- If the condition is true then print “The queue is empty”.
- If the condition is not true then,
- Assign the value of front to the variable “num”.
- Make “front->next” as “temp”.
- Delete the front value.
- Make temp as front.
- Decrement the variable “numItems”.
- Give function definition for “isEmpty()”.
- Assign “true” to a Boolean variable
- Check if “numItems” is true.
- If the condition is true then assign “false” to the variable.
- Return the Boolean variable.
- Give function definition for “clear()”.
- Declare a variable.
- Dequeue values from queue till the queue becomes empty using “while” condition.
- Declare a variable.
IntBinaryTree.h:
- Include required header files.
- Declare a class named “IntBinaryTree”. Inside the class,
- Inside the “private” access specifier,
- Give the structure declaration for the creation of node.
- Declare a variable
- Create two pointers named “left” and “right” to access the value left and right nodes respectively.
- Create a pointer named “root” to access the value of root node.
- Give function declaration for “insert ()”, “destroy_SubTree ()”, “delete_Node ()”, “make_Deletion ()”, “display_InOrder ()”, “display_PreOrder ()”, “display_PostOrder ()”, “copyTree ()”, and “setQueue ()”.
- Give the structure declaration for the creation of node.
- Inside “public” access specifier,
- Give the definition for constructor and destructor.
- Give function declaration for binary tree operations.
- Inside the “private” access specifier,
IntBinaryTree.cpp:
- Include required header files.
- Give definition for copy constructor.
- Give function definition for “insert()”.
- Check if “nodePtr” is null.
- If the condition is true then, insert node.
- Check if value of new node is less than the value of node pointer
- If the condition is true then, Insert node to the left branch by calling the function “insert()” recursively.
- Else,
- Insert node to the right branch by calling the function “insert()” recursively.
- Check if “nodePtr” is null.
- Give function definition for “insert_Node ()”.
- Create a pointer for new node.
- Assign the value to the new node.
- Make left and right node as null.
- Call the function “insert()” by passing parameters “root” and “newNode”.
- Give function definition for “destroy_SubTree()”.
- Check if the node pointer points to left node
- Call the function recursively to delete the left sub tree.
- Check if the node pointer points to the right node
- Call the function recursively to delete the right sub tree.
- Delete the node pointer.
- Check if the node pointer points to left node
- Give function definition for “search_Node()”.
- Assign false to the Boolean variable “status”.
- Assign root pointer to the “nodePtr”.
- Do until “nodePtr” exists.
- Check if the value of node pointer is equal to “num”.
- Assign true to the Boolean variable “status”
- Check if the number is less than the value of node pointer.
- Assign left node pointer to the node pointer.
- Else,
- Assign right node pointer to the node pointer.
- Check if the value of node pointer is equal to “num”.
- Return the Boolean variable.
- Give function definition for “remove()”.
- Call the function “delete_Node()”
- Give function definition for “delete_Node()”
- Check if the number is less than the node pointer value.
- Call the function “delete_Node()” recursively.
- Check if the number is greater than the node pointer value.
- Call the function “delete_Node()” recursively.
- Else,
- Call the function “make_Deletion()”.
- Check if the number is less than the node pointer value.
- Give function definition for “make_Deletion()”
- Create pointer named “tempPtr”.
- Check if the “nodePtr” is null.
- If the condition is true then, print “Cannot delete empty node.”
- Check if right node pointer is null.
- If the condition is true then,
- Make the node pointer as the temporary pointer.
- Reattach the left node child.
- Delete temporary pointer.
- If the condition is true then,
- Check is left node pointer is null
- If the condition is true then,
- Make the node pointer as the temporary pointer.
- Reattach the right node child.
- Delete temporary pointer.
- If the condition is true then,
- Else,
- Move right node to temporary pointer
- Reach to the end of left-Node using “while” condition.
- Assign left node pointer to temporary pointer.
- Reattach left node sub tree.
- Make node pointer as the temporary pointer.
- Reattach right node sub tree
- Delete temporary pointer.
- Give function definition for “display_InOrder()”.
- Check if the node pointer exists.
- Call the function “display_InOrder()” recursively.
- Print the value
- Call the function “display_InOrder()” recursively.
- Check if the node pointer exists.
- Give function definition for “display_PreOrder()”.
- Print the value.
- Call the function “display_PreOrder()” recursively.
- Call the function “display_PreOrder()” recursively.
- Give function definition for “display_PostOrder()”.
- Call the function “display_PostOrder()” recursively.
- Call the function “display_PostOrder()” recursively.
- Print value.
- Give function definition for assignment operator.
- Call the function “destroy_SubTree()”
- Call the copy constructor.
- Return the pointer.
- Copy tree function is called by copy constructor and assignment operator function
- Create a pointer named “newNode”.
- Check if “nPtr” is not equal to null
- Allocate memory dynamically.
- Assign pointer value to the new node.
- Call the function “copyTree()” by passing “nPtr” of left.
- Call the function “copyTree()” by passing “nPtr” of right
- Return the new node.
- Function definition for “setQueue()”.
- Check if the pointer “nodePtr” exists.
- Call the function “setQueue()” recursively by passing the left node.
- Call the function “setQueue()” recursively by passing the right node.
- Call the function “enqueue()” recursively by passing the left node.
Main.cpp:
- Include required header files.
- Inside “main()” function,
- Declare a variable “value” and assign it to 0.
- Create an object “intBT” for “IntBinaryTree” class.
- Insert 5 values using “insert_Node()” function.
- Display all the values by using the function “display_InOrder()”.
- Create an object “iqueue” for “DynIntQueue” class.
- Load the address to the pointer “qPtr”.
- Pass this pointer to the function “treeToQueue ()”.
- Do until the queue is not empty.
- Declare a variable.
- Call the function “dequeue()”.
- Display the value.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
PYTHON: LINKED LISTS
PYTHON: LINKED LIST
IN PYTHON THANK YOU
Chapter 20 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Ch. 20.1 - Prob. 21.1CPCh. 20.1 - Prob. 21.2CPCh. 20.1 - Prob. 21.3CPCh. 20.1 - Prob. 21.4CPCh. 20.1 - Prob. 21.5CPCh. 20.1 - Prob. 21.6CPCh. 20.2 - Prob. 21.7CPCh. 20.2 - Prob. 21.8CPCh. 20.2 - Prob. 21.9CPCh. 20.2 - Prob. 21.10CP
Ch. 20.2 - Prob. 21.11CPCh. 20.2 - Prob. 21.12CPCh. 20 - Prob. 1RQECh. 20 - Prob. 2RQECh. 20 - Prob. 3RQECh. 20 - Prob. 4RQECh. 20 - Prob. 5RQECh. 20 - Prob. 6RQECh. 20 - Prob. 7RQECh. 20 - Prob. 8RQECh. 20 - Prob. 9RQECh. 20 - Prob. 10RQECh. 20 - Prob. 11RQECh. 20 - Prob. 12RQECh. 20 - Prob. 13RQECh. 20 - Prob. 14RQECh. 20 - Prob. 15RQECh. 20 - Prob. 16RQECh. 20 - Prob. 17RQECh. 20 - Prob. 18RQECh. 20 - Prob. 19RQECh. 20 - Prob. 20RQECh. 20 - Prob. 21RQECh. 20 - Prob. 22RQECh. 20 - Prob. 23RQECh. 20 - Prob. 24RQECh. 20 - Prob. 25RQECh. 20 - Prob. 1PCCh. 20 - Prob. 2PCCh. 20 - Prob. 3PCCh. 20 - Prob. 4PCCh. 20 - Prob. 5PCCh. 20 - Prob. 6PCCh. 20 - Prob. 7PCCh. 20 - Prob. 8PC
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
- List=(Two,three,four,five,six,seven and eight)Perform the following operations on the given list above utilizing c++ code.a)enqueue() i.e add the list above to the queue.b)dequeue() i.e delete or remove any two elements in the queue in a) above.c)ISfull() i.e Determine if the queue is Full.d)Isempty() i.e Determine if the queue is Empty.e)print() i.e print the elements of the Queue.arrow_forward8-Write a procedure that takes a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in right to left order or left to right. For example, ( leaves '((1 2) ((3 4) 5 )) returns (1 2 3 4 5) or (5 4 3 2 1). Then, Manually trace your procedure with the provided example.arrow_forward3- Write a program that randomly generates 10 numbers (between 1 and 8), inserts into queue and then finds how many distinct elements exist in the queue. Example 1: Example 2: Queue: 2 40 3 3 2 18 4 18 18 3 Queue: 1 1 4 33 16 16 4 16 4 Output: 5 Output: 4 Notes: • You must use ONLY queue data structure. Don't use other different data structures like string or normal (pure) array or stack or array list. • Don't write any other method in the Qeueu class. All methods must be written in the main program.arrow_forward
- ListQueue Node Node Node front= next next= next = nul1 rear = data "Thome" data "Abreu" data - "Jones" size - 3 The above is a queue of a waiting list. The ListQueue has a node (front) to record the address of the front element of a queue. It also has another node (rear) to record the address of the tail element of a queue. 4. How do you push a node with data, "Chu" to the above queue? front.next = new Node("Chu", front); a. b. front = new Node ("Chu", front) rear = new Node("Chu", rear) с. d. = new Node ("Chu", rear.next) rear Describe the reason of your choice. Your answer is (a, b, c, or d) Will the push action take time in 0(1) or 0(n)? Next Page Type here to searcharrow_forwardListQueue Node Node Node front- next = next next - nul1 data - "Jones" rear data "Thome" data - "Abreu" size = 3 The above is a queue of a waiting list. The ListQueue has a node (front) to record the address of the front element of a queue. It also has another node (rear) to record the address of the tail element of a queue. 3. How do you pop a node from the above queue? a. front.next = front; b. front = front.next с. rear = rear.next d. rear.next = rear Describe the reason of your choice. Your answer is (a, b, c or d) will the pop action take time in 0(1) or 0(n)? Next Page P Type here to searcharrow_forwardMultiple choice in data structures void doo(node<int>*root){ if(root !=0) { node<int>*p=root; while(root->next!=0) root=root->next; p->data=root->data; } What is this code do? a. swap the first item with the last item in the linked list b. set the first item in the linked list as the last item c. doesn't do anything because the root parameter is passed by value d. change the root item in the binary tree with the farthest leaf itemarrow_forward
- JAVA :arrow_forward24If you enqueue 5 elements into an empty queue, and then perform the isEmpty operation 5 times, the queue will be empty again.arrow_forwardTOPICS: LIST/STACK/QUEUE Write a complete Java program about Appointment schedule(anything). Your program must implements the linked list The program should have the following basic operations of list, which are: a) Add first, in between and last b) Delete first, in between and last c) Display all data The program should be an interactive program that allow user to choose type of operation.arrow_forward
- LINKED LIST IN PYTHON Create a program using Python for the different operations of a Linked List. Your program will ask the user to choose an operation. 1. Create a List -Ask the user how many nodes he/she wants. -Enter the element/s -Display the list -Back to menu 2. Add at beginning -Ask for the element to be inserted. -Display the list -Back to menu 3. Add after -Ask for the element to be inserted. -Ask for the position AFTER which the element is to be inserted -Display the list -Back to menu 4. Delete -Ask for the element (data) to be deleted -If found, delete the node with that data. -If multiple values, delete only the first element found -If not found, display that the element is not found -back to menu 5. Display -Display the list 6. Count -Display the number of elements 7. Reverse -Reverse the list and display it 8. Search -Ask the user for the element (data) to be searched -Display a message if the element is found or not 9. Quit -Exits the programarrow_forwardjava data structure Queue: Q4: A program performs the following operations on an empty queue Q: Q.enqueue(24) Q.enqueue(74) Q.enqueue(34) Q.first() Q.dequeue() Q.enqueue(12) Q.dequeue() Please show the queue contents at the end of these operations. Clearly show the front of the queue.arrow_forwardC# language Write a program that creates a Queue or Stack (your choice) that represents a list of work orders. This program should use loop, allowing the user to push and pop items on the stack / queue. The program should also allow the user to print all the items in the stack / queue to the console.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 Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning