Array implementation of a dequeue, Data Structure & Algorithms

Assignment Help:

If a Dequeue is implemented via arrays, then this will suffer with the similar problems which a linear queue had suffered. Program 8 gives the array implementation of Dequeue.

Program: Array implementation of a Dequeue

#include "stdio.h"

#define QUEUE_LENGTH 10;

int dq[QUEUE_LENGTH];

 int front, rear, choice,x,y;

 main()

{

int choice,x;

front = rear = -1; /* initialize the front and rear to null i.e empty queue */

printf ("enter 1 for insertion of any element and 2 for eliminate element from the front of the queue");

printf ("enter 3 to add needed element  and 4 for  eliminate element from the rear of the queue"); printf("Enter your option");

scanf("%d",&choice);

switch (choice)

{

case 1:

printf ("Enter element to be added :");

scanf("%d",&x);

add_front(x);

break;

case 2:

delete_front();

break;

case 3:

printf ("Enter any element to insertion :");

scanf("%d ",&x);

add_rear(x);

break;

case 4:

delete_rear();

break;

}

}

 

/**************** Insertion at the front ***************/

add_front(int y)

{

if (front == 0)

{

printf("At the front position element cannot be inserted ");

return;

else

{

front = front - 1;

dq[front] = y;

if (front == -1 ) front = 0;

}

}

/**************** Deletion from the front ***************/

delete_front()

{

if front == -1

printf("Queue empty");

 

else

return dq[front];

if (front = = rear)

front = rear = -1

else

front = front + 1;

 }

/**************** Insertion at the rear ***************/

add_rear(int y)

if (front == QUEUE_LENGTH -1 )

{

printf("At the rear , element cannot be inserted ")

return;

else

{

rear = rear + 1;

dq[rear] = y;

if (rear = = -1 )

rear = 0;

}

}

/**************** Delete at the rear ***************/

delete_rear()

{

if rear == -1

printf("deletion is not attainable at rear position");

else

{

if (front = = rear)

front = rear = -1

else

{

rear = rear - 1;

return dq[rear];

}

}

}


Related Discussions:- Array implementation of a dequeue

Binary tree and binarytree parts, Q. What do you understand by the term Bin...

Q. What do you understand by the term Binary Tree? What is the maximum number of nodes which are possible in a Binary Tree of depth d. Explain the terms given below with respect to

Write a function that performs integer division, Write a function that perf...

Write a function that performs integer division. The function should take the large number in memory location 1 and divide it by the large number in memory location 2 disregarding

Explain class p problems, Explain class P problems Class  P  is  a  cla...

Explain class P problems Class  P  is  a  class  of  decision  problems  that  can  be  solved  in  polynomial time  by(deterministic) algorithms. This class of problems is kno

Search on a heap file, Consider the file " search_2013 ". This is a text fi...

Consider the file " search_2013 ". This is a text file containingsearch key values; each entry is a particular ID (in the schema given above). You are tosimulate searching over a h

Graph, explain the prims''s algorithm with suitable example?

explain the prims''s algorithm with suitable example?

C++ function, Write c++ function to traverse the threaded binary tree in in...

Write c++ function to traverse the threaded binary tree in inorder traversal

Shortest path dijkstras algorithm, * Initialise d & pi* for each vertex ...

* Initialise d & pi* for each vertex v within V( g ) g.d[v] := infinity  g.pi[v] := nil g.d[s] := 0; * Set S to empty * S := { 0 }  Q := V(g) * While (V-S)

File organization, Define File organization''s and it''s types

Define File organization''s and it''s types

Method for keeping two stacks within a single linear array, Q. Define a met...

Q. Define a method for keeping two stacks within a single linear array S in such a way that neither stack overflows until entire array is used and a whole stack is never shifted to

Define container in terms of object-oriented terms, Define container in te...

Define container in terms of  object-oriented terms A Container is a broad category whose instances are all more specific things; there is never anything which is just a Contai

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd