0

I have written a piece of code for queue.

#include <iostream>
using namespace std;

struct q
        {
            int items[10];
            int front,rear;
        } queue;

void Addqueue(struct q *queue, int item)
{
    if (queue->rear==9)
        cout << "Queue is Full.";
    else
        queue->items[++queue->rear]=item;
}


int main()
{
    queue.front=queue.rear=-1;
    Addqueue(q *queue,5);
    return 0;
}

As you can see I have used struct object for it. In Addqueue Function, first element is (struct q *queue), I want to know what should I write instead of it, while I'm caling this function in main function, for example I tested Addqueue(q *queue,5) and Addqueue(queue,5) and Addqueue(*queue,5) and Addqueue(struct q *queue,5), but none of them works and I will get an error for this line. so what should I do? what is wrong?

3 Answers 3

1
#include <iostream>
using namespace std;

struct q
{
    int items[10];
    int front,rear;
} queue;

void Addqueue(q *queue, int item)
{
    if (queue->rear==9)
        cout << "Queue is Full.";
    else
        queue->items[++queue->rear]=item;
}


int main()
{
    queue.front=queue.rear=-1;
    Addqueue(&queue, 5);
    return 0;
}

That's correct way.

Sign up to request clarification or add additional context in comments.

Comments

1

The correct syntax is Addqueue(&queue, 5). Addqueue accepts a pointer to a struct q, so you need to get queue's address with the address-of operator& to pass in.

That said, you should absolutely use std::queue<int> instead of your own home-grown queue.

Comments

1

You created pointer to struct q, but it has not allocated memory. And local name of pointer q *queue override your object queue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.