简体   繁体   中英

How to store (x,y) coordinates within a queue in C

I am doing a breadth first search algorithim and I am having trouble storing the starting coordinates within my Queue implementation as (x,y) as one element within the queue.

I have used arrays and structs but it keeps entering in each int value as two separated queue elements. Is there a way to store both elements as one queue?

struct queue {
int items[SIZE];
int front;
int rear;
};

struct queue* createQueue();

 struct queue* createQueue() {
    struct queue* q = malloc(sizeof(struct queue));
    q->front = -1;
    q->rear = -1;
    return q;
}

struct queue* q = createQueue();  
int c[2] = {2,3};

enqueue(q, c);
enqueue(q, 6);
printQueue(q);

void enqueue(struct queue* q, int value){
    if(q->rear == SIZE-1)
        printf("\nQueue is Full!!");
    else {
        if(q->front == -1)
            q->front = 0;
        q->rear++;
        q->items[q->rear] = value;
    }
}

Expected:

Queue Contains: (2,3), 6

As far as I understand you want a queue where you can store both items that represents integer pairs (aka x,y ) and items that represents a single integer.

That is not possible with the queue you define:

struct queue {
    int items[SIZE];  <--- each item can only hold a single integer! Not a pair of integers
    int front;
    int rear;
};

So you need another data structure.

Further, your enqueue function are called with two different types of arguments

int c[2] = {2,3};
enqueue(q, c);     <--- here the argument is a int-pointer (because c decays into int-pointer)
enqueue(q, 6);     <--- here the argument is a integer

That is not allowed in C. So you need two different functions. One for pairs and one for integers.

There are many different ways to implement what you ask for. Below is one example that uses a struct dataitem to store the actual data. The size field is used to check if the stored data is a pair or just a single integer.

#include <stdio.h>
#include <stdlib.h>

#define SIZE 8

struct dataitem
{
  int size;     // Will be 2 for pairs and 1 for single integers
  int data[2];
};

struct queue {
  struct dataitem items[SIZE];
  int front;
  int rear;
};


struct queue* createQueue() {
  struct queue* q = malloc(sizeof *q);
  q->front = 0;
  q->rear = 0;
  return q;
}

int full(struct queue* q)
{
  return (((q->rear + 1) % SIZE) == q->front);
}

int empty(struct queue* q)
{
  return (q->rear == q->front);
}

// Add single integer
void enqueue_int(struct queue* q, int value){
  if (full(q))
  {
    printf("\nQueue is Full!!");
    return;
  }

  q->items[q->rear].size = 1;
  q->items[q->rear].data[0] = value;
  q->rear = (q->rear + 1) % SIZE;
}

// Add pair of integers
void enqueue_pair(struct queue* q, int* values){
  if (full(q))
  {
    printf("\nQueue is Full!!");
    return;
  }

  q->items[q->rear].size = 2;
  q->items[q->rear].data[0] = values[0];
  q->items[q->rear].data[1] = values[1];
  q->rear = (q->rear + 1) % SIZE;
}

void printQueue(struct queue* q)
{
  int tmp = q->front;
  printf("Queue Contains: ");
  int flag = 0;
  while(tmp != q->rear)
  {
    if (flag) printf(", ");
    flag = 1;
    if (q->items[tmp].size > 1)
    {
      printf("(%d,%d)", q->items[tmp].data[0], q->items[tmp].data[1]);
    }
    else
    {
      printf("%d", q->items[tmp].data[0]);
    }
    tmp = (tmp + 1) % SIZE;
  }
  printf("\n");
}

struct dataitem dequeue(struct queue* q)
{
    struct dataitem res;
    if (empty(q))
    {
        res.size = 0;
    }
    else
    {
        res = q->items[q->front];
        q->front = (q->front + 1) % SIZE;
    }
    return res;
}

int main()
{
  int c[2] = {2,3};
  struct queue* q = createQueue();
  enqueue_pair(q, c);
  enqueue_int(q, 6);
  printQueue(q);

  struct dataitem data;
  for (int i=0; i<3; ++i)
  {
    data = dequeue(q);
    if (data.size == 0)
    {
      printf("queue was empty\n");
    }
    else if (data.size == 1)
    {
      printf("queue had integer %d\n", data.data[0]);
    }
    else
    {
      printf("queue had pair (%d,%d)\n", data.data[0], data.data[1]);
    }
  }
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM