• No se han encontrado resultados

¡Revisa tu árbol!

In circular queues the elements Q[0],Q[1],Q[2] .... Q[n – 1] is represented in a circu- lar fashion with Q[1] following Q[n]. A circular queue is one in which the insertion of a new element is done at the very first location of the queue if the last location at the queue is full.

Suppose Q is a queue array of 6 elements. Push and pop operation can be per- formed on circular. The following figures will illustrate the same.

Q[0] Q[1] Q[5] Q[4] Q[2] Q[3] 18 7 42 67 Front Rear

Q[0] Q[1] Q[5] Q[4] Q[2] Q[3] 67 42 Front Rear

Fig. 4.12. A circular queue after popping 18, 7

After inserting an element at last location Q[5], the next element will be inserted at the very first location (i.e., Q[0]) that is circular queue is one in which the first element comes just after the last element.

Q[0] Q[1] Q[5] Q[2] Q[3] 42 67 Front Rear 30 47 14

Fig. 4.13. A circular queue after pushing 30, 47, 14

At any time the position of the element to be inserted will be calculated by the relation Rear = (Rear + 1) % SIZE

After deleting an element from circular queue the position of the front end is calcu- lated by the relation Front= (Front + 1) % SIZE

After locating the position of the new element to be inserted, rear, compare it with front. If (rear = front), the queue is full and cannot be inserted anymore.

4.3.1. ALGORITHMS

Let Q be the array of some specified size say SIZE. FRONT and REAR are two point- ers where the elements are deleted and inserted at two ends of the circular queue. DATA is the element to be inserted.

Inserting an element to circular Queue 1. Initialize FRONT = – 1; REAR = 1 2. REAR = (REAR + 1) % SIZE 3. If (FRONT is equal to REAR)

(a) Display “Queue is full” (b) Exit

4. Else

(a) Input the value to be inserted and assign to variable “DATA” 5. If (FRONT is equal to – 1)

(a) FRONT = 0 (b) REAR = 0 6. Q[REAR] = DATA

7. Repeat steps 2 to 5 if we want to insert more elements 8. Exit

Deleting an element from a circular queue 1. If (FRONT is equal to – 1)

(a) Display “Queue is empty” (b) Exit

2. Else

(a) DATA = Q[FRONT] 3. If (REAR is equal to FRONT)

(a) FRONT = –1 (b) REAR = –1 4. Else

(a) FRONT = (FRONT +1) % SIZE

5. Repeat the steps 1, 2 and 3 if we want to delete more elements 6. Exit

PROGRAM 4.2

/// PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY //CODED AND COMPILED USING TURBO C++

#include<conio.h> #include<process.h> #include<iostream.h> #define MAX 50

//A class is created for the circular queue class circular_queue

{

int cqueue_arr[MAX]; int front,rear; public:

//a constructor is created to initialize the variables circular_queue()

{

front = –1; rear = –1; }

//public function declarations void insert();

void del(); void display(); };

//Function to insert an element to the circular queue void circular_queue::insert()

{

int added_item;

//Checking for overflow condition

if ((front == 0 && rear == MAX-1) || (front == rear +1)) {

cout<<“\nQueue Overflow \n”; getch();

return; }

if (front == –1) /*If queue is empty */ {

front = 0; rear = 0; }

else

if (rear == MAX-1)/*rear is at last position of queue */ rear = 0;

else

rear = rear + 1;

cout<<“\nInput the element for insertion in queue:”; cin>>added_item;

cqueue_arr[rear] = added_item; }/*End of insert()*/

//This function will delete an element from the queue void circular_queue::del()

{

//Checking for queue underflow if (front == –1)

{

cout<<“\nQueue Underflow\n”; return;

}

cout<<“\nElement deleted from queue is:”<<cqueue_arr[front]<<“\n”; if (front == rear) /* queue has only one element */

{ front = –1; rear = –1; } else if(front == MAX-1) front = 0; else front = front + 1; }/*End of del()*/

//Function to display the elements in the queue void circular_queue::display()

{

int front_pos = front,rear_pos = rear;

//Checking whether the circular queue is empty or not if (front == –1)

{

cout<<“\nQueue is empty\n”; return;

//Displaying the queue elements cout<<“\nQueue elements:\n”; if(front_pos <= rear_pos ) while(front_pos <= rear_pos) { cout<<cqueue_arr[front_pos]<<“, ”; front_pos++; } else { while(front_pos <= MAX-1) { cout<<cqueue_arr[front_pos]<<“, ”; front_pos++; } front_pos = 0; while(front_pos <= rear_pos) { cout<<cqueue_arr[front_pos]<<“, ”; front_pos++; } }/*End of else*/ cout<<“\n”; }/*End of display() */ void main() { int choice;

//Creating the objects for the class circular_queue co; while(1) { clrscr(); //Menu options cout <<“\n1.Insert\n”; cout <<“2.Delete\n”; cout <<“3.Display\n"; cout <<“4.Quit\n”;

cout <<“\nEnter your choice: ”; cin>>choice;

{ case 1: co.insert(); break; case 2 : co.del(); getch(); break; case 3: co.display(); getch(); break; case 4: exit(1); default: cout<<“\nWrong choice\n”; getch(); }/*End of switch*/ }/*End of while*/ }/*End of main()*/

4.4. DEQUES

A deque is a homogeneous list in which elements can be added or inserted (called push operation) and deleted or removed from both the ends (which is called pop opera- tion). ie; we can add a new element at the rear or front end and also we can remove an element from both front and rear end. Hence it is called Double Ended Queue.

Fig. 4.14. A deque

There are two types of deque depending upon the restriction to perform insertion or deletion operations at the two ends. They are

1. Input restricted deque 2. Output restricted deque

An input restricted deque is a deque, which allows insertion at only 1 end, rear end, but allows deletion at both ends, rear and front end of the lists.

An output-restricted deque is a deque, which allows deletion at only one end, front end, but allows insertion at both ends, rear and front ends, of the lists.

The possible operation performed on deque is 1. Add an element at the rear end

2. Add an element at the front end 3. Delete an element from the front end 4. Delete an element from the rear end

Only 1st, 3rd and 4th operations are performed by input-restricted deque and 1st, 2nd and 3rd operations are performed by output-restricted deque.

4.4.1. ALGORITHMS FOR INSERTING AN ELEMENT

Let Q be the array of MAX elements. front (or left) and rear (or right) are two array index (pointers), where the addition and deletion of elements occurred. Let DATA be the element to be inserted. Before inserting any element to the queue left and right pointer will point to the – 1.

INSERT AN ELEMENT AT THE RIGHT SIDE OF THE DE-QUEUE