It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.)
void remove_list(struct linked_list* list) //*This is the function to make and below is the explanation that should be written in given code.
"This function removes the list. When the list is removed, all the memory should be released to operating system (OS) so that OS lets other computer programs use this. While deleting the list, every node should be freed separately; free(list) will not remove every node in the list. To check whether the nodes are removed perfectly, for every deletion of a node, this function should print message “The node with value n (corresponding value) is deleted!” Also, if the whole list is deleted, this function should print message “The list is completely deleted: n nodes are deleted”."
Given code is written below,(There is a function to fill in last moment in this code)
linked_list.h: This is the header file of linkLQS.c that declares all the functions and values that are going to be used in linkLQS.c. You do not have to touch this function.
-----------------------------------------------------------------------
(Below code is about linked_list.h)
#include
#include
#include
struct linked_node{
int value;
struct linked_node* next;
struct linked_node* prev;
};
struct linked_list{
int type_of_list; // normal = 0, stack = 1
struct linked_node* head;
struct linked_node* tail;
int number_of_nodes;
};
--------------------------------------------------------
#include "linked_list.h"
#include "string.h"
extern int list_exist;
struct linked_list* create_list (int number_of_nodes, int
list_type)
{
int a[number_of_nodes];
int i, j;
int bFound;
if (number_of_nodes < 1)
{
printf("Function create_list: the
number of nodes is not specified correctly\n");
return NULL;
}
if(list_exist == 1)
{
printf("Function create_list: a
list already exists\nRestart a Program\n");
exit(0);
}
if(list_type != 0 && list_type != 1)
{
printf("Function create_list: the
list type is wrong\n");
exit(0);
}
struct linked_list * new_list = (struct
linked_list*)malloc(sizeof(struct linked_list));
new_list->head = NULL;
new_list->tail = NULL;
new_list->number_of_nodes = 0;
new_list->type_of_list = list_type;
//now put nodes into the list with random
numbers.
srand((unsigned int)time(NULL));
if(list_type == 0)
{
for ( i = 0; i <
number_of_nodes; ++i )
{
while ( 1
)
{
a[i] = rand() % number_of_nodes + 1;
bFound = 0;
for ( j = 0; j < i; ++j )
{
if ( a[j] == a[i] )
{
bFound =
1;
break;
}
}
if ( !bFound )
break;
}
struct
linked_node* new_node = create_node(a[i]);
insert_node(new_list, new_node);
}
}
else if(list_type == 1)
{
for ( i = 0; i <
number_of_nodes; ++i )
{
while ( 1
)
{
a[i] = rand() % number_of_nodes + 1;
bFound = 0;
for ( j = 0; j < i; ++j )
{
if ( a[j] == a[i] )
{
bFound =
1;
break;
}
}
if ( !bFound )
break;
}
struct
linked_node* new_node = create_node(a[i]);
push_Stack(new_list, new_node);
}
}
list_exist = 1;
printf("List is created!\n");
return new_list;
}
struct linked_node* create_node (int node_value)//This
functon is the example for reference of the assignment
function
{
struct linked_node* node = (struct
linked_node*)malloc(sizeof(struct linked_node));
node->value = node_value;
node->next = NULL;
node->prev = NULL;
return node;
}
void insert_node(struct linked_list* list, struct
linked_node* node)//This functon is the example for reference of
the assignment function
{
node->next = NULL;
node->prev = NULL;
if(list->head == NULL)
//if head is NULL, tail is also NULL.
{
list->head = node;
list->tail = node;
list_exist = 1;
}
else if(list->head == list->tail)
{
node->next =
list->head;
list->head->prev =
node;
list->head = node;
}
else if(list->head != list->tail)
{
node->next =
list->head;
list->head->prev =
node;
list->head = node;
}
(list->number_of_nodes)++;
}
void remove_list(struct linked_list*
list)//
{
~~~~~~~~~~~ //your code starts from
here
int deleted_nodes = 0;//Please do not erase
these sentences. you should cover these sentences!
int deleted_node_value;
}
}
In: Computer Science
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.)
void remove_node(struct linked_list* list, int rm_node_value) (the function to make)
This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is stack, print the error message "Function remove_node: The list type is not normal. Removal is not allowed" Also, if there is no such node to remove from the list, print the error message “Function remove_node: There is no such node to remove” and exit the function.
<<This is the function and its explanation that should be written in given code.
Given code is written below,(There is a function to fill in last moment in this code)
linked_list.h: This is the header file of linkLQS.c that declares all the functions and values that are going to be used in linkLQS.c. You do not have to add any more functions or variables in order to complete the project and you are NOT ALLOWED to.
string.h : This header file defines several functions to manipulate C strings and arrays.
-----------------------------------------------------------------------
#include "linked_list.h"
#include "string.h"
extern int list_exist;
struct linked_list* create_list (int number_of_nodes, int
list_type)
{
int a[number_of_nodes];
int i, j;
int bFound;
if (number_of_nodes < 1)
{
printf("Function create_list: the
number of nodes is not specified correctly\n");
return NULL;
}
if(list_exist == 1)
{
printf("Function create_list: a
list already exists\nRestart a Program\n");
exit(0);
}
if(list_type != 0 && list_type != 1)
{
printf("Function create_list: the
list type is wrong\n");
exit(0);
}
struct linked_list * new_list = (struct
linked_list*)malloc(sizeof(struct linked_list));
new_list->head = NULL;
new_list->tail = NULL;
new_list->number_of_nodes = 0;
new_list->type_of_list = list_type;
//now put nodes into the list with random
numbers.
srand((unsigned int)time(NULL));
if(list_type == 0)
{
for ( i = 0; i <
number_of_nodes; ++i )
{
while ( 1
)
{
a[i] = rand() % number_of_nodes + 1;
bFound = 0;
for ( j = 0; j < i; ++j )
{
if ( a[j] == a[i] )
{
bFound =
1;
break;
}
}
if ( !bFound )
break;
}
struct
linked_node* new_node = create_node(a[i]);
insert_node(new_list, new_node);
}
}
else if(list_type == 1)
{
for ( i = 0; i <
number_of_nodes; ++i )
{
while ( 1
)
{
a[i] = rand() % number_of_nodes + 1;
bFound = 0;
for ( j = 0; j < i; ++j )
{
if ( a[j] == a[i] )
{
bFound =
1;
break;
}
}
if ( !bFound )
break;
}
struct
linked_node* new_node = create_node(a[i]);
push_Stack(new_list, new_node);
}
}
list_exist = 1;
printf("List is created!\n");
return new_list;
}
struct linked_node* create_node (int node_value)//This
functon is the example for reference of the assignment
function
{
struct linked_node* node = (struct
linked_node*)malloc(sizeof(struct linked_node));
node->value = node_value;
node->next = NULL;
node->prev = NULL;
return node;
}
void insert_node(struct linked_list* list, struct
linked_node* node)//This functon is the example for reference of
the assignment function
{
node->next = NULL;
node->prev = NULL;
if(list->head == NULL)
//if head is NULL, tail is also NULL.
{
list->head = node;
list->tail = node;
list_exist = 1;
}
else if(list->head == list->tail)
{
node->next =
list->head;
list->head->prev =
node;
list->head = node;
}
else if(list->head != list->tail)
{
node->next =
list->head;
list->head->prev =
node;
list->head = node;
}
(list->number_of_nodes)++;
}
void remove_node(struct linked_list* list, int
rm_node_value)(the function to be written!!)
{
struct linked_node * del_Node//please do
not erase it. you should cover this sentence.
~~~~~//your code starts from here
}
}
In: Computer Science
LinkedList.h:
/*-- LinkedList.h --------------------------------------------------------------
This header file defines the data type List for processing
lists.
Operations are:
Constructor
Destructor
Copy constructor
Assignment operator
insert: Insert an item
erase: Remove an item
getListElement: get a range of list elements
-------------------------------------------------------------------------*/
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
class LinkedList
{
public:
typedef int ListElement;
private:
/******** Data Members ********/
class Node
{
public:
ListElement
data;
Node *
next;
};
Node *first;
// pointer to first
element in linked list
int getListSize() const;
public:
/******** Error Codes ********/
static const int ILLEGAL_LIST_POSITION =
-1;
static const int NO_ERROR = 0;
LinkedList();
~LinkedList();
LinkedList(const LinkedList &
origList);
const LinkedList & operator=(const
LinkedList & rightHandSide);
int insert(ListElement item, int
pos);
int erase(int pos);
int getListElement(int posStart, int
posEnd, ListElement rv[]) const;
}; //--- end of List class
#endif
LinkedList.cpp:
/*-- LinkedList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <new>
#include <iostream>
#include "LinkedList.h"
//--- Definition of class constructor
// Construct a List object.
// Precondition: None.
// Postcondition : An empty List object is constructed; first ==
nullptr
//--- Definition of class destructor
// Destroys a List object.
// Precondition : The life of a List object is over.
// Postcondition : The memory dynamically allocated to the linked
list is now deallocated
//--- Definition of copy constructor
// Construct a copy of a List object.
// Precondition: A copy of origList is needed; origList is a
const
// reference parameter.
// Postcondition: A copy of origList has been constructed.
//--- Definition of assignment operator
// Assign a copy of a List object to the current object.
// Precondition: rightHandSide List is required.
// Postcondition : A copy of rightHandSide has been assigned to
this
// object. A const reference to this list is returned.
//--- Definition of insert()
// Insert item at pos position.pos 0 is the first element position
in the list
// Precondition : A constructed list, either empty or with
elements
// Postcondition : inserted item into list at pos position
// Returns ILLEGAL_LIST_POSITION for insert that is out of range of
the current list,
// Otherwise return a NO_ERROR.
//--- Definition of erase()
// Erase item at pos position.pos 0 is the first element position
in the list.
// Precondition: A constructed list, either empty or with
elements
// Postcondition : erased item at pos position
// Returns ILLEGAL_LIST_POSITION for erase that is out of range of
the current list,
// Otherwise return a NO_ERROR.
//--- Definition of getListElement()
// Returns list values
// Precondition : A constructed list, either empty or with
elements.
// The rv[] array must be large enough to hold the returned
contents.
// Postcondition : Fills array rv with the list elements
specified
// Returns ILLEGAL_LIST_POSITION for move that is out of range of
the current list,
// Otherwise return a NO_ERROR. Both posStart and posEnd must be
valid positions
// and posStart <= posEnd.posStart is an index to the start of
the data and
// posEnd is an index to the end of the data.To retieve one
element
// posStart and posEnd will be the same value.
int LinkedList::getListElement(int posStart, int posEnd,
ListElement rv[]) const
{
if (posStart < 0 || posStart >=
getListSize() || posEnd < 0 || posEnd >= getListSize() ||
posStart > posEnd)
{ // check for valid paramenters
return
ILLEGAL_LIST_POSITION;
}
Node *current = first; // point
to zero node
for (int i = 0; i < posStart; i++)
{ // loop to find the node
current =
current->next;
}
for (int i = 0; i < ((posEnd - posStart)
+ 1); i++)
{ // put returned elements in the beginning
of the list
rv[i] =
current->data;
current =
current->next;
}
return NO_ERROR;
}
//--- Definition of getSize()
// returns the size of the list. Do not change this.
int LinkedList::getListSize() const
{
int rv = 0;
Node *current = first;
for (; current != nullptr;)
{
rv++;
current =
current->next;
}
return rv;
}
In: Computer Science
I know how to do this with arrays, but I have trouble moving my code to use with linked lists
Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command. The valid commands are: R or r: reserve a room C or c: cancel a reservation W or w: remove a request from the waiting list L or l: list the current reservations for the night Q or q: quit the program Any other input: print an error message and prompt for another command.
You must use a linked list to represent the reservation list, and another linked list to represent the waiting list. You can determine whether each list will be singly- or doubly linked, and whether each list has just a front pointer, or a front and rear pointer. The two lists do not need to have the same design (that is one could be singly-linked with a front pointer, and the other doubly-linked with front and rear pointers. The reservation list can have at most as many nodes as there are rooms in the hotel Actions taken in response to a valid command (r, c, w, or l) must be implemented using programmer-defined functions, one per command. Any needed data must be passed to the functions, not declared globally. Implement reservation ids using a simple integer counter. Names will have fewer than 15 characters.
Actions for each command are:
Reservation: If there is a free room, reserve a room by inserting a node on the reservation list containing the next reservation id and the name associated with the reservation. When there is space available, print the reservation id for the person at the keyboard, and prompt for and read the name associated with the reservation. If there are no rooms, print an appropriate message and ask if the person wants to be entered on the waiting list. If they do, add a node to the waiting list array, print the reservation id for the person at the keyboard, and prompt for and read the name associated with the waiting list entry. The waiting list must be implemented as a queue (insert nodes at the back of the list and remove nodes from the front of the list when a room becomes available)
Cancellation: If there is a room reserved under that reservation id, cancel the reservation by removing the node associated with the reservation. Otherwise print a message that the id is not valid. If a room is cancelled and there are entries on the waiting list, remove the first entry on the waiting list and insert the data in the reservation list, then print a message indicating that reservation id is now confirmed. Note that, if the nodes on both lists are the same type, you can simply insert the node you removed from the waiting list into the reservation list.
Wait cancellation: If there is a waiting list entry with that reservation id, the node containing that reservation id should be removed from the waiting list. Otherwise print a message indicating that id is not on the waiting list.
List reservations: Print the reservation ids and associated names of all rooms that are reserved. Do not print anything for rooms that are vacant. If there are no rooms reserved, print a message indicating that. If there are any entries on the waiting list you should also print the reservation number and name of all elements on the waiting list.
Quit: end the program by returning from the main function. Any other command: print an error message and prompt for another command.
Use an integer counter for reservation ids that starts at 1. Reservation ids are not reused. Use another integer to keep track of the number of rooms reserved. Your solution will be for a boutique hotel with only 3 (very expensive) rooms. But make liberal use of #define statements so it would be trivial to adapt your solution to a larger hotel.
In: Computer Science
I know this qu is posted but I have not got the answer BY unsorted list using STLl!!
1.(70) An organization
that your little cousin belongs to is selling low-fat cookies. If
your cousin's class sells more cookies than any other class, the
teacher has promised to take the whole class on a picnic. Of
course, your cousin volunteered you to keep track of all the sales
and determine the winner. Each class has an identification number.
Each sales slip has the class identification number and the number
of boxes sold.
Input (Note: if you use keyboard for the input, it is ok for this
assignment) Here is a sample of the data. (The classes are numbered
from 1 through 10.)
Id. Number Boxes Sold
3 23
4 1
2 13
2 7
4 5
1 6
10 16
Output The following information written on file "boxes", all
properly labeled. The total number of boxes sold by each class. The
identification number of the winning class. If there is a tie, list
all winners.
Data Structures: using class UnsortedType defined in the textbook
(chapter 3). The interface is provided as follows or you can
include from the STL library. You can use either array or
LinkedList as the fundamental data structure. (you need to justify
your decision.)
Deliverables
Part I - Your design (objected-oriented design). (use diagrams, or
pseudo-code, or CRC card to show your logical level design)
Part II - A listing of your program (implementation of the program
in C++) - A listing of your test plan as input to the program - A
listing of the output file
Interface of UnsortedType class:
bool IsFull() const; // Function: Determines whether list is full.
// Pre: List has been initialized.
// Post: Function value = (list is full)
int LengthIs() const; // Function: Determines the number of
elements in list. // Pre: List has been initialized. // Post:
Function value = number of elements in list
void RetrieveItem(ItemType& item, bool& found); //
Function: Retrieves list element whose key matches item's // key
(if present). // Pre: List has been initialized. // Key member of
item is initialized. // Post: If there is an element someItem whose
key matches // item's key, then found = true and item is a copy of
// someItem; otherwise found = false and item is unchanged. // List
is unchanged.
void InsertItem(ItemType item); // Function: Adds item to list. //
Pre: List has been initialized. // List is not full. // item is not
in list. // Post: item is in list.
void DeleteItem(ItemType item); // Function: Deletes the element
whose key matches item's key. // Pre: List has been initialized. //
Key member of item is initialized. // One and only one element in
list has a key matching // item's key. // Post: No element in list
has a key matching item's key.
void ResetList(); // Function: Initializes current position for an
iteration // through the list. // Pre: List has been initialized.
// Post: Current position is prior to list.
void GetNextItem(ItemType& item); // Function: Gets the next
element in list. // Pre: List has been initialized and has not been
changed since // last call. // Current position is defined. //
Element at current position is not last in list. // Post: Current
position is updated to next position. // item is a copy of element
at current position.
In: Computer Science
PLEASE ANSWER ALL BEFORE SUBMITTING. THANK YOU. "___" = answers needed.
4. What are the difference scores for the following list of scores for participants observed at two times?
| Time 1 | Time 2 | Time 1 − Time 2 |
|---|---|---|
| 10 | 8 | ____ |
| 3 | 2 | ____ |
| 6 | 7 | ____ |
| 5 | 6 | ____ |
| 7 | 3 | ____ |
6. A statistics individual wants to assess whether her remedial job has been effective for her five students. She decides to conduct a related samples t-test and records the following grades for students prior to and after receiving her job.
| Tutoring | |
|---|---|
| Before | After |
| 2.6 | 3.2 |
| 2.7 | 3.1 |
| 3.2 | 3.7 |
| 3.1 | 3.3 |
| 2.9 | 3.8 |
(A) Test whether or not her tutoring is
effective at a 0.05 level of significance. State the value of the
test statistic. (Round your answer to three decimal places.)
t = ___
(B) Compute effect size using estimated Cohen's
d. (Round your answer to two decimal places.)
d = ___
7. A psychologist wants to know whether wives and husbands who both serve in a foreign war have similar levels of satisfaction in their marriage. To test this, six married couples currently serving in a foreign war were asked how satisfied they are with their spouse on a 7-point scale ranging from 1 (not satisfied at all) to 7 (very satisfied). The following are the responses from husband and wife pairs.
| Married Couples | |
|---|---|
| Wife | Husband |
| 6 | 5 |
| 4 | 6 |
| 7 | 5 |
| 6 | 6 |
| 7 | 5 |
| 6 | 5 |
(A) Test whether or not mean ratings differ at
a 0.05 level of significance. State the value of the test
statistic. (Round your answer to three decimal places.) ___
(B) Compute effect size using eta-squared. (Round
your answer to two decimal places.) ___
9. A researcher records the amount of time (in minutes) that parent-child pairs spent on social networking sites to test whether they show any generational differences. From the following findings reported in APA format, interpret these results. Parents spent significantly less time on social networking sites compared to their children (MD = 42 minutes), t(19) = 3.476, p < 0.05, d = 0.59.
State the sample size.
___ participants
16. Listening to music has long been thought to enhance intelligence, especially during infancy and childhood. To test whether this is true, a researcher records the number of hours that eight high-performing students listened to music per day for 1 week. The data are listed in the table.
| Music
Listening Per Day (in hours) |
|---|
| 4.3 |
| 4.9 |
| 4.9 |
| 3.8 |
| 4.2 |
| 5.4 |
| 4.2 |
| 4.5 |
(A) Find the confidence limits at a 95% CI for
this one-independent sample. (Round your answers to two decimal
places.)
___ to ___ hours per day
17. To save money, a local charity organization wants to target its mailing requests for donations to individuals who are most supportive of its cause. They ask a sample of 5 men and 5 women to rate the importance of their cause on a scale from 1 (not important at all) to 7 (very important). The ratings for men were M1 = 6.3. The ratings for women were M2 = 5.4. If the estimated standard error for the difference (sM1 − M2) is equal to 0.25, then consider the following.
(A) Find the confidence limits at an 80% CI for
these two-independent samples. (Round your answers to two decimal
places.)
___ to ___
18. An instructor believes that students do not retain as much information from a lecture on a Friday compared to a Monday. To test this belief, the instructor teaches a small sample of college students some preselected material from a single topic on statistics on a Friday and on a Monday. All students received a test on the material. The differences in scores for material taught on Friday minus Monday are listed in the following table.
| Difference
Scores (Friday − Monday) |
|---|
|
−1.7 |
|
+1.0 |
|
+6.4 |
|
+3.5 |
|
+4.5 |
(A) Find the confidence limits at a 95% CI for
these related samples. (Round your answers to two decimal
places.)
___ to ___
In: Statistics and Probability
python question:
Problem Statement Given a list of integers input_list, loop through every element in the list to find the product of all positive integers and the count of all negative integers. The code to get the input_list is provided for you. The first line of code provided gets the size of the list. The remaining lines of code provided get the elements of the list. The provided data preprocessing code reads in these values and creates a list of ints for you.
In: Computer Science
Write a program that create a single linked list and consist of all the necessary functions to do the following
Note:
You need to add proper documentation to your programs and you need to include the output of each program
C++
In: Computer Science
Can you please write a pseudocode for the following:
#include <stdio.h>
int main(){
printf("Welcome to my Command-Line Calculator
(CLC)\n");
printf("Developer: Your name will come here\n");
printf("Version: 1\n");
printf("Date: Development data Will come
here\n");
printf("----------------------------------------------------------\n\n");
//choice stores users input
char choice;
//to store numbers
int val1,val2;
//to store operator
char operation;
//flag which leths the loop iterate
//the loop will break once the flag is set to 0
int flag=1;
printf("Select one of the following items: \n");
printf("B) - Binary Mathematical Operations, such as
addition and subtraction.\n");
printf("U) - Unary Mathematical operations, such as
square root, and log.\n");
printf("A) - Advances Mathematical Operations, using
variables, arrays.\n");
printf("V) - Define variables and assign them
values.\n");
printf("E) - Exit\n");
//taking user input for choice
scanf(" %c",&choice);
//do while loop
do{
//switch case
switch(choice){
case 'B':
printf("Please
enter the first number:\n");
scanf("%d",&val1);
printf("Please
enter the operation (+ , - , * , / ):\n");
scanf("
%c",&operation);
printf("Please
enter the second number:\n");
scanf("%d",&val2);
switch
(operation){
case '+':
printf("The result is %d
\n",(val1+val2));
break;
case '-':
printf("The result is %d
\n",(val1-val2));
break;
case '*':
printf("The result is %d
\n",(val1*val2));
break;
case '/':
printf("The result is %d
\n",(val1/val2));
break;
//if any other input is entered
default:
printf("Invalid
operator\n");
}
//asking the
user for input as the operation ended
printf("Please
select your option ( B , U , A , E , V)\n");
scanf("
%c",&choice);
break;
case 'U':
printf("Sorry,
at this time I don't have enough knowledge to serve you in this
category\n");
//asking the
user for input as the operation ended
printf("Please
select your option ( B , U , A , E , V)\n");
scanf("
%c",&choice);
break;
case 'A':
printf("Sorry,
at this time I don't have enough knowledge to serve you in this
category\n");
//asking the
user for input as the operation ended
printf("Please
select your option ( B , U , A , E , V)\n");
scanf("
%c",&choice);
break;
case 'V':
printf("Sorry,
at this time I don't have enough knowledge to serve you in this
category\n");
//asking the
user for input as the operation ended
printf("Please
select your option ( B , U , A , E , V)\n");
scanf("
%c",&choice);
break;
case 'E':
//if user enters
E flag is set to 0, so that the loop will break
flag=0;
printf("Thanks
for using my Simple Calculator. Hope o see you soon again,
Goodbye!\n");
break;
default:
printf("Invalid
input");
printf("Please
select your option ( B , U , A , E , V)\n");
scanf("
%c",&choice);
}
}while(flag);
}
In: Computer Science
Given the following program with fill in the blanks, implement the PQ class which is priority Queue MAX heap; the higher the number, the higher the prority of that number. None of the public methods has been implemented. You will need to write other methods to help implement some of the methods. For example you will need to implement bubbleUp to help with the insert method.
Methods to implement:
insert -> insert one element into the PQ and maintain heap shape/ order
remove-> remove the highest priority and maintain heap shape/order
poll -> gets the highest priority without removing
heapify -> build a priority queue from a given list of numbers
toSortedList-> removes all elements from the PQ into a list such that is is sorted from highest to lowest priority.
_________________________________________________________________________________________________________
public class PQimplement {
public static void main(String args[]){
List list = randomList(20);
System.out.println("Unsorted Input List: " + list.toString());
PQ priorityQueue = new PQ(list);
List sortedList = priorityQueue.toSortedList();
System.out.println("Sorted List Descending order: " + list.toString());
}
private static List randomList(int size){
final Random rng = new Random();
List list = new ArrayList<>(size);
for(int i = 0; i< size; i++){
list.add(rng.nextInt()%10);
}
return list;
}
}
| public class PQ { | |
| int data[]; | |
| int size; | |
| int capacity; | |
| PQ(int capacity){ | |
| this.capacity = capacity; | |
| size = 0; | |
| data = new int[capacity]; | |
| } | |
| PQ(){ | |
| capacity = 1000; | |
| size = 0; | |
| data = new int[capacity]; | |
| } | |
| PQ(List data){ | |
| capacity = data.size() > 1000 ? data.size() : 1000; | |
| size = data.size(); | |
| heapify(data); | |
| } | |
| public void insert(int data){ | |
| //Insert the new data into the PQ, ensure the heap maintains heap order/shape | |
| //fill in | |
| } | |
| public void remove(){ | |
| //Removes the root or the node with the highest priority | |
| //fill in | |
| } | |
| public int poll(){ | |
| //Returns the node with the highest priority. This method should NOT remove that node | |
| //fill in | |
| return -1; | |
| } | |
| private void heapify(List data){ | |
| //implement the heapify method that will build a PQ given a list of data | |
| } | |
| public List toSortedList(){ | |
| //this method will return a list of all the integers in the priority queue in sorted order (Max Heap) | |
| //this method will remove all the data from the pq | |
| } | |
| } |
In: Computer Science