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

Explain merge sort, Question 1 Explain the use of algorithms in computing ...

Question 1 Explain the use of algorithms in computing Question 2 Explain time complexity and space complexity of an algorithm Question 3 Explain how you can analyz

Types of a linked list, A linked list can be of the following types:- ...

A linked list can be of the following types:-  Linear linked list or one way list Doubly linked list or two way list. Circular linked list Header linked list

Graphs, c program to represent a graph as an adjacency multilist form

c program to represent a graph as an adjacency multilist form

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

Boundary tag method in context of dynamic memory management, Q. How can we ...

Q. How can we free the memory by using Boundary tag method in the context of Dynamic memory management?

Explain avl tree, AVL tree An AVL tree is a binary search tree in which...

AVL tree An AVL tree is a binary search tree in which the height of the left and right subtree of the root vary by at most 1 and in which the left and right subtrees are again

Deletion of a node from an avl tree, For AVL trees the deletion algorithm i...

For AVL trees the deletion algorithm is a little more complicated as there are various extra steps involved in the deletion of node. If the node is not a leaf node, then it contain

Define about the structure - container, Define about the Structure - Contai...

Define about the Structure - Container - Some containers hold elements in some sort of structure, and some don't. Containers with no structure include bags and sets. Containe

Advantages of first in first out method, Advantages of First in First out (...

Advantages of First in First out (FIFO) Costing Advantages claimed for first in first  out (FIFO)  costing method are: 1. Materials used are drawn from the cost record in

Kruskal algorithm for minimum spanning, Implementations of Kruskal's algori...

Implementations of Kruskal's algorithm for Minimum Spanning Tree. You are implementing Kruskal's algorithm here. Please implement the array-based Union-Find data structure.

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