Can we be strangers again
Can we be strangers again
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.
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
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
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
1. Array-based representation
2. Linked representation
Array implementation :
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).
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.
Disadvantages are :
Extra pointer fields are used along with each data item in the queue. They take some extra
memory space.
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.
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.
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)
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
typedef struct queue
{
int rear,front;
int arr[MAX];
}queue;
}
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