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 structure, We would like to implement a 2-4Tree containing distinct in...

We would like to implement a 2-4Tree containing distinct integer keys. This 2-4Tree is defined by the ArrayList Nodes of all the 2-4Nodes in the tree and the special 2-4Node Root w

Linked list, write an algorithm for multiplication of two sparse matrices u...

write an algorithm for multiplication of two sparse matrices using Linked Lists

General, whats the definition of ADT and data type?

whats the definition of ADT and data type?

C++, #What is the pointer

#What is the pointer

Explain space complexity, Explain Space Complexity Space Complexity :...

Explain Space Complexity Space Complexity :- The space complexity of an algorithm is the amount of memory it requires to run to completion. Some of the reasons to study space

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

how we will make projects on stack in c?

Sequential files, In this section, we will discuss about Sequential file or...

In this section, we will discuss about Sequential file organization. Sequential files have data records stored in a particular sequence. A sequentially organized file might be stor

Write an algorithm to display this repeated calculation, The following form...

The following formula is used to calculate n: n = x * x/(1 - x) . Value x = 0 is used to stop algorithm. Calculation is repeated using values of x until value x = 0 is input. There

Red black tree, red black tree construction for 4,5,6,7,8,9

red black tree construction for 4,5,6,7,8,9

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