Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter 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
Assignment 05 PART 1 Queues - Note Part 1 is a separate deliverable: Write a class with a main method that uses a priority queue to store a list of prioritized chores. This is all the information that is provided. The rest is up to you. You may decide how to store your chores and how to store your priorities. YOU MUST WRITE your own Queue class – do not use the Java Library, Queue Class.
Part 3: Building a Point of Sales (POS) linked list data structure. In a POS system, a transaction is based on items purchased by the customer. The following is an example of a customer transaction receipt, where the prices shown in the receipt are GST inclusive.   Write a linked list classes (one class for Node and another class for List), which store the items in the transaction. Test the classes by printing the items in the linked list and show the total price of the transaction. The following listing is the sample output for your reference:   ============================== BC Items                                    Price ============================== 10 Pagoda Gnut 110g              3.49 11 Hup Seng Cream Cracker   4.19 12 Yit Poh 2n1 Kopi-o              7.28 13 Zoelife SN & Seed               5.24 14 Gatsby S/FO Wet&Hard    16.99 15 GB W/G U/Hold 150g         6.49 ============================== Total (GST Incl.)                        43.68…
C++ programming design a class to implement a sorted circular linked list. The usual operations on a circular list are:Initialize the list (to an empty state). Determine if the list is empty. Destroy the list. Print the list. Find the length of the list. Search the list for a given item. Insert an item in the list. Delete an item from the list. Copy the list. Write the definitions of the class circularLinkedList and its member functions (You may assume that the elements of the circular linked list are in ascending order). Also, write a program to test the operations of your circularLinkedList class. NOTE: The nodes for your list can be defined in either a struct or class. Each node shall store int values.

Chapter 15 Solutions

Problem Solving with C++ (9th 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