answersLogoWhite

0


Best Answer

//implement double ended queue using array.

#include<stdio.h>

#include<conio.h>

#define SIZE 20

typedef struct dq_t

{

int front,rear;

int item[SIZE];

}deque;

/********** Function Declaration begins **********/

void create(deque *);

void display(deque *);

void insert_rear(deque *, int);

void insert_front(deque *, int);

int delete_front(deque *, int);

int delete_rear(deque *, int);

/********** Function Declaration ends **********/

void main()

{

int data,ch,x;

deque DQ;

clrscr();

create(&DQ);

printf("\n\t\t Program shows working of double ended queue");

do

{

printf("\n\t\t Menu");

printf("\n\t\t 1: insert at rear end");

printf("\n\t\t 2: insert at front end");

printf("\n\t\t 3: delete from front end");

printf("\n\t\t 4: delete from rear end");

printf("\n\t\t 5: exit. ");

printf("\n\t\t Enter choice :");

scanf("%d",&ch);

switch(ch)

{

case 1:

if (DQ.rear >= SIZE)

{

printf("\n Deque is full at rear end");

continue;

}

else

{

printf("\n Enter element to be added at rear end :");

scanf("%d",&data);

insert_rear(&DQ,data);

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 2:

if (DQ.front <=0)

{

printf("\n Deque is full at front end");

continue;

}

else

{

printf("\n Enter element to be added at front end :");

scanf("%d",&data);

insert_front(&DQ,data);

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 3:

x = delete_front(&DQ,data);

if (DQ.front==0)

{

continue;

}

else

{

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 4:

x = delete_rear(&DQ,data);

if (DQ.rear==0)

{

continue;

}

else

{

printf("\n Elements in a deque are :");

display(&DQ);

continue;

}

case 5: printf("\n finish");

return;

}

}

while(ch!=5);

getch();

}

/********** Creating an empty double ended queue **********/

/********** Function Definition begins **********/

void create(deque *DQ)

{

DQ->front=0;

DQ->rear =0;

}

/********** Function Definition ends **********/

/********** Inserting element at rear end **********/

/********** Function Definition begins **********/

void insert_rear(deque *DQ, int data)

{

if ((DQ->front 0)

{

printf("\n Underflow");

return(0);

}

else

{

DQ->rear = DQ->rear -1;

data = DQ->item[DQ->rear];

printf("\n Element %d is deleted from rear:",data);

}

if (DQ->front==DQ->rear)

{

DQ->front =0;

DQ->rear = 0;

printf("\n Deque is empty(rear end)");

}

return data;

}

/********** Function Definition ends **********/

/********** Displaying elements of DEQUE **********/

/********** Function Definition begins **********/

void display(deque *DQ)

{

int x;

for(x=DQ->front;x<DQ->rear;x++)

{

printf("%d\t",DQ->item[x]);

}

printf("\n\n");

}

/********** Function Definition ends **********/

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

If this is a homework assignment, you really should consider trying it yourself, first.

A linear queue is a first-in, first-out structure, similar to a line of people at a check-out register in a store. Using an array as the list of pointers to strings, one way to do this is... (A circular queue)

This example uses ellipses (...) to represent tabs for clarity

#define MAX_Q_SIZE (1000) /* or whatever max size you want */
char *queue[MAX_Q_SIZE];
int in_index = 0;
int out_index = 0;

int insert(char *str) {
... if ((in_index + 1) % MAX_Q_SIZE == out_index) return 0; /* error - queue full */
... queue[in_index] = str;
... in_index = (in_index + 1) % MAX_Q_SIZE;
... return 1; /* success */
}

char* remove() {
... char *temp;
... if (in_index == out_index) return null; /* queue empty */
... temp = queue[out_index];
... out_index = (out_index + 1) % MAX_Q_SIZE;
... return temp; /* success */
}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<stdio.h>

#include<conio.h>

#define MAXSIZE 4

void pqinsert();

void pqdelete();

void display();

int pqueue[3][MAXSIZE];

int front[3]={0,0,0};

int rear[3]={-1,-1,-1};

int item,pr;

void main()

{

char ch;

int choice;

clrscr();

do

{

printf("\n*************** Queue Operations **************\n");

printf("\t\t 1-> PQInsert\n");

printf("\t\t 2-> PQDelete\n");

printf("\t\t 3-> Display\n");

printf("\t\t 4-> Exit\n");

printf("\t\t Enter your choice:");

scanf("%d",&choice);

switch(choice)

{

case 1: printf("\t\t Enter the priority:");

scanf("%d",&pr);

if(pr>0 && pr<4)

pqinsert(pr-1);

else

printf("\t\t Invalid priority\n");

break;

case 2: pqdelete();

break;

case 3: display();

break;

case 4: exit(0);

break;

default: printf("\t\tInvalid choice\n");

break;

}

printf("\t\tDo you want to continue(type y or n):");

fflush(stdin);

scanf("%c",&ch);

} while(ch!='n');

getch();

}

void pqinsert(int pr)

{

int item;

if(rear[pr]==MAXSIZE-1)

printf("\t\t PQueue Overflows\n");

else

{

printf("\t\t Enter the element:");

scanf("%d",&item);

rear[pr]++;

pqueue[pr][rear[pr]]=item;

}

}

void pqdelete()

{

static int i=0 ;

if(rear[i]==front[i]-1)

{

printf("\t\t Queue %d Underflows\n",i+1);

i++;

}

else

{

printf("\t\t The deleted element is %d of queue %d:\n",pqueue[i][front[i]],i+1);

front[i]++;

}

}

}

void display()

{

int i,j;

for(i=0;i<3;i++)

{

if(rear[i]==front[i]-1)

printf("\t\t Queue %d Underflows\n:",i+1);

else

{

printf("\t\tThe elements in Queue %d are\n",i+1);

printf("\t");

for(j=front[i];j<=rear[i];j++)

printf("\t%d",pqueue[i][j]);

printf("\n");

}

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for the two dimensional array repesentation of priority queue?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


Array implementation of priority queue example program in c plus plus?

yes


How does two dimensional array differ from single dimensional array?

A one dimensional array is a scalar value repeated one or more times.A two dimensional array is an array of one dimensional arrays.A three dimensional array is an array of two dimensional arrays, and so forth.The one dimensional array is like a list of things, where the two dimensional array is like an array of things. (Think one row of a spreadsheet versus the whole spreadsheet.)[addendum]Every level of array depth is also a level of pointer depth. For example: A 3 dimensional int array is an int***. So a one dimensional int array is an int*, and a two dimensional int array is an int**. This is only important if you are doing pointer work, but it can become very important.


What is a multi array?

A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right. A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables. Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.


How do you write a program in C to find and display the sum of each row and column of a 2 dimensional array of type float?

array type


What does a 2 dimensional array do?

A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.


Explain the Different types of array?

one dementional array and two dementional array


What is single dimensional array in c plus plus?

A one dimensional array is an array of objects that goes in one "direction". Any array with only one [] is a one dimensional array. For example: int numbers[6]; is a one dimensional array. int numbers[6][3]; is a two dimensional array.Graphical terms:One dimensional array[4]:14 - 75 - 8164 - 234Two dimensional array[2][3]:47 - 178108 - 8517 - 128It didn't come out quite how I wanted it...


How can one become a member of the Priority Club Rewards program?

Members of the Priority Club Rewards program are privy to a vast array of benefits and discounts. One can apply to become a member through the company's official website.


What is a table array?

A two-dimensional array.


How many types of arrays in c?

An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.


What are the types of array?

There are several different types of arrays, which describe the characteristics of the array. Arrays may be static or dynamic. Static arrays have a predetermined size that will not change over the course of the program's life cycle, while dynamic arrays may be made larger or smaller as necessary while the program runs. Arrays may be one-dimensional or multi-dimensional. An array of just one dimension stores values in a straight line, while a multi-dimensional array represents data that might be rectangular, such as the dots in an image, or even 3 dimensional, such as a multi-layer image, an animation, etc.