Printing an Array in a Queue - c++

I'm having a problem printing out the contents of an array used in a queue.
A portion of my template queue:
#include <iostream>
#include <cstdlib>
using namespace std;
template<class T>
class Queue
{
private:
int front; //front position
int rear; //rear position
int maxQue; //maximum number of elements in the queue
T* items; //points to a dynamically-allocated array code here
public:
Queue() // default constructor: Queue is created and empty
{
front = -1;
rear = 0;
maxQue = 10;
items = new T[maxQue];
}
void Print() // print the value of all elements in the queue
{
while(front != rear)
{
cout<<items[front];
front++;
if(front==rear)
break;
cout<<" - ";
}
cout<<endl;
}
void Enqueue(T add) // insert x to the rear of the queue
{ // Precondition: the queue is not full
if(IsFull())
{
cout<<"Queue is full!"<<endl;
}
else
{
items[rear] = add;
rear++;
rear = rear % maxQue;
}
}
void Dequeue(T &x) // delete the element from the front of the queue
{ // Precondition: the queue is not empty
if(!IsEmpty())
{
front = (front+1)%maxQue;
x = items[front];
}
}
bool IsEmpty() // test if the queue is empty
{
return (rear==front);
}
bool IsFull() // test if the queue is full
{
return ((rear+1)%maxQue==front);
}
int length() // return the number of elements in the queue
{
return abs(rear-front);
}
~Queue() // Destructor: memory for the dynamic array needs to be deallocated
{
delete [] items;
}
};
A portion of the main routine:
int main()
{
Queue<float>FloatQueue;
float y;
FloatQueue.MakeEmpty();
FloatQueue.Dequeue(y);
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
cout << "The float queue contains: ";
FloatQueue.Print();
return 0;
}
The code compiles fine up until it tries to print, at which point I get these errors.
0 00000000 0x00466a7f in std::__convert_from_v() (??:??)
1 00000000 0x00430302 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>() (??:??)
2 00000000 0x00430da8 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put() (??:??)
3 00000000 0x00447455 in std::ostream::_M_insert<double>() (??:??)
4 00000000 0x00448988 in std::ostream::operator<<() (??:??)
5 0041CB37 Queue<float>::Print(this=0x28ff00)
I've been stuck on this for a few days now, any help would be greatly appreciated.

It looks like you're implementing a fixed-size circular buffer. If so (or even if not) you have a few issues:
If you enqueue more than the maximum size of the queue before taking anything out of it, it will never register as full.
If your "front" pointer is greater than your rear pointer, your print function will never stop, and front will continue until MAX_INT and maybe loop around again. You're not doing the mod operation on the maximum size of the buffer.
You have no destructor, so your buffer will leak every time you make and destroy one of these objects.
Your length function is incorrect. Any time front is larger than rear (which is half the time) it will be in error. Think of it this way, when it's full, size will say zero.
And maybe a few other things than that. I'd re-think your design. You're close, but you have a few math errors.

Related

How to index array of pointers to arrays [queue]?

I am trying program a queue with arrays in C++.
I used this approach https://stackoverflow.com/a/936709/7104310 as shown below.
My question: How can I index the arrays to fill them?
In a normal 2d-array it would be arr[3][2] for example. But I do not know how to do this with pointers. The question hat not been answered in the Solution upon.
Thank you!
#include <iostream>
#define MAX_SIZE 3
using namespace std;
// ary[i][j] is then rewritten as
//arr[rear*capacity + front]
// Class for queue
class msg_queue
{
char **arr; // array to store queue elements
int capacity; // maximum capacity of the queue
int front; // front points to front element in the queue (if any)
int rear; // rear points to last element in the queue
int count; // current size of the queue
public:
msg_queue(int size = MAX_SIZE, int slot_length = MAX_SIZE); // constructor
void dequeue();
void enqueue(char x);
char peek();
int size();
bool isEmpty();
bool isFull();
};
// Constructor to initialize queue
msg_queue::msg_queue(int size, int slot_length)
{
arr = new char*[size];
for (int i = 0; i < size; ++i) {
arr[i] = new char[slot_length];
}
capacity = size;
front = 0;
rear = -1;
count = 0;
}
// Utility function to remove front element from the queue
void msg_queue::dequeue()
{
// check for queue underflow
if (isEmpty())
{
cout << "UnderFlow\nProgram Terminated\n";
exit(EXIT_FAILURE);
}
cout << "Removing " << arr[front] << '\n';
front = (front + 1) % capacity;
count--;
}
// Utility function to add an item to the queue
void msg_queue::enqueue(char item)
{
// check for queue overflow
if (isFull())
{
cout << "OverFlow\nProgram Terminated\n";
exit(EXIT_FAILURE);
}
cout << "Inserting " << item << '\n';
rear = (rear + 1) % capacity;
arr[rear] = item; //ERROR HERE
count++;
}
// Utility function to return front element in the queue
char msg_queue::peek()
{
if (isEmpty())
{
cout << "UnderFlow\nProgram Terminated\n";
exit(EXIT_FAILURE);
}
return arr[front]; //ERROR HERE
}
Well, it's still arr[3][2].
Although arrays are not pointers, the way we use them is effectively using a pointer because of the way they work and the way their name decays.
x[y] is *(x+y), by definition.
That being said, I would recommend you drop the 2D dynamic allocation (which is poison for your cache) and create one big block of Width×Height chars instead. You can use a little bit of maths to provide 2D indexes over that data.
Also you forgot to free any of that memory. If you use a nice std::vector to implement my suggested 1D data scheme (or even if you hire a vector of vectors, but ew!) then it'll be destroyed for you. Of course if you could do that then you'd probably be using std::queue…

Segmentation fault in C++ with std::cin while macro is defined

I am trying to solve a problem related to stack data structure, I have an implementation of a stack, and a main method that uses it, this is a for-learning question as i am a beginner, can you guys tell me, why i get this error?:
GDB trace:
Reading symbols from solution...done.
[New LWP 24202]
Core was generated by `solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 main () at solution.cc:70
70 cin >> N;
#0 main () at solution.cc:70
my code is the following:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_SIZE 5000000
class Stack
{
private:
int A[MAX_SIZE]; // array to store the stack
int top; // variable to mark the top index of stack.
public:
// constructor
Stack()
{
top = -1; // for empty array, set top = -1
}
// Push operation to insert an element on top of stack.
void Push(int x)
{
if(top == MAX_SIZE -1) { // overflow case.
printf("Error: stack overflow\n");
return;
}
A[++top] = x;
}
// Pop operation to remove an element from top of stack.
void Pop()
{
if(top == -1) { // If stack is empty, pop should throw error.
printf("Error: No element to pop\n");
return;
}
top--;
}
// Top operation to return element at top of stack.
int Top()
{
return A[top];
}
// This function will return 1 (true) if stack is empty, 0 (false) otherwise
int IsEmpty()
{
if(top == -1) return 1;
return 0;
}
// ONLY FOR TESTING - NOT A VALID OPERATION WITH STACK
// This function is just to test the implementation of stack.
// This will print all the elements in the stack at any stage.
void Print() {
int i;
printf("Stack: ");
for(i = 0;i<=top;i++)
printf("%d ",A[i]);
printf("\n");
}
};
int main() {
int N;
cin >> N;
Stack S1;
Stack S2;
for(int i = 0; i < N; i++)
{
int q;
cin >> q;
if(q == 1)
{
int x;
cin >> x;
if(S1.IsEmpty() || S2.IsEmpty())
{
S1.Push(x);
S2.Push(x);
}
else
{
S1.Push(x);
if(x >= S2.Top()) S2.Push(x);
}
}
if(q==2)
{
if(S1.Top() == S2.Top())
{
S1.Pop();
S2.Pop();
}else
{
S1.Pop();
}
}
if(q==3)
{
cout << S2.Top() << endl;
}
}
return 0;
}
if i set MAX_SIZE variable to a lower number the code runs well, i want to know why is that the case, how std::cin and macros interact??, i am a beginner, sorry if this is a simple question it is the first time that i am asking in stackoverflow,
MAX_SIZE is far too big. MAX_SIZE determines the size of your Stack objects. As the total size of local variables in a function is limited to a few megabytes (depending on the platform), you simply exceed this size.
In your case you have two local Stack objects in main (S1and S2), each of them taking roughly 20 Mbytes (assuming sizeof int is 4).
This is totally unrelated to cin though.
Your Stack objects are allocated on the stack.
By default the stack is limited to something like 1-8 MB per thread depending on your platform.
Each of your stack objects takes up 20 MB so you are running out of stack space. To fix this change your code to:
std::unique_ptr<Stack> S1(new Stack());
std::unique_ptr<Stack> S2(new Stack());
This will allocate your objects on the heap which is only limited by the size of the available memory and swap space on your machine.

Seg. fault resizing array C++

I have a priority queue array that is filled with "Jobs" (name + priority). I've been able to get everything queue related working aside from re sizing if it is full. Here is the bits that I think are causing a segmentation fault that I haven't been able to figure out.
EDIT:
Here is a bit more code that will compile, I left in the rest of the functions in case those might help in any way. Right now the initial capacity is set to 5, when you try to add a job to the full list it will double the capacity of the array and allow you to add a couple more jobs before a SEG. fault.
pq.h
#ifndef PQ_H
#define PQ_H
#include "interface.h"
#include <string>
using namespace std;
class Job {
public:
int getPriority();
string getTaskName();
void setPriority(int val);
void setTaskName(string tname);
Job();
private:
int priority;
string taskName;
};
class PriorityQueue {
public:
PriorityQueue();
~PriorityQueue();
int size();
bool isEmpty();
void clear();
void enqueue(string value, int priority);
string dequeue();
string peek();
int peekPriority();
PriorityQueue(const PriorityQueue & src);
PriorityQueue & operator=(const PriorityQueue & src);
private:
static const int INITIAL_CAPACITY = 5;
Job *array;
int count;
int capacity;
void expandCapacity() {
Job *oldArray = array;
capacity *= 2;
array = new Job[capacity];
for (int i = 0; i < count; i++) {
array[i] = oldArray[i];
}
delete[] oldArray;
}
};
#endif
pq.cpp
#include <iostream>
#include <cstring>
using namespace std;
//#include "job.h"
#include "pq.h"
Job::Job() // Constructor
{
priority= 0;
taskName = "There are no items in the list.";
}
int Job::getPriority(){ // returns the prority of the job
return priority;
}
string Job::getTaskName(){ // returns the name of the job
return taskName;
}
void Job::setPriority(int val){ // sets the priority of a newly created job
priority = val;
}
void Job::setTaskName(string tname){ // sets the name of a new job
taskName = tname;
}
PriorityQueue::PriorityQueue() // constructor
{
count = 0;
capacity = INITIAL_CAPACITY - 1;
array = new Job[INITIAL_CAPACITY];
}
PriorityQueue::~PriorityQueue() { // destructor
delete [] array;
}
int PriorityQueue::size() { // returns the number of jobs in the queue
return count;
}
bool PriorityQueue::isEmpty() { // returns true if queue is empty
if (count != 0){
return false;
}else{
return true;
}
}
void PriorityQueue::clear() { // clears queue of all jobs
count = 0;
// need to make it remove and delete the items
}
void PriorityQueue::enqueue(string value, int priority) {
// tests size to see if Queue is a max capacity
if(count == capacity){
expandCapacity();
cout << "\tList was full and has been expanded\n";
}
array[++count].setPriority(priority);
array[count].setTaskName(value);
// upheap operations
Job v = array[count];
int tempcount = count;
while (array[tempcount/2].getPriority() >= v.getPriority()){
array[tempcount] = array[tempcount/2];
tempcount = tempcount/2;
array[tempcount] = v;
}
}
string PriorityQueue::dequeue() {
// removes the job with the highest priority from the queue and returns the name
if(this->isEmpty()){ // make sure the queue isnt empty
string empty = "The queue is empty";
return empty;
}else{
Job remove = array[1];
array[1] = array[count--];
int j;
Job v;
int k = 1;
v = array[k];
while(k <= count/2){
cout << "dequeuewhile"; // test
j = k + k;
if(j < count && array[j].getPriority() > array[j+1].getPriority()){
j++;
cout << "dequeueloop if1"; // test
}
if(v.getPriority() <= array[j].getPriority()){
cout << "dequeueloop if2"; //test
break;
}
array[k] = array[j];
k = j;
}
array[k] = v;
return remove.getTaskName(); // returns the name of the removed job
}
}
string PriorityQueue::peek() { // returns the name of the highest priority job without removing it from the queue
if(count == 0){
return array[0].getTaskName();
}
return array[1].getTaskName();
}
int PriorityQueue::peekPriority() { // returns the priority from the highest priority job without removing it from the queue
if(count == 0){
cout << "\tThere are no items in the list.\n";
return array[0].getPriority();
}
return array[1].getPriority();
}
I think that when you do ++count, the next use of count will be out of bounds for the array.
array[++count].setPriority(priority);
// SEGMENTATION FAULT HERE
array[count].setTaskName(value);
If the capacity of the array is 5, and count was 4, then you just incremented count to 5, and tried to access element 5, which is out-of-bounds.
array = new Job[capacity];
for (int i = 0; i < count; i++) {
array[i] = oldArray[i];
}
Lets assume capacity is 10, so you've got an array of 10 elements, ranging from elements 0 to 9.
counttells us how many elements are being used.
If count happens to be 9, then when you increment count by one, it is now 10. Then, when line come you marked as producing segment fault comes, you're trying to access element 10, in our example. There is no element 10in an array of length 10, so you're out of bounds.
array[++count].setPriority(priority); // array[10], but last element is 9!
// SEGMENTATION FAULT HERE
array[count].setTaskName(value); // array[10], but last element is 9!
And, of course, everything after that part causes the same issue, as you keep using array[count].
Your original code did exactly as the previous answer given by #antiHUMAN.
The problem you're having is mixing or erroneously using 0-based and 1-based concepts.
Your first mistake is to make capacity a 0-based number. The capacity should denote the maximum number of items in an array, thus you should not be subtracting 1 from it. If the array can hold 5 items, then capacity should be 5, not 4.
PriorityQueue::PriorityQueue() // constructor
{
count = 0;
capacity = INITIAL_CAPACITY; // this remains 1-based.
array = new Job[INITIAL_CAPACITY];
}
or using the initializer-list:
PriorityQueue::PriorityQueue() : count(0),
capacity(INITIAL_CAPACITY),
array(new Job[INITIAL_CAPACITY]) {}
The 0-based number in your situation should be count, not capacity. Given that, since count is 0-based, and capacity is 1-based, your test in enqueue needs to be changed:
if(count + 1 == capacity){
expandCapacity();
cout << "\tList was full and has been expanded\n";
}
Note that 1 is added to count to account for the fact that count is 0-based and capacity is 1 based.

Implementing Queue With Two Stacks - Dequeuing Test Issue?

I'm trying to implement a queue with two stacks for purposes of understanding both data structures a little better. I have the below, with the main function serving as a test:
#include <iostream>
#include <stack>
using namespace std;
template <class T>
class _Stack : public stack<T> {
public:
T pop(){
T tmp=stack::top();
stack::pop();
return tmp;
}
};
template <class T>
class QueueS {
public:
QueueS(){}
bool isEmpty() const{
return pool.empty();
}
void enqueue(const T& el){
while( !output.empty()) {
input.push(output.pop());
}
input.push(el);
}
T dequeue(){
while(!input.empty()){
output.push(input.pop());
}
return output.pop();
}
T firstElement(){
if(output.empty()) {
return NULL;
}
return output.top();
}
private:
_Stack<T> pool;
_Stack<T> input;
_Stack<T> output;
};
int main(){
QueueS<int> n_QueueS;
//fill the queue of integers 0-9
for(int i=0; i<10;i++)
n_QueueS.enqueue(i);
// add another number to the queue
n_QueueS.enqueue(50);
//retrieve the first element without removing it
cout<<"front of the queue: "<<n_QueueS.firstElement()<<endl;
// removing the first 5 elements from the queue
cout<<"deleting first five elements of the queue: ";
for(int i=0; i<5;i++)
cout<<n_QueueS.dequeue()<<" ";
//removing the remainder of the queue and displaying the result
//should see 5 6 7 8 9 50 - see nothing!
cout<<endl<<"deleting remainder of the queue: ";
while(!n_QueueS.isEmpty())
cout<<n_QueueS.dequeue()<<" ";
if(n_QueueS.isEmpty())
cout<<endl<<"Queue is now empty";
else
cout<<endl<<"Error in emptying the queue";
system("pause");
return 0;
}
It works pretty well thusfar. However, when I run my test, deleting the first five elements works fine, and they display fine. It displays the line "deleting first five elements of the queue:" followed by 0 1 2 3 4, as expected.
However, deleting the second half doesn't display the values after the text "deleting remainder of the queue" like the previous test case did. I'm assuming the problem is minor, but I can't locate it through debugging. Maybe I've overlooked something?
Any help would be greatly appreciated!
First of all, your empty check should be something like this:
bool isEmpty() const{
return input.empty() && output.empty();
}
in enqueue, just push to the input stack:
void enqueue(const T& el){
input.push(el);
}
in enqueue and dequeue, move input to output if output is empty:
T dequeue(){
if (output.empty())
while(!input.empty()){
output.push(input.pop());
}
// throw exception of output.empty() ??
return output.pop();
}
T firstElement(){
if (output.empty())
while(!input.empty()){
output.push(input.pop());
}
if(output.empty()) {
return T(0); // throw exception?
}
return output.top();
}

Implementing a simple queue using arrays

I don't know much about arrays and queues and stacks. I know how to implement a simple queue.
#include <iostream>
#include <queue>
using namespace std;
void main()
{
queue<char> queue1;
queue1.push('a');
queue1.push('b');
queue1.push('c');
queue1.push('d');
while(!queue1.empty())
{
cout << queue1.front();
queue1.pop();
cout << endl;
}
system("pause");
}
How can I implement a simple queue using an array?
If your queue is based on an array, then for efficiency's sake, I would recommend creating a bounded or "circular" queue, where the max-size of the queue is fixed, and you basically have a head and tail pointer that point to the "first" and "last" positions in the queue's array, and when the tail-pointer (or an index value) moves to a position "past" the end of the array, it actually moves back to the beginning of the array. An unbounded queue based on an array would be horribly inefficient, as you would need to keep reallocating memory each time you fill up the max-size of the array, and/or attempt to re-shuffle elements down the array when you remove the first element of the queue.
Using integral-type array indexes for head and tail rather than actual pointer types, along with a counter for determining the overall number of items in your queue, your enqueue and dequeue functions could look as simple as:
template<typename T>
bool queue<T>::enqueue(const T& item)
{
if (count == array_size)
return false;
array[tail] = item;
tail = (tail + 1) % array_size;
count++;
return true;
}
template<typename T>
bool queue<T>::dequeue(T& item)
{
if (!count)
return false;
item = array[head];
head = (head + 1) % array_size;
count--;
return true;
}
You can extend this concept to whatever other functions you'd like, i.e., if you'd rather have a separate functions like the STL uses for accessing the head of the queue and actually "removing" an element from the queue.
NOTE: While simulating an array(linear data storage) as a circular data storage and maintaining the properties of Queue, one cell will always be unused. Hence, the maximum capacity of array will be 5 for the array having 6 cells. The c++ code below is self explanatory. Also, see The Linked List Based Implementation of Queue.
/*Implementation of queue with basic operation using arrays */
#include<iostream>
using namespace std;
#define MAX 6 //to accomodate a maximum of 05 elements as 1 cell pointed by tail will always be vacant
void ENQUE(int key); // ~insertion
int DEQUEUE(); // ~deletion
void TRAVERSE();
bool isEmpty();
bool isFull ();
int Q[MAX], head=0, tail=0; /* Note: head is the side facing cashier and new person joins the queue at tail. So, from cashier point of view tail~rear and head~front.
Q -> [h ][][][][][][][][][][t]
Q -> [h,t][][][][][][][][][][] : initial configuration*/
int main(){
int choice,val,i;
char ch='y';
do{
cout<<"1. For Enqueue \n";
cout<<"2. For Dequeue \n";
cout<<"3. For Traverse \nYour Option : ";
cin>>choice;
switch(choice)
{
case 1 : // insertion
if( isFull() ){
cout<<"\nQueue Full !!!\n";
break;
}
cin>>val;
ENQUE(val);
TRAVERSE();
break;
case 2 : //deletion
if( isEmpty() ){
cout<<"\nQueue Empty !!!\n";
break;
}
cout<<"\nDeleted element from Queue : "<<DEQUEUE()<<endl;
TRAVERSE();
break;
case 3 : //traversal
if( isEmpty() ){
cout<<"\nQueue Empty !!!\n";
break;
}
TRAVERSE();
break;
default :
cout<<"Please choose 1/2/3 !!! \n";
}
cout<<"\nDo you want to continue(y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y'); //end of do loop
return 0;
}
void ENQUE(int x){
Q[tail] = x;
tail =(tail+1)%MAX ; //OR tail = (tail==MAX) ? 0 : tail+1 ; */
}
int DEQUEUE(){
int temp =Q[head];
head =(head+1)%MAX ; //OR head = (head==MAX) ? 0 : head+1 ; */
return temp;
}
void TRAVERSE(){
int i; //simple case: Q -> [ ][ ][h7][8][9][5t][ ][ ][ ][ ][ ]
for(i=head; i!=tail; i=(i+1)% MAX) //complex case: Q -> [16][t][ ][ ][ ][h5][11][12][13][14][15]
cout<<Q[i]<<" ";
cout<<endl;
}
bool isEmpty(){
if(head == tail)
return true;
else
return false;
}
bool isFull(){
if( (tail == MAX-1 && head == 0) || (head == tail + 1) )
return true;
else
return false;
}
A video tutorial of the same can be seen here : Data structures: Array implementation of Queue