首页 > 解决方案 > 当我尝试插入第二个元素时,循环队列插入元素问题显示队列已满

问题描述

在我的循环队列中,当我输入第一个元素时,它完美地进入了循环队列,但是当我在队列中输入第二个元素时,它显示队列已满,而数组的大小为 5,即使我调试它,我在输入第一个元素后发现它给出队列已满 '''

 #include<stdio.h>
    #include<stdlib.h>
    # define SIZE 5
    int cqueue[SIZE];
    int front = -1;
    int rear = -1;
    void insert(int);
    void main()
    {
        int ch,item;
        while(1)
        {
            printf("\n select choice from given below operations of circular queue : ");
            printf("\n 1.insert element");
            printf("\n 2. delete element");
            printf("\n 3. display elements");
            printf("\n 4. exit");

            printf("\n enter choice : ");
            scanf("%d",&ch);

            switch(ch)
            {
                case 1 : printf("enter element : ");
                         scanf("%d",&item);
                         insert(item);
                         break;
                case 4 : exit(0);

                default: printf("\ninvalid choice! enter again\n");
            }
        }
    }

    void insert(int ele)
    {
        if(front=rear+1 || SIZE==rear+1)
        {
            printf("\n circular queue is full ");
        }
        else if((rear==-1)&&(front==-1))
        {
            front=rear=0;
            cqueue[rear]=ele;
        }
        else if(rear==SIZE-1)
        {
            rear=0;
            cqueue[rear]=ele;
        }
        else
        {
            rear++;
            cqueue[rear]=ele;
        }
    }

'''

标签: c

解决方案


插入功能中,您有 if(front=rear+1 || SIZE==rear+1). 将其更改为if(front == rear + 1 .... 所以你基本上每次被调用时都分配rear + 1给变量。frontinsert


推荐阅读