0% found this document useful (0 votes)
18 views9 pages

DS Experiment No 04

The document describes implementing a linear queue using an array data structure in C programming language. It defines key queue concepts like FIFO and uses front and rear pointers to track the head and tail. The main steps are: 1. Define a queue struct with front and rear indexes and an array. 2. Implement functions to initialize the queue, check if full/empty, insert, remove, print, and peek elements. 3. The code uses a circular queue approach to avoid buffer re-filling issues. 4. A menu driver is included to test the queue functions.

Uploaded by

jadhavkalpesh881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

DS Experiment No 04

The document describes implementing a linear queue using an array data structure in C programming language. It defines key queue concepts like FIFO and uses front and rear pointers to track the head and tail. The main steps are: 1. Define a queue struct with front and rear indexes and an array. 2. Implement functions to initialize the queue, check if full/empty, insert, remove, print, and peek elements. 3. The code uses a circular queue approach to avoid buffer re-filling issues. 4. A menu driver is included to test the queue functions.

Uploaded by

jadhavkalpesh881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Aim:

write a program to Implement Linear Queue ADT using array

Input Specification :
elements for Queue

Output Specification :
execution of fundamental operations on queue.

Theory :
A Queue is an ordered list in which all insertion takes place at one end and all
deletions take place at the opposite end. Since the first element removed is
the first element inserted, queues are also known as First In First Out (FIFO)
lists. The end at which insertions are taken place is called 'rear and the end at
which deletions take place is called 'front’ .
In a standard queue data structure re-buffering problem occurs for each
dequeue operation. That means, the queue tells it is full even though there are
empty locations. This problem is solved by joining the front and rear ends of
the queue to make the queue as a circular queue. When rear ==MaxSize-1 the
next element is entered at queue [0] in case that is empty .
Queues are frequently used in computer programming, and one example is the
creation of a job queue by an Operating System. If the OS does not use
priorities, the jobs a processed in the order they enter the system .

Alogorithm :
Step 1: Include all the header files which are used in the program and
constant Max with special value.
Step 2: declare all the user defined functions which one used in
queue implementation

Step 3: Create a one dimensional array with defined size

Step 4: Define two integer variables front and rear and initilize both
with -1

Step 5 : Then implement main method by displaying menu of


operations hot and make list and male suitable functions calls to
perform operation selected by the

Insertq ();

step 1: check whether queue is full.

Step 2 : if it is Pull, then display "queue is the function. Pull", and


terminate function .

Steps 3 : if it is NOT fall then increment rear by one & and insert the
value.
Step 4: END

Printq () ;

step 1: check whether queue IS EMPTY

Step 2 : If it is FTPpy Man display "Quellt iS EMPTY" and desembrade


the Canction..

Step 3 : if (1 is not emply then detaine an Integn voriable and Ted In


Bond 1

Step 4: display value and Increment ''1' value by one ( i++) ,repeat
the same until value reach to rear (i<=rear )

Step 5 : end

PROFRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define mxsize 5
struct queue
{
int front,rear;
int arr[mxsize];
};
typedef struct queue QUE;
void initq(QUE *q)
{
q->front = 0;
q->rear = -1;
}
int isfullq(QUE q)
{
if(q.rear == mxsize-1)
return 1;
else
return 0;
}
int isemptyq(QUE q)
{
if(q.rear == -1) // if(q.rear<q.front)
return 1;
else
return 0;
}
void insertq(QUE *q)
{
int val;
if(isfullq(*q))
printf("No space to add element in queue.....Cannot add.\n");
else
{
printf("Enter value to be added : ");
scanf("%d",&val);
q->rear = q->rear + 1;
q->arr[q->rear] = val;
}
}
void removeq(QUE *q)
{
if(isemptyq(*q))
printf("Queue is Empty.....Cannot remove.\n");
else
{
printf("%d is removed from queue.\n",q->arr[q->front++]);
if(q->front==q->rear+1)
{
q->front = 0;
q->rear = -1;
}
}
}
void printdq(QUE q)
{
int i;
if(isemptyq(q))
printf("Queue is Empty.....\n");
else
{
printf("Elements of queue are.....\n");
for(i=q.front;i<=q.rear;i++)
printf("%d\t",q.arr[i]);
printf("\n");
}
}
int peekq(QUE q)
{
return q.arr[q.front];
}
#define mxsize 5
void desesort(QUE *q)
{
int i,j,temp;
for(i=0;i<q->rear;i++)
for(j=i+1;j<=q->rear;j++)
if(q->arr[i]<q->arr[j])
{
temp = q->arr[i];
q->arr[i] = q->arr[j];
q->arr[j] = temp;
}
}
void main()
{
QUE q;
int x,ch=1;
clrscr();
initq(&q);
printf("\n M E N U for operations on Queue......\n");
printf("1. Add element in an queue \n");
printf("2. Delete element from an queue \n");
printf("3. Print queue \n");
printf("4. Peek element\n");
printf("5. Quit\n");
while(ch)
{
printf("\nEnter your choice of operation : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : insertq(&q);
desesort(&q);
if(!isemptyq(q))
printdq(q);
break;
case 2 : removeq(&q);
if(!isemptyq(q))
printdq(q);
break;
case 3 : printdq(q);
break;
case 4 : if(!isemptyq(q))
{
x = peekq(q);
printf("%d is at the front of an queue.\n",x);
}
else
printf("Cannot peek as Queue is Empty.....\n");
break;
case 5 : exit(1);
default: printf("ERROR ! Entered wrong choice.....Try
again.....\n");
}
}
getch();
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy