C/C++ :: Error In Circular Vector Implementation Of Sorted Priority Queue
Mar 1, 2015
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){}
[Code] ....
View 1 Replies
ADVERTISEMENT
Jun 28, 2013
/* 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
View 2 Replies
View Related
Aug 7, 2014
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.
priority_queue<int, vector<int>, greater<int> > Open;
View 2 Replies
View Related
Nov 28, 2014
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();
[Code] .....
View 3 Replies
View Related
Jan 19, 2013
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
[Code] .....
View 1 Replies
View Related
Nov 4, 2013
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?
Example:
#include <cstdlib>
struct Random {
bool operator<(const Random&) const { return !(rand()&1); } };
View 10 Replies
View Related
Jun 19, 2013
how double ended priority queue be used to implement external quick sort?
View 8 Replies
View Related
Oct 28, 2014
I know queue but with priority queue i am really amateur. How to implement some functions in priority queue.
template <class T> class PRIORITY_QUEUE {
private:
T *items; // array of queue items
int rear;
int maxSize; // maximum size of queue
[Code] ....
and what does heapify mean???
View 8 Replies
View Related
Jan 4, 2014
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.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pqueue.h"
struct track
{
char artist[20];
char title[20];
int rating;
}
[code]....
View 11 Replies
View Related
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++) {
[Code]...
View 2 Replies
View Related
Jan 27, 2013
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);
[Code] ....
View 1 Replies
View Related
Aug 19, 2013
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:
template<class T>
struct sorter {
std::vector<T>& data;
sorter(std::vector<T>& __d) : data(__d) {}
bool operator()(int i, int j) const { return data[i] > data[j]; }
};
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?
View 1 Replies
View Related
Feb 27, 2015
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.
PQType.h
template<class ItemType>
class PQType {
public:
PQType(int);
PQType(const PQType&); /
[Code] .....
View 4 Replies
View Related
Oct 6, 2013
I am stuck with how to make a circular queue that are based on a struct. Have been reading about the implementation but cant really understand it fully. Here is what i got so far.
Code:
#define SIZE 10
typedef struct {
char reg;
char brand;
int modelyear;
int mileage;
[Code] .....
View 8 Replies
View Related
Oct 31, 2014
I want to write a program where one core (core 0) will fill the FIFO and other core (core 1 ) will delete the data from FIFO.
Core 0:
I create a fifo
Code:
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
And my en-queue function is in core 0 and writing to specific memory location..(which works perfectly)
Code:
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
[Code] ....
Now the problem is in the core 1. Here I am unable to read the values from the specific memory location. I am getting garbage value. Where I am doing some stupid error.. I did not understand
Code:
(front->ptr) = (unsigned int *) memory_location;
When I print the (front->ptr) it shows correct memory address but inside the De-queue function in core 1, I am getting wrong value..
Code:
int deq(int buf[n]) {
front1 = front;
printf("Val %d ", front->info); // showing wrong value
if (front1 == NULL) {
printf("
Error: Trying to display elements from empty queue");
return 0;
[Code] ...
View 8 Replies
View Related
Dec 4, 2013
I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class.
My store.h
#ifndef __STORE_H_INCLUDED__
#define __STORE_H_INCLUDED__
#include <queue>
using namespace std;
#include "Transaction.h"
struct TransactionCompare;
[Code] ....
The error im getting with this set up is
error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &)
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'
View 6 Replies
View Related
Feb 20, 2013
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.
I want to do something like the code given below
#include <vector>
#include <queue>
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();
[Code] ....
How to implement this logic?
will q[i].size=q[i].size+5 increase the queuesize by 5 for the ith queue?
View 12 Replies
View Related
Jan 29, 2014
I have a circular queue using DLL which is using globally declared pointers. The problem now is that it is not being initialize properly or being cleared thus my code is not working as expected.
In my code you will be asked how many nodes do you wish to enter "i made 2 default for now", after that you may then add or delete the node. add only works now since delete is still on progress.
When you add the nodes "2 nodes by default" the program will only record the latest input so if i were to input 1 and 2, only 2 will be displayed. I know that this maybe because of my *first and *last variables not being initialize properly.
How should i really work with global pointers? Also im really new to project file programming and not a fan of pointers or linked list at all.
main.c
Code:
void main(){
int ch, number, numdum = 0;
n *new, *ptr, *prev, *first, *last;
first = NULL;
last = NULL;
clrscr();
printf("Enter number of nodes: ");
scanf("%d", &number);
[Code] .....
View 5 Replies
View Related
Mar 23, 2013
I am trying to create a code to represent a queue using a linked list but i get the disturbing "Segmentation fault coredumped" after compilation.
Code:
#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>
typedef int Item;
typedef struct queue_type *Queue;
typedef struct node *return_node;
[Code] .....
View 2 Replies
View Related
Aug 31, 2014
I am trying to compare performance of different lock-free queues, therefore, I want to create a stress test - which includes pushing/popping user-defined pre-built objects to and from the queue. My question is how can perform the stress test with pushing and popping objects instead of pointers to object like I have done in my code. What is the different in terms of performance of pushing/popping objects Vs. pushing/popping pointers.
#include <cstdlib>
#include <stdio.h>
#include <string>
[Code]....
View 3 Replies
View Related
Dec 1, 2014
I have vector pair like below, it is sorted but I want to remove duplicate values: The vector is like below:
std::vector<std:air<std::string,double>> fileWeight;
Here is what in vector:
scene1.jpg 0.4
scene2.png 0.4
scene3.jpg 2.3
scene4.jpg 4.1
The highlighted top 2 are duplciated. How can I remove duplciated like this where I have to check whole pair?
View 9 Replies
View Related
Sep 17, 2014
How to pass my array to the function and return it as a sorted vector. I'm kind of lost with the functions part.
Problem: Write a program that performs a sorting routine as discussed in class. Create an array of 10 integers (e.g., 63, 42, 27, 111, 55, 14, 66, 9, 75, 87). Pass this array to a separate function that sorts the integers and returns a vector containing the sorted integers. Use either the SELECTIONSORT algorithm or the INSERTIONSORT.
Code so far:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
[Code]...
View 2 Replies
View Related
Oct 7, 2012
For school we have to create a blackjack game using windows form. I had to store the images of each card into a sorted list so i created a class called cardList and created a constructor which contained the the sorted list called cards. So it looks kinda like this:
public class cardList : Form1
{
SortedList cards = new SortedList();
public cardList()
[Code]....
There's probably a few other errors, I'm still trying to figure this whole c# thing out. why the error tells me (on the line that contains c.cards.GetByIndex(cardNumber);) that cards is inaccessible due the its protection level.
View 1 Replies
View Related
May 15, 2013
When compiling the source I get the following error
"pms_program.h:54:56: error: expected declaration specifiers or ‘...’ before ‘CourseType’"
the code you are looking for in pms_course.h ( there is a circular dependency between pms.h , pms_course.h and pms_program.h)
function prototype AppendProgram ( part of doubly linked list )
I have attached the full course code.
As much as I like I cannot change the structure of the startup code ...
please see attached zip file for the entire source code.
View 6 Replies
View Related
Feb 15, 2013
I have a paradigm where a integer before gets enqueued to a queue in a vector, the loop of queues is searched and integer is enqueued to a queue which has minimum size among the queues. the following code shows the operation
#include <vector>
#include <queue>
std::vector<std::queue<int> > q
[Code].....
next i am trying to extend my paradigm with the condition, that the integers should be enqueued to the shortest queue until the count of the shortest queue is less than or equal to count of any another queues in the loop of queues.
i want to do something like the code shown below
#include <vector>
#include <queue>
std::vector<std::queue<int> > q
[Code].....
View 4 Replies
View Related
Apr 14, 2014
I created a structure Vector and implement some functions to make the new defined type (vector) dynamically allocated and resized (inspired from the C++ implementation of the dynamic arrays : vector). I need to assign a structure to every vector element but I am not sure that I am doing it right:
here is the structure that I've defined:
typedef struct {
void** mem; // to make this parametrizable I want the void* to point to a Structure (referenced with * a pointer)
unsigned long elems;
unsigned long elemsize; //element size
[Code] ....
I believe I have serious problems with pointers, values and dereferencing a pointer.
View 7 Replies
View Related