队列让我陷入了奇怪的问题

时间:2017-08-29 22:00:05

标签: c data-structures

我正在尝试使用队列的数据结构,这是一种循环队列。这是以下代码。

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

enum boolean{
   false,true
};

struct ArrayQueue{
    int rare,front;
    int capacity;
    int *array;
};

struct ArrayQueue* CreateQueue(int cap)
{
    struct ArrayQueue* queue;
    queue = (struct ArrayQueue*)malloc(sizeof(struct ArrayQueue));
    queue->rare = -1;
    queue->front = -1;
    queue->capacity = cap;
    queue->array = (int*)malloc(sizeof(int)*queue->capacity);
    int i;
    for (i=0;i<queue->capacity;i++)
        queue->array[i] = 0;
    return(queue);
}

enum boolean isFull(struct ArrayQueue *queue)
{
    return((queue->rare+1)%queue->capacity == queue->front);
}

enum boolean isEmpty(struct ArrayQueue *queue)
{
    return(queue->front == -1);
}

int ElementsInQueue(struct ArrayQueue *queue)
{
    return(((queue->capacity-queue->front)+queue->rare+1)%queue->capacity);
}

void En_queue(struct ArrayQueue *queue,int item)
{
    if (isFull(queue) == false)
    {
        queue->rare = (queue->rare+1)%queue->capacity;
        queue->array[queue->rare] = item;
        if (queue->front == -1)
            queue->front = queue->rare;
    }
}

int De_queue(struct ArrayQueue *queue)
{
    int data;
    if (isEmpty(queue) == false)
    {
        data = queue->array[queue->front];
        queue->array[queue->front] = 0;
        if (queue->front == queue->rare)
        {
            queue->front = -1;
            queue->rare = -1;
        }
        else
        {
            queue->front = queue->front+1%queue->capacity;
        }
        return data;
    }
}

void Display(struct ArrayQueue *queue)
{
    int i;
    printf("\n Displaying the queue : \n");
    for (i=0;i<queue->capacity;i++)
        printf("%d ",queue->array[i]);
}

int main(void)
{
    int cap,choice,item;
    struct ArrayQueue *queue;
    printf("\nEnter the Capacity of Queue: ");
    scanf("%d",&cap);
    queue = CreateQueue(cap);
    while(1)
    {
        printf("\n\n");
        printf("\n1.Enqueue");
        printf("\n2.Dequeue");
        printf("\n3.Display");
        printf("\n4.Exit");
        printf("\nEnter choice: ");
        scanf("%d",&choice);
        switch (choice)
        {
        case 1: if (isFull(queue) == true)
                {
                    printf("\nQueue is Full..");
                    break;
                }
                else
                {
                   printf("\nEnter a number: ");
                   scanf("%d",&item);
                   En_queue(queue,item);
                   Display(queue);
                }
                break;
        case 2: if (isEmpty(queue)== true)
                {
                    printf("\nQueue is Empty..");
                    break;
                }
                else
                {
                    item = De_queue(queue);
                    printf("\nItem popped is: %d",item);
                    Display(queue);
                }
                break;
        case 3: Display(queue);
                break;
        case 4: exit(0);
                break;
        }

    }
}
乍一看它看起来很完美,但问题很少。

查看截图

插入后

2nd. Deleting and inserting simultaneously 3rd. the problem occured

对不起,我真的很想解释一下。

我的输入流是(根据这种模式)......

  1. 入队
  2. 出列
  3. 显示
  4. 退出
  5. {1,1,1,1,2,1,2,1,2,1,2,1,2,1}

                        |___|
                      here is the problem
    
    • 当我输入2队列时,队列已满。但没有删除。输出是一些垃圾值
    • 然后我输入1表示队列已满,但没有显示队列已满。相反,它让我输入一个数字。

    (因为我先出列了所以我想首先将一个元素排入队列,然后输入选项为1.但是它又要求我输入一个数字。)

    我无法弄清楚错误。原谅我的解释和奇怪的词汇。请尝试代码并输入输入流以找出问题。

    请指导我到底出了什么问题。 提前致谢。

1 个答案:

答案 0 :(得分:0)

这是正确的答案。谢谢大家:)

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

enum boolean{
   false,true
};

struct ArrayQueue{
    int rare,front;
    int capacity;
    int *array;
};

struct ArrayQueue* CreateQueue(int cap)
{
    struct ArrayQueue* queue;
    queue = (struct ArrayQueue*)malloc(sizeof(struct ArrayQueue));
    queue->rare = -1;
    queue->front = -1;
    queue->capacity = cap;
    queue->array = (int*)malloc(sizeof(int)*queue->capacity);
    int i;
    for (i=0;i<queue->capacity;i++)
        queue->array[i] = 0;
    return(queue);
}

enum boolean isFull(struct ArrayQueue *queue)
{
    return((queue->rare+1)%queue->capacity == queue->front);
}

enum boolean isEmpty(struct ArrayQueue *queue)
{
    return(queue->front == -1);
}

int ElementsInQueue(struct ArrayQueue *queue)
{
    return(((queue->capacity-queue->front)+queue->rare+1)%queue->capacity);
}

void En_queue(struct ArrayQueue *queue,int item)
{
    if (isFull(queue) == false)
    {
        queue->rare = (queue->rare+1)%queue->capacity;
        queue->array[queue->rare] = item;
        if (queue->front == -1)
            queue->front = queue->rare;
    }
}

int De_queue(struct ArrayQueue *queue)
{
    int data;
    if (isEmpty(queue) == false)
    {
        data = queue->array[queue->front];
        queue->array[queue->front] = 0;
        if (queue->front == queue->rare)
        {
            queue->front = -1;
            queue->rare = -1;
        }
        else
        {
            queue->front = (queue->front+1)%queue->capacity;
        }
        return data;
    }
}

void Display(struct ArrayQueue *queue)
{
    int i;
    printf("\n Displaying the queue : \n");
    for (i=0;i<queue->capacity;i++)
        printf("%d ",queue->array[i]);
}

int main(void)
{
    int cap,choice,item;
    struct ArrayQueue *queue;
    printf("\nEnter the Capacity of Queue: ");
    scanf("%d",&cap);
    queue = CreateQueue(cap);
    while(1)
    {
        printf("\n\n");
        printf("\n1.Enqueue");
        printf("\n2.Dequeue");
        printf("\n3.Display");
        printf("\n4.Exit");
        printf("\nEnter choice: ");
        scanf("%d",&choice);
        switch (choice)
        {
        case 1: if (isFull(queue) == true)
                {
                    printf("\nQueue is Full..");
                    break;
                }
                else
                {
                   printf("\nEnter a number: ");
                   scanf("%d",&item);
                   En_queue(queue,item);
                   Display(queue);
                }
                break;
        case 2: if (isEmpty(queue)== true)
                {
                    printf("\nQueue is Empty..");
                    break;
                }
                else
                {
                    item = De_queue(queue);
                    printf("\nItem popped is: %d",item);
                    Display(queue);
                }
                break;
        case 3: Display(queue);
                break;
        case 4: exit(0);
                break;
        }

    }
}