+3 votes
in Mathematics by kratos

Device a representation for a list where insertions and deletions can be made at either end. Such a structure is called a Deque (Double Ended Queue). Write functions for inserting and deleting at either end.

1 Answer

+2 votes
by kratos
 
Best answer

Dequeue

A Dequeue can be represented as follows

Let the total size of elements be n in this Dequeue represented as an array int array[n];

Then

front = index of the first element

Rear = index of the last element

If front = rear then we suppose that the Dequeue is empty

The Dequeue grows from front side till the index of the front becomes 0 and from rear side till the index of the rear equals the array size (n-1).

Now the functions for inserting at the front end of the Dequeue is:-

Insert_front ( int data )

{

if ( front == 0 )

printf (“Dequeue is full from the front end”);

else

{

front--;

array[front] = data;

}

}

Inserting from the rear end is:-

insert_rear ( int data )

{

if (rear == n-1)

printf(“Dequeue is full from rear end”);

else

{

rear++;

array [ rear ] = data;

}

}

Deleting from the front end: -

int delete_front ()

{

if (front == rear)

printf ( “the Dequeue is empty”);

else

return ( array [ front++ ] );

}

Deleting from the rear end:-

int delete_rear ()

{

if ( front == rear )

printf(“ the Dequeue is empty ”);

else

return ( array [ rear-- ] );

}

...