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

Best case, Best Case: If the list is sorted already then A[i] T (n) = ...

Best Case: If the list is sorted already then A[i] T (n) = c1n + c2 (n -1) + c3(n -1) + c4 (n -1)  = O (n), which indicates that the time complexity is linear. Worst Case:

Write stream analogues of list processing functions, (a) Write (delay ) as...

(a) Write (delay ) as a special form for (lambda () ) and (force ), as discussed in class. (b) Write (stream-cons x y) as a special form, as discussed in class. (c) Write

Graph traversal schemes, Q. Explain various graph traversal schemes and wri...

Q. Explain various graph traversal schemes and write their advantages and disadvantages. A n s . Graph Traversal Scheme is explained below In many troubles we wish

Representing sparse matrix in memory using array, Q. What do you understand...

Q. What do you understand by the term sparse matrix? How sparse matrix is stored in the memory of a computer? Write down the function to find out the transpose of a sparse matrix u

Write about enterprise manager, Question 1 . Give the structure of PL/SQL B...

Question 1 . Give the structure of PL/SQL Blocks and explain Question 2 . Differentiate between PL/SQL functions and procedures Question 3 . Explain the following Par

Best case, for i=1 to n if a[i}>7 for j=2 to n a[j]=a{j}+j for n=2 to n a...

for i=1 to n if a[i}>7 for j=2 to n a[j]=a{j}+j for n=2 to n a[k]=a[j]+i else if a[1]>4 && a[1] for 2 to a[1] a[j]= a{j]+5 else for 2to n a[j]=a[j]+i ..

Stack, Explain in detail the algorithmic implementation of multiple stacks....

Explain in detail the algorithmic implementation of multiple stacks.

Splay trees, Addition of new records in a Binary tree structure always occu...

Addition of new records in a Binary tree structure always occurs as leaf nodes, which are further away from the root node making their access slower. If this new record is to be ac

Linked lists, representation of links list in memory

representation of links list in memory

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