C++ :: Priority Queue - Inserting Into Bottom Of Array
Jan 24, 2013
i am working on creating a priority queue, i seem to have done the algorithm for inserting from the top correctly but the algorithm for inserting from the bottom doesnt seem to work it just overwrites the existing data.
below is my code :
list is an array and listlength is the size of the array declared as a integer
void inserttop(string task) {
//int head = 0 ;
string temp ;
listlength++ ;
for (int i = 1; i < listlength; i++) {
This assignment is about Heap and PQ's to sort out jobs inside a printer. I'm far from finishing the assignment but the most important part isn't working. My issue is that nothing is getting stored inside the array. I keep getting crashes and at this point I'm not sure what to do. I notice that my destructor runs right after my "addJob" Function finishes, which is destroying the memory. Which might be why nothing gets stored inside OR I think my implementation of Heap/PQ is wrong.
Functions inside my test.cpp aren't properly done, they are made just to see if something is stored inside.
1. Check if I created the array correctly [PQtype.cpp / Heap.h/ PQType.h] 2. Am I even using/storing into the array. [Test.cpp "addJob" Function] 3. I'm also new to working with Class Templates.
I'm trying to use a priority queue sorting in reverse order to take a vector or 2d array. The problem is that I want to sort by the vector/array cell value but keep the reference to the vector/array index of the value. I don't know quite howto keep them both related so when I pop. I can find the corresponding cell.
How to build a FiFO queue without using the STL (done that no problem), get it to dequeue (again, done that no problem). However, to get those extra marks, I need to be able to order it using a priority system.
I've tried ordering the NodeDequeue class that I'll show at the bottom of this post, but I just cannot get it to order appropriately. The closest I have got is everything in order but I lose a Node from the memory completely. So, that's no good.
The most logical idea I have thought of right now is to send the largest number to the back of the queue each time it's iterated, eventually, the largest number will end up at the front.
class PriorityQueue : public Queue { public: Node* NodeDequeue(void) { Node* tmp = front; Node* seek = tmp->getPrev();
I'm trying to implement Prim's Algorithm and for that I need to have a decreaseKey method for a priority queue (to update the key value in a priority queue). Can I implement this in the STL Priority Queue?
This is the algorithm I'm following:
for each vertex u in graph G set key of u to INFINITY set parent of u to NIL set key of source vertex to 0 en-queue to priority queue Q all vertices in graph
I've got a priority queue of items who, from time to time, returns a different comparison result. Any way to re-sort a priority queue who is storing them?
write a program, pfpq.c, that maintains a priority queue of music tracks (songs) and ratings. The struct is given by:
Code:
struct track { char artist[20]; char title[20]; int rating; };
The program is only designed to demonstrate the concept so get the user to enter a small number of tracks and create a priority queue. (Use an empty string title or an empty string artist to end the program). Print the priority queue as it grows. Your program should initially ask the user if they wish to order the priority queue by rating in ascending order, or by rating in descending order, or if they wish to order the priority queue by artist name. Then build the priority queue accordingly. in the last case, "if they wish to order the priority queue by artist name." which is case 3. How do I sort the priority queue by artist name using in the following code.
So i have been banging my head against a wall with this problem for awhile. I have also copy and pasted direct examples from the internet but nothing seems to sort it right.
Node Header
Code: #ifndef _NODE_H #define _NODE_H #include <stdio.h> class Node { public: Node(); Node(int weight, char value, Node* left = NULL, Node* right = NULL);
On several occasions in my project, I need to sort elements (indeces) based on their linked values. Those values are stored in an array. This means the comparison looks like this:
bool operator()(int i, int j) { return someArray[i] > someArray[j]; }
Because this form returns often but someArray may differ, I wrapped this in a template class:
Now, I want to use this in a different way: I want to select the K indeces with the lowest value from someArray attached to it. My idea was to build a priority_queue, push the first K elements and then compare all the next elements to PQ.top(), as such:
INDEX t(0); for (; t < someLimit; ++t) { pq.push(t); if (pq.size() == K) break; } for (; t < someLimit; ++t) { if (someArray[t] < someArray[pq.top()]) { pq.pop(); pq.push(t); } }
My problem, however, is the definition / initialization of the priority_queue object. My first idea was simply std::priority_queue<INDEX> pq(sorter<VALUE>(someArray));, but calling any function on pq provides the error "expression must have class type" (?) when hovering over pq.
My second guess, std::priority_queue<INDEX, std::vector<INDEX>, sorter<VALUE>(someArray)> pq;, provides the error 'expected a ')'' when hovering over someArray.
What is the correct way to initialize this data structure?
There appears to be some kind of error in by removeMin() function. Inserting items seems to work just fine but attempting to remove any items gives me the error "vector subscript out of range".
Here is my SortedPQ class... Below it you will find the quicksort implementation, in case looking at it is necessary.
template <class Type> class SortedPQ { private: int front; int rear; vector<Type> V; public: SortedPQ(void) : front(-1), rear(-1), V(0){}
For some reason, my input seems to lag one behind when inserting data. Attached, I have code that I wrote along with the example of the lag. What may be wrong with my code that is causing this to lag?
I have the structure defined in the code below and need to insert a new string into the middle of the string array. I know there is a way to insert a new element with a vector. How to do this. I have tried several variations similar to uniqueList.word.insert(400,"disisdabomb"); but with no luck.
const int maxWordCount=1500; struct wordCountList { string word[maxWordCount]; int count[maxWordCount]; }; wordCountList uniqueList;
Below is an extracted portion of an example exercise from the C Programming Language book:
Code: void escape(char * s, char * t) { int i, j; i = j = 0;
while ( t[i] ) { switch( t[i] ) {
[Code] ....
Here's my problem with this. s is a pointer to a char array. That means each index stores 1 byte. A backslash character '' is one byte. In above example, we escape it with a second backslash character: ''. That means we have two bytes there. So why does it allow inserting two bytes into a one byte index?
I have a paradigm in a loop of queues of a vector,if a condition is true,increase sizes of the queue of that particular queue in the loop of queues, if condition is false, the queuesize is left as such in loop of queues. After this operation i need to search the queue sizes of all queues and enqueue in the shortest queue.
Is there a start of file flag (sof) like eof ??? Because they are going to be passed into a 2 dimensional array and the data is a whole lot to be entering them by hand...
I tried to keep the coding style as similar as possible. I tested these with 4 million (4x1024x1024) 64 bit unsigned integers, Visual Studio 2005, 64 bit mode, Win XP X64, Intel 2600K 3.4ghz cpu. The average time for top down = 3.7 seconds, bottom up = 3.5 seconds.
Code: // tsorttd.h - top down merge sort template <class T> T * TopDownMergeSort(T a[], T b[], size_t n) { TopDownMergeSortAtoA(a, b, 0, n);
Code: typedef vector<int> LIST; // LIST can be a vector of any comparable type static LIST merge_sort(LIST &linp){ size_t i, width; LIST lout(linp.size()); // second list for output
/* Implementation of a circular queue of Array containg names.. */ # include <stdio.h> # include <conio.h> # include <stdlib.h> # include <string.h> # define QSIZE 5 typedef struct{
[Code] ....
I changed my code. but whenever i typed in the ILoveBacolod it takes it as a whole, and if i deleted it deletes the string not the letter. for example:
Enter String: ILoveBacolod Enter a command: Delete (D) Output: LoveBacolod Enter a command: Delete (D) Output: oveBacolod Enter a command: Add (A) Enter a character: z Output: oveBacolodz
I have an assignment where i am required to code up in C, a program to simulate a taxi rank that is implemented as a queue via an array that can hold up to a maximum of six taxis.When a taxi arrives, it joins the rear of the queue. When a taxi departs, the first taxi in the rank is used and its departure is logged.A "rolling menu" comprising integer codes as specified below is used until 0 is entered to exit the simulation. I've done this stage but now the next stage is asking me to implement the queue as a linked list. what the difference is between an array and a linked list and what is a linked list?
Is it possible to write a program such that it will automatically execute the function in queue base on their priority even if user programmed it randomly?
Code:
function1(){ very_important; } function2(){ important; } function3(){ less_important; } int main() { // Programmed by user function3; function1; function2; //Output: Program will execute function1, 2 and lastly 3. }