0% found this document useful (0 votes)
21 views7 pages

Can we be strangers again

A Queue is a linear data structure that follows the First-in-First-out (FIFO) principle, allowing insertion at the rear and deletion from the front. It can be implemented using an array or a linked list, with each having its advantages and disadvantages. Queues are commonly used in various applications such as print jobs, computer networks, and operating systems for managing processes.

Uploaded by

anshumansahu505
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)
21 views7 pages

Can we be strangers again

A Queue is a linear data structure that follows the First-in-First-out (FIFO) principle, allowing insertion at the rear and deletion from the front. It can be implemented using an array or a linked list, with each having its advantages and disadvantages. Queues are commonly used in various applications such as print jobs, computer networks, and operating systems for managing processes.

Uploaded by

anshumansahu505
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/ 7

Queues

A Queue is a linear data structure. A Queue is an ordered collection of homogenous data elements
where an insertion operation takes place at one end and the deletion operation takes place at the
other end. A Queue is also called as FIFO list.

Queue Abstract Data Type (QUEUE -ADT):

The Queue Abstract Data Type: The queue differs from a stack in that its insertion and removal
routines follow the First-in-first-out (FIFO) principle. Elements are inserted at the rear
(enqueued) and removed from the front (dequeued).

A queue is a list with two special properties. First, new items can be added only to the end of the
list. In this respect, the queue is like the simple list. Second, items can be removed from the list
only at the beginning. You can visualize a queue as a line of people buying tickets to a theater. You
join the line at the end, and you leave the line at the front, after purchasing your tickets. A queue
is a first in, first out (FIFO) data form

Basic operations

 Enqueue(q,x): Insert x at rear end of the queue q


 Dequeue(q): Remove object from the front, error if empty
 Size(q) : returns size of the queue. (no of elements)
 Isempty(q): retrun a boolean indicating queue is empty
 First(q): return the object at front without removing

Implementing the Interface Data Representation :

The first step is deciding what C data form to use for a queue. One possibility is an array. The
advantages to arrays are that they're easy to use and that adding an item to the end of an array's
filled portion is easy. The problem comes with removing an item from the front of the queue.

A clever solution to the dead space problem is to make the queue circular. This means wrapping
around from the end of the array to the beginning. That is, consider the first element of the array

Ch. Sree Kumar Page 1


as immediately following the last element so that when you reach the end of the array, you can
start adding items to the beginning elements if they have been vacated

Yet another solution is to use a linked list. This has the advantage that removing the front item
doesn't require moving all the other items. Instead, you just reset the front pointer to point to the
new first element.

Representation of queues

It is also possible to represent Queues using:

1. Array-based representation
2. Linked representation

Array implementation :

Fig: shows the array implementation of a Linear queue

Queues can be easily implemented using linear array. Every queue has front and rear variables that
point to the position from where deletions and insertions can be done respectively.

The advantages of this representation is that it is easy to represent Queue using the array
representation.
The disadvantage is it is possible to store only a fixed number of elements and wastage of memory
space can also occur. hence solution is to use Circular queue (wrap around & use a length variable to
keep track of the queue length).

Linked representation of queue :

In many applications, it is not possible to predict the size of the queue in advance and it may vary
from time to time. The representation of queue using a linked list can be used. The representation
overcomes all the problems of the array representation of the queue.

The advantages of this type of representation are

 There is no wastage of memory space

Ch. Sree Kumar Page 2


 Insertions and deletions operations are easy to do
 Size of the queue is not fixed (any number of elements can be placed in the queue
dynamically

Disadvantages are :

 Extra pointer fields are used along with each data item in the queue. They take some extra
memory space.

Fig: shows the Linked implementation of a linear queue.

Applications of Queues

• Print jobs
• Computer networks
• Real-life waiting lines
• When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk
Scheduling.
• Typical uses of queues are in simulations and operating systems.

Operating systems often maintain a queue of processes that are ready to execute or that are
waiting for a particular event to occur.

Computer systems must often provide a “holding area” for messages between two processes, two
programs, or even two systems. This holding area is usually called a “buffer” and is often
implemented as a queue.

Figures showing the Enqueue and Dequeue operations in a linear queue:

Ch. Sree Kumar Page 3


En-queue is the process of appending data to queue.De-queue is the process of removing data from
queue.

Along with these 2 operations the other two functions are isempty() and isfull() which tells the
empty condition or the full condition of the queue respectively.

what is the basic difference between stack and queue?

STACK: QUEUE:
1. Stack is defined as a list of 1. Queue is a collection of the
element in which we can insert same type of element. It is a
or delete elements only at the linear list in which insertions
top of the stack. can take place at one end of the
2. The behavior of a stack is like list,called rear of the list,
a Last-In First-Out(LIFO) and deletions can take place
system. only at other end, called
3. Stack is used to pass parameters the front of the list
between function. On a call to a 2. The behaviour of a queue is like
function, the parameters and a First-In-First-Out (FIFO)

Ch. Sree Kumar Page 4


local variables are stored on a system.
stack.
4. High-level programming languages
such as Pascal, c, etc. that
provide support for recursion
use the stack for bookkeeping.
Remember in each recursive call,
there is a need to save the
current value of parameters,
local variables, and the return
address (the address to which
the control has to return after
the call).

Write a C- program to implementation of Linear - QUEUE .


Note : Menu based program is required.

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
typedef struct queue
{
int rear,front;
int arr[MAX];
}queue;

void init(queue *q)


{
q->rear=-1;
q->front=0;
}

int isFull(queue *q)


{
if(q->rear==MAX-1)
return 1;
else
return 0;
}
int isEmpty(queue *q)
{
if(q->front>q->rear)
return 1;
else
return 0;
Ch. Sree Kumar Page 5
}
void insert(queue *q,int ele)
{
if(isFull(q))
{
printf("Queue is full\n");
exit(0);
}
else
{
q->rear++;
q->arr[q->rear]=ele;
}
}
int delete(queue *q)
{
int ele;
if(isEmpty(q))
{
printf("Queue is empty\n");
return -1;
}
else
{
ele=q->arr[q->front];
q->front++;
return ele;
}
}
void display(queue *q)
{
int i;
if(isEmpty(q))
{
printf("Queue is empty\n");

}
else
{
for(i=q->front;i<=q->rear;i++)
{
printf("%d\t",q->arr[i]);
}
}
}
Ch. Sree Kumar Page 6
int count(queue *q)
{
int count=0,i;
for(i=q->front;i<=q->rear;i++)
{
count++;
}
return count;
}
int main()
{
int ch,ele,number=0;
queue q;
init(&q);
do
{
printf("\n............MENU...............\n");
printf("\n Enter 1 for inserting an element\n");
printf("\n Enter 2 for deleting an element\n");
printf("\n Enter 3 for displaying the elements present\n");
printf("\n Enter 4 for counting number of elements present\n");
printf("\n Enter 5 to exit\n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element to be entered\n");
scanf("%d",&ele);
insert(&q,ele);
break;
case 2: ele=delete(&q);
printf("\nDeleted element=%d",ele);
break;
case 3: display(&q);
break;
case 4: number=count(&q);
printf("\nNumber of elements in queue=%d\n",number);
break;
case 5: exit(0);
default:printf("\nWrong choice!!!!!!\n");
break;
}
}while(ch!=5);
return 0;
}
Ch. Sree Kumar Page 7

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