📅  最后修改于: 2020-10-15 00:57:28             🧑  作者: Mango
就线性队列而言,删除和插入只能分别在前端和后端进行。
考虑下图所示的队列。
上图所示的队列已完全填满,由于后方== max-1变为true的条件,因此无法再插入任何元素。
但是,如果我们删除队列前端的2个元素,由于条件after = max -1仍然成立,我们仍然不能插入任何元素。
这是线性队列的主要问题,尽管数组中有可用空间,但是不能在队列中插入更多元素。这仅仅是内存浪费,我们需要克服这个问题。
解决此问题的方法之一是循环队列。在循环队列中,第一个索引紧接在最后一个索引之后。您可以想到一个循环队列,如下图所示。
当front = -1和back = max-1时,循环队列将已满。循环队列的实现类似于线性队列的实现。只有在插入和删除的情况下实现的逻辑部分才与线性队列中的逻辑部分不同。
时间复杂度
Front | O(1) |
Rear | O(1) |
enQueue() | O(1) |
deQueue() | O(1) |
在队列中插入元素的情况有三种。
void insert(int item, int queue[])
{
if((rear+1)%maxsize == front)
{
printf("\nOVERFLOW");
return;
}
else if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else if(rear == maxsize -1 && front != 0)
{
rear = 0;
}
else
{
rear = (rear+1)%maxsize;
}
queue[rear] = item;
}
要从循环队列中删除元素,我们必须检查以下三个条件。
void delete()
{
if(front == -1 & rear == -1)
{
printf("\nUNDERFLOW\n");
return;
}
else if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == maxsize -1)
{
front = 0;
}
else
front = front + 1;
}
#include
#include
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("%d",&item);
if((rear+1)%maxsize == front)
{
printf("\nOVERFLOW");
return;
}
else if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else if(rear == maxsize -1 && front != 0)
{
rear = 0;
}
else
{
rear = (rear+1)%maxsize;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void delete()
{
int item;
if(front == -1 & rear == -1)
{
printf("\nUNDERFLOW\n");
return;
}
else if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == maxsize -1)
{
front = 0;
}
else
front = front + 1;
}
void display()
{
int i;
if(front == -1)
printf("\nCircular Queue is Empty!!!\n");
else
{
i = front;
printf("\nCircular Queue Elements are : \n");
if(front <= rear){
while(i <= rear)
printf("%d %d %d\n",queue[i++],front,rear);
}
else{
while(i <= maxsize - 1)
printf("%d %d %d\n", queue[i++],front,rear);
i = 0;
while(i <= rear)
printf("%d %d %d\n",queue[i++],front,rear);
}
}
}
输出:
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter the element
1
Value inserted
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter the element
2
Value inserted
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter the element
3
Value inserted
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
Circular Queue Elements are :
1
2
3
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?2
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter the element
4
Value inserted
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?3
Circular Queue Elements are :
2
3
4
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?1
Enter the element
1
OVERFLOW
**********Main Menu**********
=============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice ?
4