Show the circular queue using given values of array (size is 5) 15, 20, 30,10,5
Answers
Answer:
100 is the answerkjhvjcetb
Function for insertion :
void insert(int ele)
{
if(front == rear+1 || rear == size-1)
{
cout<<"Circular queue is full";
}
else if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
cqueue[rear] = ele;
}
else if(rear == size-1)
{
rear = 0;
cqueue[rear] = ele;
}
else
{
rear ++;
cqueue[rear] = ele;
}
}
Function for deletion :
void delete()
{
if(front == -1 && rear == -1)
{
cout<<"Circular queue is empty ";
}
else if(front == rear)
{
cout<<cqueue[front];
front = -1;
rear = -1;
}
else if(front == size-1)
{
cout<<cqueue[front];
front = 0;
}
else
{
cout<<cqueue[front];
front++;
}
}
Execution :
size = 5
Initially,
front = -1
rear = -1
Array index : 0 1 2 3 4
element : (Empty)
1) insert(15)
front = 0
rear = 0
Array index : 0 1 2 3 4
element : 15
2) insert(20)
front = 0
rear = 1
Array index : 0 1 2 3 4
element : 15 20
3) insert(30)
front = 0
rear = 2
Array index : 0 1 2 3 4
element : 15 20 30
4) insert(10)
front = 0
rear = 3
Array index : 0 1 2 3 4
element : 15 20 30 10
5) insert(5)
front = 0
rear = 4
Array index : 0 1 2 3 4
element : 15 20 30 10 5 (Full)
6) delete()
front = 1
rear = 4
Array index : 0 1 2 3 4
element : 20 30 10 5
7) delete()
front = 2
rear = 4
Array index : 0 1 2 3 4
element : 30 10 5
8) insert(15)
front = 2
rear = 0
Array index : 0 1 2 3 4
element : 15 30 10 5
9) insert(20)
front = 2
rear = 1
Array index : 0 1 2 3 4
element : 15 20 30 10 5 (Full)
10) delete()
front = 3
rear = 1
Array index : 0 1 2 3 4
element : 15 20 10 5
11) delete()
front = 4
rear = 1
Array index : 0 1 2 3 4
element : 15 20 5
12) delete()
front = 0
rear = 1
Array index : 0 1 2 3 4
element : 15 20
13) delete()
front = 1
rear = 1
Array index : 0 1 2 3 4
element : 20
14) delete()
front = -1
rear = -1
Array index : 0 1 2 3 4
element : (Empty)