Question

****NEED CODED IN C++**** You are to generate a list of customers to serve based on...

****NEED CODED IN C++****

You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority which is the largest number. Priorities of equal value are first come first served as listed in the input file. You input is a collection of customer IDs and priority, one pre line. You are to read in the data and insert new data into a sorted link list. The link list is sorted by priority.

A customer may need an update on his order. He may submit a request a second time in order to change his priority level. If so, you will search the link list, change the priority and adjust the new order in the sorted link list.

Input file: Priority Queue.txt

Output: List of ID’s with priorities in priority order

   Example input:

1345 4

8243 1

   Example output:

Customer Processing Order

Customer ID Priority

4124 5
1345 4

          …. ....


Restrictions: Use an ordered link list for the data structure and a nice formatted print out.

Input file (Priority Queue.txt):

1432 2
8234 3
2124 5
8123 2
1314 2
1432 4
7141 3
7123 4
5523 1
6543 2
1731 5
3813 4
7213 5
3318 5
7213 3
7131 2
8882 3
9974 1
7221 3
7342 4
5523 3
3113 5
7002 4
9769 1
3145 5
7145 3
8834 2
9123 4
7878 1
7588 4
2025 1
6069 3
2025 3

Homework Answers

Answer #1

Algorithm :
PUSH(HEAD, DATA, PRIORITY)
Step 1: Create new node with DATA and PRIORITY
Step 2: Check if HEAD has lower priority. If true follow Steps 3-4 and end. Else goto Step 5.
Step 3: NEW -> NEXT = HEAD
Step 4: HEAD = NEW
Step 5: Set TEMP to head of the list
Step 6: While TEMP -> NEXT != NULL and TEMP -> NEXT -> PRIORITY > PRIORITY
Step 7: TEMP = TEMP -> NEXT
[END OF LOOP]
Step 8: NEW -> NEXT = TEMP -> NEXT
Step 9: TEMP -> NEXT = NEW
Step 10: End

POP(HEAD)
Step 2: Set the head of the list to the next node in the list. HEAD = HEAD -> NEXT.
Step 3: Free the node at the head of the list
Step 4: End

PEEK(HEAD):
Step 1: Return HEAD -> DATA
Step 2: End

// C++ code to implement Priority Queue
// using Linked List
#include <bits/stdc++.h>
using namespace std;

// Node
typedef struct node
{
   int data;

   // Lower values indicate
   // higher priority
   int priority;

   struct node* next;

} Node;

// Function to create a new node
Node* newNode(int d, int p)
{
   Node* temp = (Node*)malloc(sizeof(Node));
   temp->data = d;
   temp->priority = p;
   temp->next = NULL;

   return temp;
}

// Return the value at head
int peek(Node** head)
{
   return (*head)->data;
}

// Removes the element with the
// highest priority form the list
void pop(Node** head)
{
   Node* temp = *head;
   (*head) = (*head)->next;
   free(temp);
}

// Function to push according to priority
void push(Node** head, int d, int p)
{
   Node* start = (*head);

   // Create new Node
   Node* temp = newNode(d, p);

   // Special Case: The head of list has
   // lesser priority than new node. So
   // insert newnode before head node
   // and change head node.
   if ((*head)->priority > p)
   {
      
       // Insert New Node before head
       temp->next = *head;
       (*head) = temp;
   }
   else
   {
      
       // Traverse the list and find a
       // position to insert new node
       while (start->next != NULL &&
           start->next->priority < p)
       {
           start = start->next;
       }

       // Either at the ends of the list
       // or at required position
       temp->next = start->next;
       start->next = temp;
   }
}

// Function to check is list is empty
int isEmpty(Node** head)
{
   return (*head) == NULL;
}

// Driver code
int main()
{
  
   // Create a Priority Queue
   Node* pq = newNode(1432 ,2);
push(&pq, 8234 ,3);
push(&pq, 2124 ,5);
push(&pq, 8123 ,2);
push(&pq, 1314 ,2);
push(&pq, 1432 ,4);
push(&pq, 7141 ,3);
push(&pq, 7123 ,4);
push(&pq, 5523 ,1);
push(&pq, 6543 ,2);
push(&pq, 1731 ,5);
push(&pq, 3813 ,4);
push(&pq, 7213 ,5);
push(&pq, 3318 ,5);
push(&pq, 7213 ,3);
push(&pq, 7131 ,2);
push(&pq, 8882 ,3);
push(&pq, 9974 ,1);
push(&pq, 7221 ,3);
push(&pq, 7342 ,4);
push(&pq, 5523 ,3);
push(&pq, 3113 ,5);
push(&pq, 7002 ,4);
push(&pq, 9769 ,1);
push(&pq, 3145 ,5);
push(&pq, 7145 ,3);
push(&pq, 8834 ,2);
push(&pq, 9123 ,4);
push(&pq, 7878 ,1);
push(&pq, 7588 ,4);
push(&pq, 2025 ,1);
push(&pq, 6069 ,3);
push(&pq, 2025, 3);

   while (!isEmpty(&pq))
   {
       cout << " " << peek(&pq);
       pop(&pq);
   }
   return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
create a function that takes a dictionary and returns a list of int. The list should...
create a function that takes a dictionary and returns a list of int. The list should appear in decreasing order based on the sum of number in each dictionary. def total_num(dict1): #Code here input = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':3}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} output = [1,5,3,4,2] 1: 38 2: 5 3: 8 4: 6 5: 15 1>5>3>4>2
Python pls create a function called search_position. This function returns a list of 2 tuples and...
Python pls create a function called search_position. This function returns a list of 2 tuples and the number should be start highest number. The first index is the number, and second are list of 2 tuples that sorted by position in alphabetical order: The first index will be position and second index will be champion's name(This also should be sorted by alphabetical order). team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
For C++: (Use only conditions, loops, user defined functions and arrays) Suppose you had data in...
For C++: (Use only conditions, loops, user defined functions and arrays) Suppose you had data in a file (input.txt) like this 1 2 6 11 4 8 12 3 16 5 6 14 ……… Find the multiples of 2 in order and store it in the output file (output.txt) like this 2 4 .......... The number 6 is in the wrong position so the order was disturbed. 6 only comes after 4. Therefore 6 and the other multiples of 2...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
c) A function named tallied_data() that takes in a nested list (Use this on the data...
c) A function named tallied_data() that takes in a nested list (Use this on the data from part 1, but it should be able to be used to solve similar problems), indices for two columns and returns a tallied list. The inputs are: i) a nested list, ii) an index for the ‘reference column’/ ‘category’ (col_ref) iii) another index for the column to be tallied (col_tally) iv) this function returns a list of tuples where each element is a tuple...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT