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

Tree traversal, Q. What do you understand by the tree traversal? Write down...

Q. What do you understand by the tree traversal? Write down the procedure for traversing a binary tree in preorder and execute it on the following tree.    Ans: Th

Design and implement a software system, In this assignment, you are invited...

In this assignment, you are invited to design and implement a software system for catalogue sale. A catalogue is organised in a tree structure. Each node of the catalogue tree repr

In-order traversal, Write steps for algorithm for In-order Traversal Th...

Write steps for algorithm for In-order Traversal This process when implemented iteratively also needs a stack and a Boolean to prevent the execution from traversing any portion

Stack, how we will make projects on stack in c?

how we will make projects on stack in c?

Data manipulation, perform the following length operation LENGTH("welcome t...

perform the following length operation LENGTH("welcome to ICA")=

If-then-else statements, In this example, suppose the statements are simple...

In this example, suppose the statements are simple unless illustrious otherwise. if-then-else statements if (cond) { sequence of statements 1 } else { sequence of st

Red-black tree after insertion, The above 3 cases are also considered conve...

The above 3 cases are also considered conversely while the parent of Z is to the right of its own parent. All the different kind of cases can be illustrated through an instance. Le

Primitive data structure, Primitive Data Structure These are the basic ...

Primitive Data Structure These are the basic structure and are directly operated upon by the machine instructions. These in general have dissimilar representations on different

Explain the term group support system, (a) Explain the term Group Support S...

(a) Explain the term Group Support System and elaborate on how it can improve groupwork. (b) Briefly explain three advantages of simulation. (c) Explain with the help of a

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