我如何将int与C中的队列进行比较?

问题描述 投票:-2回答:1

假设排队正确将数字添加到队列中。因此,我的队列应该为{1,2,3,4}。另外,我意识到这是很多代码。问题在于我相信的主要功能。编辑:我按照某些人的要求发布了整个代码,但是我意识到对于非专业人士来说,这似乎非常复杂。我只想将队列的内容与一个整数进行比较。

// C program for array implementation of queue
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// A structure to represent a queue
struct Queue
{
int front, rear, size;
unsigned capacity;
int* array;
};

// function to create a queue of given capacity.
// It initializes size of queue as 0
struct Queue* createQueue(unsigned capacity)
{
struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;  // This is important, see the enqueue
queue->array = (int*) malloc(queue->capacity * sizeof(int));
return queue;
}

// Queue is full when size becomes equal to the capacity
int isFull(struct Queue* queue)
{  return (queue->size == queue->capacity);  }

// Queue is empty when size is 0
int isEmpty(int Queue* queue)
{  return (queue->size == 0); }

// Function to add an item to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
    return;
queue->rear = (queue->rear + 1)%queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", item);
}

// Function to remove an item from queue.
// It changes front and size
int dequeue(int Queue queue)
{
if (isEmpty(queue))
    return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)%queue->capacity;
queue->size = queue->size - 1;
return item;
}

// Function to get front of queue
int front(int Queue queue)
{
if (isEmpty(queue))
    return INT_MIN;
return queue->array[queue->front];
}

// Function to get rear of queue
int rear(int Queue queue)
{
if (isEmpty(queue))
    return INT_MIN;
return queue->array[queue->rear];
}

int main()
{
    struct Queue* queue = createQueue(1000);
    enqueue(queue, 1);
    enqueue(queue, 2);
    enqueue(queue, 3);
    enqueue(queue, 4);
    int tag = 2;
    if (queue[0] = tag){ //if the first element of queue is 2, it should print. It is not.
        printf("Your first element of your queue is 2");
    }
    if (tag = queue[1]){ //this should print because the first element of queue is 2!
        printf("Your first element of your queue is 2");
    }
}

错误:

从类型'int'分配给类型'struct Queue'时不兼容的类型

c queue
1个回答
0
投票

错误消息清楚地指出了这一点:您是分配,而不是比较

将您的条件更改为

if (queue[0] == tag) {

if (tag == queue[1]) {

相等性的比较运算符是==,而不是=

© www.soinside.com 2019 - 2024. All rights reserved.