Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134521176
Author: SAVITCH
Publisher: PEARSON
Question
Book Icon
Chapter 15, Problem 12PP
Program Plan Intro

Priority Queue

Program Plan:

queue.h:

  • Include required header files.
  • Create a namespace named “queuesavitch”.
    • Create a structure.
      • Declare a variable and a pointer.
    • Declare a class “Queue”.
      • Inside “public” access specifier.
        • Declare the constructor and destructor.
        • Declare the functions “add ()”, “remove ()”, “empty ()”.
      • Inside the “protected” access specifier,
        • Create a pointer “front” that points to the head of the linked list.
        • Create a pointer “back” that points to the other end of the linked list.

queue.cpp:

  • Include required header files.
  • Create a namespace named “queuesavitch”.
    • Declare the constructor.
    • Inside the parameterized constructor,
      • Declare the temporary point that moves through the nodes from front to the back.
      • Create a pointer “temp_ptr_new” that is used to create new nodes.
      • Create new nodes.
      • Assign “emp_ptr_old->link” to “temp_ptr_old”.
      • Using while condition “temp_ptr_old != NULL”.
        • Create a new node.
        • Assign the temporary old data to the new pointer.
        • Assign NULL to the link of temporary new pointer.
        • Assign “temp_ptr_new” to “back->link”.
        • Assign “temp_ptr_new” to “back”.
        • Assign “temp_ptr_old->link” to “temp_ptr_old”.
    • Give definition for the destructor.
      • Declare a variable “next”.
      • Do till the queue is empty.
        • Remove the items using “remove ()” method.
    • Give definition for “empty ()” to check if the queue is empty or not.
      • Return “back == NULL”.
    • Give definition for the method “add ()”.
      • Check if the queue is empty.
        • Create a new node.
        • Assign the item to the data field.
        • Make the front node as null.
        • Assign front as the back.
      • Else,
        • Create a new pointer.
        • Create a new node.
        • Assign the item to “temp_ptr->data”.
        • Assign “NULL” to “temp_ptr->link”.
        • Assign temporary pointer to the link.
        • Assign temporary pointer to the back.
    • Give definition for the method “remove ()”.
      • Check if the queue is empty.
        • Print the message.
      • Store the value into the variable “result”.
      • Create an object “discard” for the pointer “QueueNodePtr”.
      • Assign “front” to “discard”.
      • Assign the link of front to “front”.
      • Check if the front is null.
        • Assign null to the back.
      • Delete the object “discard”.
      • Return “result”.

PriorityQueue.h:

  • Include required header files.
  • Create a namespace named “queuesavitch”.
    • Declare a class “PriorityQueue”.
      • Inside “public” access specifier,
        • Declare the constructor and destructor.
        • Declare the virtual function.

PriorityQueue.cpp:

  • Include required header files.
  • Create a namespace named “queuesavitch”.
    • Declare the constructor and destructor.
    • Give function to remove items.
      • Check if the queue is empty.
        • Print the messge.
      • Assing “front->data” to “smallest”.
      • Assign “NULL” to “nodeBeforeSmallest”.
      • Assign “front” to “previousPtr”.
      • Assign the link of front to “current”.
      • Using while condition “current != NULL”,
        • Check if the data is smaller.
          • Assing “current->data” to “smallest”.
          • Assign “previousPtr” to “nodeBeforeSmallest”.
        • Assign “current” to “previousPtr”.
        • Assign “current->link” to “current”.
      • Create an object “discard”.
      • Check if the node is null.
        • Assign “front” to “discard”.
        • Assign the link of front to “front”.
      • Check if the link is equal to back.
        • Assign “back” to “discard”.
        • Assign “nodeBeforeSmallest” to “back”.
        • Assign “NULL” to “back->link”.
      • Else,
        • Assign “nodeBeforeSmallest->link” to “discard”.
        • Assign “discard->link” to “nodeBeforeSmallest->link”.
      • Check if front is equal to null.
        • Assign “NULL” to “back”.
      • Delete the object “discard”.
      • Return the smallest item.

main.cpp:

  • Include required header files.
  • Inside the “main ()” function,
    • Create an object for “PriorityQueue”.
    • Add the integers to the queue using “add ()” method.
    • While “(!que.empty())”,
      • Call the function “que.remove()”.
    • Declare a character variable.
    • Get a character.
    • Return the statement.

Blurred answer
Students have asked these similar questions
Which tool takes the 2 provided input datasets and produces the following output dataset? Input 1: Record First Last Output: 1 Enzo Cordova Record 2 Maggie Freelund Input 2: Record Frist Last MI ? First 1 Enzo Last MI Cordova [Null] 2 Maggie Freelund [Null] 3 Jason Wayans T. 4 Ruby Landry [Null] 1 Jason Wayans T. 5 Devonn Unger [Null] 2 Ruby Landry [Null] 6 Bradley Freelund [Null] 3 Devonn Unger [Null] 4 Bradley Freelund [Null] OA. Append Fields O B. Union OC. Join OD. Find Replace Clear selection
What are the similarities and differences between massively parallel processing systems and grid computing. with references
Modular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.

Chapter 15 Solutions

Problem Solving with C++ (10th Edition)

Knowledge Booster
Background pattern image
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
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
CMPTR
Computer Science
ISBN:9781337681872
Author:PINARD
Publisher:Cengage